Create An Ansible Playbook to Retrieve the Container IP and update the Inventory File & Configure the Webserver.

Shashwat Singh
Towards AWS
Published in
3 min readMar 30, 2021

--

Let's First Build An Docker Image In such a way so that we can do SSH to it. We can do either manually or by the help of DockerFile ;

Manually Configuring

OR

FROM centos:latest
RUN yum install net-tools -y
RUN yum install openssh-server -y
RUN ssh-keygen -A
RUN yum install passwd -y
CMD echo redhat | passwd root --stdin
CMD /usr/sbin/sshd

Now, let's Build the Image and Lunch it. We can also build & run this container with the help of the Ansible.

Now, let's Create the Playbook which will Dynamically Retrieve the Name and IP of the running Container and also update the Inventory File !!

- hosts: localhosttasks:- name:  Getting the Docker Container Name
shell: "docker ps --format '{% raw %}{{ .Names }}{% endraw %}'"
register: Docker_name
- name: Getting the IP of Docker Container
shell: "docker inspect --format '{%raw %}{{ .NetworkSettings.IPAddress }}{% endraw %}' $(docker ps --format '{% raw %}{{ .Names }}{% endraw %}')"
register: Docker_IP
- name: Updating the Inventory File
blockinfile:
path: /root/dbIP/docker
block: |
{% raw %}[{% endraw %}{{ Docker_name.stdout }}{%raw %}]{% endraw %}
{{ Docker_IP.stdout }} ansible_ssh_user=root ansible_ssh_pass=redhat

Here, We are running this Playbook at localhost because Our BaseOS is working as Docker Engine.

Updated Inventory File

Let's Configure the Webserver on the Fetched data of a Container !!

For this Configuration, I am Going to use the Role of WebServer →

--

--