Creating an Ansible playbook that will retrieve newly launched container IP and update the inventory.
This is the continuation of Task 10 I have implemented before. Check out the link below if you haven't
In the previous Task 10, we implemented the docker setup into the remote system using Ansible. And now in this task, we will write an Ansible script to retrieve the IP of the launched docker container so that we can use that container to build infrastructure using the Ansible
Let's get started
Firstly we will create an ansible-playbook and copy the below code
- hosts: localhost vars_prompt: - name: docker_name prompt: Enter the name of docker container- tasks: - name: start docker service service: name: docker - name: Pulling httpd image docker_image: name: "httpd" source: pull - name: Launch container with given name docker_container: name: "{{ docker_name }}" image: "httpd" state: started volumes: "/var/www/html/:/usr/local/apache2/htdocs" - name: docker container info docker_container_info: name: "{{docker_name }}" register: result - debug: var : result.container.NetworkSettings.IPAddress - name: Updating inventory file blockinfile: block: | [dockerip] {{ result.container.NetworkSettings.IPAddress }} ansible_user=root ansible_connection=ssh
path: "/hosts.txt"
In the above file, the last block of code will update the IP of the container in the inventory file used by the ansible
As we can see the debug section output that we have successfully retrieved the IP of the container