Retrieving Docker Container IP and Launching Webserver on Docker Container Using Ansible

Juzer Patanwala
3 min readMar 28, 2021

--

In this blog,we will launch a docker container using Ansible,retrieve it’s IP address and add it in the ansible inventory file dynamically.Then,finally we will configure the webserver on this newly launched docker container using ansible

Pre-requisite

  • For doing this practical,you need to know about docker and have docker configured in the target machine.To configure docker using ansible,you can refer to my blog in which I have explained every step in very detail.
  • For connecting to the docker container from Ansible,we need to have ssh services enabled in the container.I have created my own image using Dockerfile in which I have installed and enabled the openssh-server.You can either use my image by going to this link or create your own image.

Now that,you know about docker,we can start off with our hands-on part

Launch docker container

We will be using the docker_container module for lauching the container.

- name: Launch the container with ssh enabled
docker_container:
name: "{{ container_name }}"
image: juzerpatan/ssh-server:v1
state: started
interactive: yes
tty: yes
detach: yes
ports:
- "8090:80"

Retrieve the container IP address

We will be using the docker_container_info module to retrieve information about the container.

- name: Get infos on container
docker_container_info:
name: "{{ container_name }}"
register: result

The output is stored in the variable result.The output contains alot of information about the container from which we have to filter the container IP.

- debug:
var : result['container']['NetworkSettings']['IPAddress']

The above debug task will print the container IP.

Update the inventory file

Next,we have to add the container IP,it’s username and password in the ansible inventory file.For this,we will use the lineinfile module which has the capability of adding new lines to a file.

- name: Update the inventory
lineinfile:
path: ./myhosts.txt
line: "{{ item }} "
loop:
- "[docker]"
- "{{ result['container']['NetworkSettings']['IPAddress'] }} ansible_ssh_user=root ansible_password=redhat"

The ansible_ssh_user is root and the password is redhat because these are the values I have used in my docker image.

The entire playbook for your reference

- hosts: localhost
vars_prompt:
- name : container_name
promt: 'Enter the container name'
private: no
tasks:
- name: Launch the container with ssh enabled
docker_container:
name: "{{ container_name }}"
image: juzerpatan/ssh-server:v1
state: started
interactive: yes
tty: yes
detach: yes
ports:
- "8090:80"
- name: Get infos on container
docker_container_info:
name: "{{ container_name }}"
register: result
- debug:
var : result['container']['NetworkSettings']['IPAddress']
- name: Update the inventory
lineinfile:
path: ./myhosts.txt
line: "{{ item }} "
loop:
- "[docker]"
- "{{ result['container']['NetworkSettings']['IPAddress'] }} ansible_ssh_user=root ansible_password=redhat"

Run the playbook

The inventory is successfully updated

Launch webserver on the container

For this,we have to first install the software httpd.

- name: Install httpd
package:
name: "httpd"
state: present

Copy the webpage content in the file /var/www/html/index.html

- name: Copy Contents of Webpage
copy:
content: "This is {{ ansible_default_ipv4['address'] }}"
dest: "/var/www/html/index.html"
mode: 0777

Start the httpd services by running the command /usr/sbin/httpd

- name: Start httpd
command: /usr/sbin/httpd
register: httpd_status

Run the playbook

You can now access the webserver using the container’s IP address

Hence,the webserver is successfully launched on the container using Ansible

Thank You!!

--

--

Juzer Patanwala

Cloud Engineer @ Searce Inc || Technical Content Writer || Technology and Automation Enthusiast