To perform this project, I’m using RedHat version8 operating system for the controller as well as managed nodes.
Ansible can be installed in multiple ways but I’m installing from the official RedHat repository using the command:
# dnf install ansible
This will download and install the stable version of the ansible from the official RedHat repository.
As Ansible is installed, now we required to add the managed nodes to the inventory so that the controller node should know where to execute the scripts.
For that, let’s create a file named myHosts.txt and add the managed nodes entry to it.
# vi /etc/myHosts.txt
Add the managed node IP, user name, and password to the myHosts.txt file.
Now, add the /etc/myHosts.txt file as an inventory in the ansible configuration file on the controller node.
# vi /etc/ansible/ansible.cfg
We can check if the n hosts are added by using the following command:
# ansible all --list-hosts
Finally, we have the lab setup ready to perform the task.
Let's start with writing the ansible script(playbook) named task1.yml for launching a website inside docker.
Step 1: Select the hosts
As of now, we are selecting all hosts but it’s a good practice to group the hosts according to the required and run the scripts on them.
Step 2: Configure yum
Our requirement is to launch the website inside DOCKER, so we required to install docker first. For this, we need to configure the yum command for the docker package.
The docker software is present at https://download.docker.com/linux/centos/7/x86_64/stable/ location and as we don’t have a signature for the package, we will set the gpgcheck to no.
Step 3: Install Docker
There are many ways to install docker. First is we can install it by using the regular approach that is
yum install docker-ce . But, Docker is not meant for RedHat version8, so we need to pass the
--nobest option to the command to skip the broken packages. But, in the ansible, there is no way to pass the
--nobest option. So, we will give the exact package name to install the docker.
The name of the docker package is docker-ce-18.06.3.ce-3.el7.x86_64
Step 4: Install Python
As Ansible is created on the top of the python, we have to use python libraries. For this, we required to install python in the managed nodes.
Step 5: Install the Docker SDK for python
The installer for the python packages is a pip. So, we required to use the pip module in the ansible.
Step 6: Start and enable docker service
Ansible provides us an service module to manage the services. The name of the service is docker and the state should be started. As we required to enable this service forever, we will set enable to yes.
Step 7: Pull httpd docker image
As we required to launch the website, we required the webserver. Here, we will use the preconfigured Apache webserver image named httpd.
Step 8: Copy website to the host(managed node)
The website code is present at a ~/AnsibleAutomation-RH294/Task1/webpage/index.html. Copy this code at /root/webpage in the managed node.
Step 9: Run docker container
We will launch a container from httpd image and expose the port 80 of the container which is the port on which the webserver is running. To access the website running inside the docker container from the outside world, we need to map the container port. Let's map the container port 80 to the host port 8080. So, the requests that are coming on the port 8080 will be redirected to the port 80 of the container.
The default location for the website code for httpd is
/usr/local/apache2/htdocs/ Mount the
/root/website to this location using the volume.
Finally, the script(playbook) for launching the website inside the docker is ready.
Running the ansible-playbook
Let’s run the ansible-playbook.
$ ansible-playbook task1.yml
After the playbook successfully runs, the managed node will be configured with the Docker and the website inside the docker container has been launched successfully.
Accessing the website
Go to the IP of the managed node and as we mapped the service to the port 8080 go to that port.
We can see that the site is accessible. So, we had successfully launched the website inside the docker container using the ansible-playbook.