Docker setup completely automated using ansible..!
To configure and start docker services and setup webserver on it using ansible playbook..!
Task Description..
Write an 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 httpd container and expose it to the public
- Copy the html code in /var/www/html directory and start the web server
Before getting into the task let's learn something..
What is Ansible..
Ansible is a Devops tool that provides simple but powerful automation for cross-platform computer support. It is primarily intended for IT professionals, who use it for application deployment, updates on workstations and servers, cloud provisioning, configuration management, intra-service orchestration, and nearly anything a systems administrator does on a weekly or daily basis. Ansible doesn't depend on agent software and has no additional security infrastructure, so it's easy to deploy.
How to setup up ansible..??
As Ansible is written in python we can use pip installer to install ansible..i.e: *pip install ansible*
To confirm the installation we can use *ansible --version*
Basic terms in ansible..
Controller node: Any machine with Ansible installed. You can run Ansible commands and playbooks by invoking the ansible or ansible-playbook command from any control node. You can use any computer that has a Python installation as a control node - laptops, shared desktops, and servers can all run Ansible. However, you cannot use a Windows machine as a control node. You can have multiple control nodes.
Managed Node: The network devices (and/or servers) you manage with Ansible. Managed nodes are also sometimes called “hosts”. Ansible is not installed on managed nodes.
Inventory: A list of managed nodes. An inventory file is also sometimes called a “hostfile”. Your inventory can specify information like IP address for each managed node. An inventory can also organize managed nodes, creating and nesting groups for easier scaling. To learn more about inventory, see the Working with Inventory section.
Playbooks: Ordered lists of tasks, saved so you can run those tasks in that order repeatedly. Playbooks can include variables as well as tasks. Playbooks are written in YAML and are easy to read, write, share and understand. To learn more about playbooks, see Intro to playbooks.
Create an inventory file and ansible configuration file and update respective info.
For this practical I setup my inventory and ansible config file for the instance ruuning on aws i.e for key-based authentication as follows.
To check the working inventory : *ansible all --list-hosts*
Playbook for entire docker setup..
Here is the playbook named ansi-task1 ready to configure all the steps mentioned in our task.
- name: Webserver On Docker hosts: all tasks: - name: Creating yum repo for docker yum_repository: name: dockerrepo description: Repository for docker.. baseurl: https://download.docker.com/linux/centos/7/x86_64/stable gpgcheck: no - name: Installing docker software.. package: name: docker-ce-18.09.1-3.el7.x86_64 skip_broken: yes state: present - name: Start Docker Services... service: name: "docker" state: started enabled: yes - name: Installing Python3 in instance.. command: yum install python36 -y - name: Installing Docker SDK for Python command: pip3 install docker - name: Creating a directory to store webpages.. file: path: /websites state: directory - name: Copying webpages copy: src: "docker.html" dest: "/websites" - name: Creating httpd docker container for webserver... docker_container: name: DWebserver image: vimal13/apache-webserver-php state: started exposed_ports: - "80" ports: - "8888:80" volumes: - "/websites:/var/www/html/"
In the above playbook we used different modules for each task which are the actual intelligence behind the ansible to make our things easier..!
Now,before running this playbook let us check our managed node has docker setup or not..
Now let's run the playbook...
cmd: ansible-playbook ansi-task1.yml
The below image shows the successful execution of all tasks ..in the controller node!
Now,let's see what happened in managed node..!
If we observe the above results...we configured the yum repo for docker and then we installed the docker-ce software and finally we pulled the image that provides webserver and created a container..and everything is done.
Now let's access our website...using public ip of our instance along with assigned port number.
That's all for the task..with just one simple code the entire configuration has been done...proving the power of ansible..!
Task completed under the mentorship of Mr.Vimal Daga...
Thanks for reading and keep connected on linkedin for more..
Software Engineer@Principal Global Services | 1xAWS Certified | 1xMicrosoft Certified | 1xOracle Certified | AWS | Azure | Docker | K8s | Ansible
4 年Well done!!!
DevOps Engineer | GCP-ACE | AWS SAA | Terraform Certified
4 年Great one bro!!! ?
DevOps Engineer at DataGrokr | ?? 1X AWS, 2X Google Cloud, and 3X Azure Certified | Cloud Architect | Infrastructure Automation Enthusiast | Docker | Kubernetes | Terraform | Ansible | Jenkins | Gitlab
4 年Nice work ???
DevOps Engineer | MLOps | DataOps | Founding Engineer
4 年Keep Going ! Well Done