ARTH - Task 14.2 ??????
Gaurav Rathi
Cloudops Engineer | RHCSA | RHCE | RHCoE(CEPH) ,(OpenShift) | Docker | Ansible | Kubernetes | OpenStack | AWS | MLOps | Jenkins |
*Task Description*??
?? *14.2* Further in ARTH - Task 10 has to create an Ansible playbook that will retrieve new container IP and update the inventory. So that further Configuration of the Webserver could be done inside that Container.
This is a very big task in this task firstly I would install Ansible Docker in my system and then Pull the image of docker from the Dockerhub. after then we have to set up our ansible to perform the task like make an ansible_conf file in which we tell our Inventory file In this tasks we use Inventory two time Because in this task we run two diff Playbook on Two Diff OS The First playbook is run on our localhost so there is no need of Inventory file for that we can write this on our playbook that we have to run this on Localhost.
And the second Playbook is run after when our first playbook is run successfully without any error and our first playbook creates an inventory file for our second Playbook or it can also create our second os on which we have to run our second playbook. after running the second playbook we get our requirement for this task as we need.
So let's see some main terminology of this two playbook and also what we write in these two playbook
Let's talk about our first playbook This is a playbook that we have to run over the Localhosts because your requirement is to run the docker container by running this playbook so on localhost we have docker setup and also the same image that we have to use our doing our practice and in these, we only pass one variable that is our container name we need this as a variable because it is necessary to have diff name of the container to launch it must be that same name of container neither be running nor in stop state so we give as a variable name. After that, we use the Ansible module name is docker_container this is used to create a docker container via ansible so we have to provide it image name that we use to create a container and the name of the container that we want to five and also port no on which we can expose it service and we create a specific image in which we have to setup
The SSh server with the permanent start of there service means when we launch a container with that image that must have a running service of SSH server it can take SSh after creating as we know Ansible use only ssh method to connect to os and then perform its works so it is must that Launched container must have an SSH server running and it can easily connect to us. and after that in the playbook, we have to get the info of launched container like IP address and all so for that we also have an ansible module that is docker_conatiner_info by which we can take all the info launched docker and after that, we have got the IP address of that Container so that by taking this IP we can create a new inventory file in which we have IP of our Container with there user name and password we can use ansible module for creating an inventory file that can fetch the IP with the help of docker_container_info module now we use blocking file module to create a file in which we have a block that has IP address name such connection type and password.
So as our Second Inventory is created by running 1st playbook and our os also launched so now we have to play our second playbook that need an inventory that we have and an os that also we have and this playbook will perform a task it will make it as a web-server and start it service
After doing all this we get that our service is started inside the docker that is created by the ansible and also configured by the ansible this is all about tasks so let's start our tasks
1.Our First Playbook file that creates Docker container and takes Docker container info and also creates an inventory for the second Playbook
cat task14.yml - hosts: localhost vars: container_name: centos1 tasks: - docker_container: name: "{{ container_name }}" image: mycentos:ssh1 state: started interactive: yes detach: yes tty: yes ports: - "9197:80" restart: yes - name: get docker info docker_container_info: name: "{{ container_name }}" register: x - debug: var: x.container.NetworkSettings.IPAddress - blockinfile: path: /root/ansible/ip.txt block: | [docker] {{ x['container']['NetworkSettings']['IPAddress'] }} a nsible_ssh_user=root ansible_ssh_pass=redhat ansible_connection=ssh
this is our playbook first
NOw let's see our Inventory file for the second os and also that is our second os is running or not
Our inventory file is
cat ip.txt # BEGIN ANSIBLE MANAGED BLOCK [docker] 172.17.0.2 ansible_ssh_user=root ansible_ssh_pass=redhat ansible_connection=ssh # END ANSIBLE MANAGED BLOCK
This is our inventory file that is created by Playbook 1
Our docker container running proof
it means our First playbook running successfully without any error and so all its tasks
NOw we can see our ansible config file that has inventory for seconf os
cat ansible.cfg [defaults] inventory = /root/ansible/ip.txt host_key_checking=false remote_user=root roles_path=/root/ansible/role/ [privilege_escalation] become=true become_method=sudo become_user=root become_ask_pass=false
After this let's see our and playbook that will configure httpd on our second os
cat web.yml - hosts: docker tasks: - package: name: httpd state: present - copy: src: /root/index.html dest: /var/www/html/ - name: strt service command: /usr/sbin/httpd
When we run this it will configure our Docker as an Httpd server or we can say the website
Let's see is it running or not in two ways checking with netstat -help and via curl
Cirl--
netstat---
It means our setup is working fine We can successfully create a docker container via ansible and also configured it as an HTTPd webserver
OP of successfully running of Playbook 1 is
O/p of ip.txt and
O/p of playbook2
Confirmation of docker launched and configured successfully is here
Thanks for reading
HAve A Nice Day