How to Install Ansible using Docker Compose (Build Ansible Container) Ubuntu 20.04. In this guide we will introduce Ansible with it’s main advantages then move on to installation with Docker Compose.
Ansible is a free, open source and on of the most popular automation tool used by system administrators and developers to configure and manage multiple servers from a central place. It helps you to build your entire infrastructure in minimal time. Installing Ansible inside a container will helps you to easily use your Ansible environment wherever you need it. You can build your Ansible container image and copy it across environments as needed.
Next in this blog tutorial about how to Install Ansible using Docker Compose (Build Ansible Container) Ubuntu 20.04 is to introduce Ansible.
Individual associated with IT knows how significant Ansible is to them. It is an open source automation tool or platform that is used for fulfilling complex IT tasks, like configuration management, application deployment, intra service orchestration, and provisioning. It is widely used for multi tier deployments and its models within the IT infrastructure in one deployment. Therefore it eliminates the hassle of handling them separately.
So, with the help of Ansible, you can get a desired state of infrastructure and crush the complexity of infrastructure scalability. It also helps in automating the entire process of infrastructure provisioning and configuration. It is usually done by handling it with the help of the code.
Using Ansible, you can handle any scale of infrastructure, even if it is as small as defining a package that needs to be installed on a system and as large as setting up a multi tier orchestration.
Moreover, Ansible becomes really significant when the configuration of the server’s manual becomes a very difficult task. At this point, it helps you with:
Managing Configuration – Ansible manages configuration by providing control at a granular level. It provides consistency in configuration management.
Deploying Application – Ansible manages applications from development to production quite straightforwardly with just a few changes.
Orchestration – Ansible teaches you the process of configuring infra in the entire server.
Security And Compliance – With the assistance of Ansible, you can also deploy a security policy. It provides you with the server’s best practices.
Ansible provides you with the following advantages:
Simple To Learn
Due to its simplicity, Ansible is considered the best tool for beginners as well. Its easy to learn features enable you to learn using Ansible fast. It also helps in increasing productivity.
Written In Python
Since Ansible is written in Python, getting it up and using it becomes effortless in it. Also, Python is a language that is common for administration and scripting tasks. Therefore, engineers and system administrations are familiar with Python as opposed to another language, like Ruby.
Do Not Depend On Agents
Ansible’s agentless nature enables it to manage all the master agent communication via Standard SSH or Paramiko module. Since the Paramiko module is a Python implementation of SSH2, it is very crucial for managing nodes. Also, it restricts Ansible from using any forms of agents installed on the remote system to ensure management. This way, it reduces maintenance overheads and performance degradations by vast margins.
YAMP Based Playbooks
Playbooks, one of the Ansible configuration files is written in the YAML language. It makes it a perfect fit for configuration management and automation purposes as compared to other formats like JSON. This format is effortless to read, supports comments and employs the use of anchors for referencing other items.
Ansible Galaxy
Ansible Galaxy is a portal that acts as a central repository for locating, reusing and sharing content related to Ansible. For instance, downloading reusable Roles for installing applications and server configuration makes them ideal for use in a particular user’s playbooks. It can contribute substantially to an increase in deployment speed.
Next in this post, we will show you how to install Ansible using Docker Compose on Ubuntu 20.04.
Before starting, you will need to install Docker CE on your server. By default, the latest version of Docker is not included in the Ubuntu default repository. So you will need to install it from the Docker’s official repository.
First, install all required dependencies using the following command:
Next, you will also need to install the Docker Compose to your server. At the time of writing this tutorial, the latest version of Docker Compose is v2.6.1. You can download it with the following command:
Save and close the file then create another Dockerfile for the Ubuntu container:
nano ssh-enabled-ubuntu/Dockerfile
Again here add the following lines:
FROM ubuntu:16.04
RUN apt-get update && apt-get install -y openssh-server
RUN mkdir /var/run/sshd
RUN echo 'rootpassword' | chpasswd
RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
At this point, all the configuration files are ready to launch the Ansible container. You can now change the directory to ansible and run the following command to launch the Ansible container:
cd ansible
docker-compose up -d
This command will download Ubuntu images from the Docker hub, install Ansible inside the Ubuntu container and launch both container as shown below:
[+] Building 0.2s (6/6) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 493B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/ubuntu:latest 0.1s
=> [1/2] FROM docker.io/library/ubuntu@sha256:b6b83d3c331794420340093eb706a6f152d9c1fa51b262d9bf34594887c2c7ac 0.0s
=> CACHED [2/2] RUN apt-get update && apt-get install -y gcc python2-dev libkrb5-dev && apt-get install python3-pip -y && 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:e51c01029a4269ae4d8dbf11721f3fe96c43325eaaa9ed2d1ebe8d2dbbb2257e 0.0s
=> => naming to docker.io/library/ansible 0.0s
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
[+] Running 2/2
⠿ Container remote-host-one Created 0.1s
⠿ Container ansible Created 0.1s
Attaching to ansible, remote-host-one
You can now verify the downloaded images using the following command:
docker images
This results in the following output:
REPOSITORY TAG IMAGE ID CREATED SIZE
ansible latest e51c01029a42 12 minutes ago 978MB
remote-host-ssh latest 8e66e11df038 28 minutes ago 220MB
Verify the running containers using the following command:
docker ps
You will get the following output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b8f87b8b4cdf remote-host-ssh "/usr/sbin/sshd -D" 3 minutes ago Up 3 minutes 22/tcp remote-host-one
324f1c88f928 ansible "bash" 3 minutes ago Up 3 minutes ansible
If you want to connect to another Ubuntu container, run the SSH command:
ssh root@remote-host-one
Provide your root password which you have defined in the Dockerfile to connect to the container. Once you are connected, you will get the following output:
The authenticity of host 'remote-host-one (172.18.0.3)' can't be established.
ED25519 key fingerprint is SHA256:oIt+LV7hy0Xpb/jslLYQzX4fIufhM9Wc9EiJx2IOjns.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'remote-host-one' (ED25519) to the list of known hosts.
root@remote-host-one's password:
Welcome to Ubuntu 16.04.7 LTS (GNU/Linux 5.4.0-121-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.
Great Effort! We have learned how to Install Ansible using Docker Compose (Build Ansible Container) Ubuntu 20.04.
How to Install Ansible using Docker Compose (Build Ansible Container) Ubuntu 20.04. Conclusion
In this guide, we explained how to install Ansible using Docker Compose in Ubuntu 20.04. You can now create an inventory file to define remote nodes that you want to manage, create a playbook file to define your task. In summary you can run the Ansible playbook to execute all tasks on the remote nodes.
I hope you can now easily deploy Ansible in the Docker environment. You can also copy the Ansible container to other hosts and use it to manage your infrastructure.
I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.