The first step is to install Ansible on the machine that will run the playbook. This machine is known as the control machine. You can install Ansible using the following commands:
sudo apt update sudo apt install software-properties-common sudo apt-add-repository --yes --update ppa:ansible/ansible sudo apt install ansible
These commands update the package index, install the necessary packages, add the Ansible repository to the sources, and finally install Ansible.
Once you have Ansible installed, you can create the playbook for deploying your Docker application. The playbook will install Docker, pull the Docker image, and start the Docker container.
Here’s an example playbook to get you started:
--- - name: Deploy Docker application hosts: <host-group-name> become: yes vars_files: - variables.yml tasks: - name: Install Docker apt: name: docker.io state: present - name: Pull Docker image docker_image: name: "{{ image_name }}" source: "{{ image_source }}" - name: Start Docker container docker_container: name: "{{ container_name }}" image: "{{ image_name }}" ports: - "{{ host_port }}:{{ container_port }}" state: started
Make sure to replace the placeholders <host-group-name>
with the appropriate host group name for your deployment.
As mentioned earlier, you can store the variables in a separate file. This makes your playbook more organized and easier to maintain.
Here’s an example of the variables file:
image_name: <image-name> image_source: <image-source> container_name: <container-name> host_port: <host-port> container_port: <container-port>
Make sure to replace the placeholders <image-name>
, <image-source>
, <container-name>
, <host-port>
, and <container-port>
with the appropriate values for your deployment.
Finally, run the playbook with the following command:
ansible-playbook <playbook-name>.yml
And that’s it! Your Docker application should now be deployed on your Ubuntu system using Ansible.
In conclusion, deploying a Docker application using Ansible on Ubuntu is a straightforward process. By using Ansible, you can automate the deployment process and ensure that your application is deployed consistently and reliably across multiple systems.
Quick Links