For configure web-server in a Docker container using Ansible, first we have to know what is ansible?
Ansible:
Ansible is an open source IT configuration management, deployment, and orchestration tool. It is unique from other management tools in many respects, aiming to provide improvements in productivity to a wide variety of automation challenges as a more productive drop-in replacement for many core capabilities in other automation solutions.
Ansible is:
Designed to be minimal in nature.
Consistent, secure, and highly reliable.
Low learning curve for administrators, developers, and IT managers.
Attempts to make it powerful for experienced users while staying available to users at all ability levels.
Ansible by default manages remote machines over SSH (Linux and UNIX) or WinRM (Windows), using the remote management frameworks that already exist natively on those platforms. Ansible builds on this by not requiring dedicated users or credentials — it respects the credentials that the user supplies when running Ansible and while Ansible does not require administrator access, leveraging sudo, su, and other privilege escalation methods on request when necessary, this method allows Ansible to be more secure.
Ansible Playbooks:
Ansible performs automation and orchestration of IT environments via Playbooks. Playbooks are a YAML definition of automation tasks that describe how a particular piece of automation should be done. Playbooks are meant to be simple, human-readable, and self-documenting. They are also idempotent, meaning that a playbook can be run on a system at any time without having a negative effect upon it. If a playbook is run on a system that’s already properly configured and in its desired state, then that system should still be properly configured after a playbook runs.
Ansible Inventory:
Ansible Inventory is a file that contains information about the devices that Ansible can connect to. IP addresses or names may be used to specify devices in the Inventory register. Devices may either be listed individually or in groups.
So, the steps to configure web-server(httpd) in a Docker container using Ansible are:
Configure Docker in the managed node.
Start and enable Docker services.
Pull the httpd server image from the Docker Hub.
Run the httpd container and expose it to the public.
Copy the html code in target
First we install Ansible in the Master node:
pip3 install ansible
Now in the Ansible we need to configure the inventory:
the inventory where we can provide the IP of target node and give the path of inventory to the ansible configuration file, the ansible configuration file is not present by default so we have to create it:
vim /etc/ansible/ansible.conf
Now we check the connectivity by using ping command:
In Ansible -Playbook we can list the number of tasks:
Configure docker and start services:
To create docker repository:
- yum_repository:
name: "docker-ce"
description: "docker"
baseurl: "https://https://download.docker.com/linux/centos/7/x86_64/stable/"
gpgcheck=0
To install docker:
- package:
name: "docker-ce-18.09.1-3.el7"
state: present
- service:
name: "docker"
state: started
enabled: yes
- pip:
name: "docker"
Pulling httpd server image from dockerhub
- docker_image:
name: httpd
source: pull
Now have to copy html code from Master-node to Target-node:
- copy:
src: "/root/Ansible/home.html"
dest: "/var/www/html/"
Now create and launch the docker container and expose the ports:
- docker_container:
name: webserver
image: httpd
state: started
exposed_ports: 80
volumes : "/var/www/html/:/usr/local/apache2/htdocs/"
published_ports: 8080:80
we can add the port number to the firewalld so it can allow access to the host on that port number:
- firewalld:
port: 9459/tcp
permanent: yes
state: enabled
zone: public
our ansible code is complete and it's time to run the playbook:
ansible-playbook <playbook-name>.yml
As there are no errors, our code has been executed and we can check that on managed node.