Ansible is an open-source software provisioning, configuration management, and application deployment tool. It uses declarative language to describe system configuration, making it easy to use.
Ansible uses a simple syntax written in YAML called playbooks. It is agentless, meaning it doesn't require any software installed on remote nodes to perform its tasks. Instead, it uses SSH to execute commands and copy resources to remote machines. Ansible is also idempotent, ensuring that the same playbook can be run multiple times without changing the final state beyond the first run. It's widely used for IT automation tasks.
Preparing the Ansible Dev Environment
Before getting started, we need to install Ansible and its dependencies on our server. As Ansible is based on Python, the latest versions are available on the PIP package repository. Note that the package repository of our host system may not have an up-to-date package, so pip is the recommended platform for getting the latest versions.
To install Ansible, simply run,
pip install ansible
If you don't have pip in your system, then simply run the following:
Debian/Ubuntu
apt install python3-pip
CentOs/RHEL
yum install epel-release
yum install python3-pip
Note: Use sudo for package installation if the install fails with a lack of privilege/permission errors.
Windows Systems
# Download the get-pip.py script
Invoke-WebRequest -Uri https://bootstrap.pypa.io/get-pip.py -OutFile get-pip.py
# Install pip
python3 get-pip.py
Provisioning the Server
We will now install Docker and Start up a Docker Stack with Ansible.
Place the configuration files provided below in the same directory. Name them according to their codeblock captions.
All the required configuration is down in the Jinja Template for Ansible. This makes it easy to generate the playbook with the necessary variables placed into it in its final form. The passwords can be encrypted using ansible-vault
which is the recommended way to do so.
This generate_playbook.yaml
file is used to generate the main playbook from the playbook.yaml.j2
template file above.
This file contains credentials to the docker registry. Although it's not required for public packages in dockerhub, it has been used here to encourage the use of a private registry for hosting docker images.
Run ansible-vault encrypt docker_credentials.txt
, you will be asked to enter a password which will be used to encrypt the password file. Once completed the username/passwords will be replaced with the encrypted form of the same.
This is the main docker-compose file, which will be used to create the docker stack containing the nginx web service.
The hosts.ini
file consists of the server host IP address and username with which to access the server.
The variables.yaml
file is used to offload all values that require frequent changes
The password used to encrypt the vault is saved as vault_password.txt
. Instead of providing the password on every run, we can point Ansible to this file to ensure faster execution of playbooks.
Once all the files are available and placed in the directory, we can run the following command to set up our server and start the docker swarm stack.
ansible-playbook --vault-password-file vault_password.txt generate_playbook.yaml -i hosts.ini
This will generate the playbook file according to the Jinja template and the provided variables. Remember that the provided passwords will also be available in the playbook file in cleartext. Don't commit this file to version control.
ansible-playbook playbook.yaml -i hosts.ini
Finally, now the server will be provisioned according to the instructions we provided in the playbook.
If you SSH into the server, you should see that docker has been installed, the nginx container has been deployed, and the necessary packages, like pip
, etc, are also available in the server.
Conclusion
In this way we learned about Ansible, provisioned a server with Ansible and deployed a Docker Swarm Stack with Ansible.
Thank you for reading, I periodically try to update my articles to ensure legibility.