Ansible Playbook that does the following operations in the managed nodes

· Configure Docker
· Start and enable Docker services
· Pull the httpd server image from the Docker Hub
· Run the docker container and expose it to the public
· Copy the html code in /var/www/html directory and start the web server
1.Install and configure docker services
In order to write playbook that install docker services we have to to write a code to create a docker repository for this we can use docker “yum_repository” module i.e
yum_repository:
baseurl:///https:download.docker.com/linux/centos/7/x86_64/stable
gpgcheck: no

This will create a yum repository of the docker.
Now to install docker we can use ansible command module which will run command on terminal to install docker i.e
name: “Install docker-ce”
command: yum install docker-ce –nobest

This will install docker-ce in your target node
Next step is to start docker sevices in the target node for this we use the services package in ansible i.e
service:
name: “docker”
state: started

This will enable docker services it the target node.
After this we have to pull httpd image from docker hub
For this we will use the command module i.e.
name: “Pull httpd image”
command: docker pull httpd

Now we need to configure apache server by copying files to /var/www/html for this we can use the copy module in ansible.i.e
- copy:
dest: /var/www/html/index.html
content: “just for check\n”
After all this steps our ansible playbook is ready.
Before running it we can check if our target node is connected by pinging it by command
“ansible all -m ping”


Now we can run our playbook by command
“ansible-playbook <filename>.yml

As we can ansible is gathering the facts like os type storage available etc. After running the playbook

As seen in above image playbook have create the yum repository for docker. After running the playbook

Docker is installed via playbook at target node.

Docker have pulled httpd image from docker hub via ansible playbook at target node.

As we can see apache server is running with our test file and desired content is showing though we have completed the above given operations successfully