Retrieving container IP and updating inventory — Ansible Demonstration

Inshiya Nalawala
2 min readApr 2, 2021

Please refer to my previous article where I configured Docker using Ansible.

This article extends that demonstration to further retrieve the new container IP and updating the inventory so that further configuration could be done inside the containers.

So, let’s get started!

Step 1: Get your inventory done correctly.

I created an empty host group [containers] at the beginning, here is where my container info will be loaded.

Step 2: If you followed this blog, you already know a lot.

The blog gives a detailed demonstration on how we can launch a container with Ansible. Thus, this step encapsulated all of those steps.

In this demo, we will use the same playbook but with certain changes.

First of all, here we will use a different image. This image is a simple CentOS docker image with SSH access. I am linking it here for you to go through the description.

Step 3: As we want to enable ssh for further configuration, we will expose that port. Also, the image supports a few environment variables like ROOT_PASS, so we will pass those values too while creating the container.

These values will be user configured and will be the input from the user before the playbook runs.

Step 4: Next, we want to get the information of the container to retrieve its IP, once it is launched. We will register the output and get the information.

Step 5: Once the information if grabbed, we can update the inventory using the lineinfile module. The empty host group that we created earlier will help us here as we will use that as a regular expression.

Step 6: After the playbook runs successfully, if you want to manually go and ssh into the docker container, just do it as you would do for any other host, or better let Ansible do it!

So, create a new playbook(you can continue in the same too, by writing a different PLAY)

In this play, our host group will be [containers]

Now, the rest is in your hands. Configure however you want to.

For a quick demo, I installed python3 here and it ran successfully

The playbook for container configuration:

- name: Configuring Inside the Container

hosts: containers

tasks:

## container configuration herer ##

- name: Installing some software

package:

name: python3

state: present

~

Thank You!

--

--