5.ansible Lab
5.ansible Lab
5.ansible Lab
com
docker pull sreeharshav/ubuntu16:withssh2.0_V2
docker pull sreeharshav/ubuntu16:ansible2.75_V2
2. Create a new network as below:
docker network create --subnet 172.20.0.0/16 --gateway 172.20.0.254 ansible_nw
3.Create three machines as below using the above network.
docker network create --subnet 172.20.0.0/16 --gateway 172.20.0.254 ansible_nw
docker run --rm -dit --name controller --hostname controller --network ansible_nw
sreeharshav/ubuntu16:ansible2.75_V2
docker run --rm -dit --name web1 --hostname web1 -p 8000:80 --network ansible_nw
sreeharshav/ubuntu16:withssh2.0_V2
docker run --rm -dit --name web2 --hostname web2 -p 8001:80 --network ansible_nw
sreeharshav/ubuntu16:withssh2.0_V2
docker run --rm -dit --name web3 --hostname web3 -p 8002:80 --network ansible_nw
sreeharshav/ubuntu16:withssh2.0_V2
docker run --rm -dit --name app1 --hostname app1 -p 9000:80 --network ansible_nw
sreeharshav/ubuntu16:withssh2.0_V2
docker run --rm -dit --name app2 --hostname app2 -p 9001:80 --network ansible_nw
sreeharshav/ubuntu16:withssh2.0_V2
docker run --rm -dit --name app3 --hostname app3 -p 9002:80 --network ansible_nw
sreeharshav/ubuntu16:withssh2.0_V2
docker run --rm -dit --name db1 --hostname db1 -p 9003:80 --network ansible_nw
sreeharshav/ubuntu16:withssh2.0_V2
docker run --rm -dit --name db2 --hostname db2 -p 9004:80 --network ansible_nw
sreeharshav/ubuntu16:withssh2.0_V2
docker run --rm -dit --name db3 --hostname db3 -p 9005:80 --network ansible_nw
sreeharshav/ubuntu16:withssh2.0_V2
docker run --rm -dit --name db4 --hostname db4 -p 9006:80 --network ansible_nw
sreeharshav/ubuntu16:withssh2.0_V2
CENTOS CONTAINERS:
docker run --rm -dit --name cent1 --hostname cent1 -p 9000:80 --network ansible_nw
sreeharshav/centos7:withssh2.0_V1
docker run --rm -dit --name cent2 --hostname cent2 -p 9001:80 --network ansible_nw
sreeharshav/centos7:withssh2.0_V1
docker run --rm -dit --name cent3 --hostname cent3 -p 9002:80 --network ansible_nw
sreeharshav/centos7:withssh2.0_V1
4. Go in to the machine1
docker exec -it machine1 /bin/bash
5.ping machine 2,3,4
6.Install Anible using below command
apt-get install ansible -y
7. Run ansible --version and you should get response as ansible 2.7.5
8.Create a file called hosts using following command.
nano hosts
9. Add the following in the hosts file and save it.
[local]
localhost
[servers]
machine2
machine3
machine4
10. Run the following commands:
12. Once the change is done, got back to host and stop machine 2,3,4 and since we
used --rm the containers will be deleted. Create them again as below.
docker run --rm -dit --name machine2 --hostname machine2 --network ansible_nw
sreeharshav/ubuntu16:withssh2.0
docker run --rm -dit --name machine3 --hostname machine3 --network ansible_nw
sreeharshav/ubuntu16:withssh2.0
docker run --rm -dit --name machine4 --hostname machine3 --network ansible_nw
sreeharshav/ubuntu16:withssh2.0
13. Go to the machine 1 using the command docker exec -it machine1 /bin/bash
14. Run the command "ansible -i hosts servers -m ping" and since the key is
disabled the response should be as below.
root@machine1:/# ansible -i hosts servers -m ping
machine4 | SUCCESS => {
"changed": false,
"ping": "pong"
}
machine3 | SUCCESS => {
"changed": false,
"ping": "pong"
}
machine2 | SUCCESS => {
"changed": false,
"ping": "pong"
}
----------------------------------Completed----------------------------------
-------------------------------Ansible-AdHoc-Commands-----------
ansible -i hosts all -m ping
ansible -i hosts all -m ping -f 1
ansible -i hosts all -m command -a hostname
ansible -i hosts all -m command -a uptime
ansible -i hosts all -m command -a free
ansible -i hosts all -m command -a df -h
ansible app -s -a "yum install -y docker-engine"
ansible -i hosts all -a "cat /etc/passwd"
ansible -i hosts servers:app -a free --limit machine2
ansible -i hosts all -a uptime --limit servers
ansible -i hosts prod -a free (clildren as app & servers)
ansible -i hosts 'servers:!app' -a free
ansible -i hosts 'sertp' -a free
ansible -i hosts servers[0] -a uptime
ansible -i hosts servers[0:2] -a uptime
ansible -i hosts servers[2:] -a uptime
ansible -i hosts servers -m user -a "user=testuser
password=$1$pGP3ICXv$6PEDQI/N1RgLzVr/l00YY0"
ansible app -s -m group -a "name=admin state=present"
ansible app -s -m user -a "name=devops group=admin createhome=yes"
ansible app -m copy -a "src=/vagrant/test.txt dest=/tmp/test.txt"
----------------------------------Completed----------------------------------
-------------------------------Ansible-Playbook-Commands---------------------
ansible-playbook playbook.yml --list-hosts
ansible-playbook playbook.yml --syntax-check
ansible-playbook playbook.yml --syntax-check --verbose
HOST-PATTERN:
https://docs.ansible.com/ansible/latest/user_guide/intro_patterns.html
https://www.middlewareinventory.com/blog/ansible-ad-hoc-command-examples-cheat-
sheet/
https://docs.ansible.com/ansible/latest/user_guide/intro_patterns.html
https://docs.ansible.com/ansible/latest/user_guide/intro_adhoc.html
-----------------------------------------------CACHING
FACTS-----------------------------------------------
https://andreas.scherbaum.la/blog/archives/1019-Local-caching-of-Ansible-Facts.html
As mentioned before, this example is using the "jsonfile" cache. The following
settings need to be added to "ansible.cfg" in the Playbook directory:
gathering = smart
gather_subset = all
fact_caching = jsonfile
fact_caching_connection = facts.json
fact_caching_timeout = 86400
Ansible has different ways when to gather facts: "implicit" is the default, and
will re-gather facts at the beginning of every Playbook run. This setting will
ignore any facts cache. The inverse is "explicit", and will not gather facts until
and unless explicitely requested by a "setup" task. The "smart" option will only
gather facts if no cached facts are available.
The "gather_subset = all" is Ansible default, and will gather all destination host
details. This can be limited to a certain subset, if not all the information are
required.