Create an Ansible playbook that will retrieve newContainer IP and update the inventory. So that further Configuration of Webserver could be done inside that Container.

Shobhit Singh Pal
3 min readMar 8, 2021

In the inventory we generally give the ip of the container that we will configure. But when we are to launch and configure a container then in that case we don’t know the ip of the container that will be launched.

To troubleshoot the problem of ip of container in the target node. We have concept of Tags. With the tags we tag the container with some name and that name allow us to dynamically retrieve the container ip so that we can make dynamically entry for the launched container and that can be used as host group for the play of configuration of the container.

As for the configuration of the docker container ansible will require its access means will need to log in it. For this purpose we launch container with our own custom ami which is down below:

To build the image

docker built -t image_name .

Here -t is for tag the image and . means current directory.

I launched a container with the image palshobhit/dockerssh which is built with the above Dockerfile. I also publish the 80 port of docker container on 1234 of local machine.

The playbook:

In the above the names define each task. The only thing which makes playbook powerful is the jinja raw string syntax i.e. {% raw %} {% endraw %}. We used it so that jinja does not apply its variable meaning to {{ some_name }} and render it as normal string.

Running the playbook:

After running the first play on localhost inventory look as down below:

In the inventory the host group os created with the entry of launched container. On this same os host group next play run which configures the container.

Output:

Conclusion: Jinja template is a powerful thing in ansible.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Shobhit Singh Pal
Shobhit Singh Pal

No responses yet

Write a response