This section for those who already have idea about Ansible .
Note: playbooks are written in YAML language that is Yet Another markup Language
Here is sample of Ansible playbook
root@ashulinux:/etc/ansible# cat test1.yml
---
- hosts: all
user: root
vars:
motd_warning: 'knowledge is power !!i n\'
tasks:
- name: sample motd
copy:
dest: /etc/motd
content: "{{ motd_warning }}"
Important : I just want to start about Ansible Playbook
like in :
Puppet : Module
Chef : CookBook
Same ansible has ===>>
Ansible : Playbook
Ansible Playbook Divided into 3 section :
1. Target section :
Note: This is similar to nodes.pp in puppet and run-list in chef
target section generally means on which target system this playbook is going to execute all the stuff.
2. Variable Section
This is not manodatory but which you can use in playbook as general placeholder
3. Task Section
List of modules in order to run
Note: playbooks are written in YAML language that is Yet Another markup Language
Here is sample of Ansible playbook
root@ashulinux:/etc/ansible# cat test1.yml
---
- hosts: all
user: root
vars:
motd_warning: 'knowledge is power !!i n\'
tasks:
- name: sample motd
copy:
dest: /etc/motd
content: "{{ motd_warning }}"
Note : To Run ansible playbook
root@ashulinux:/etc/ansible# ansible-playbook test1.yml
____________
< PLAY [all] >
------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
_________________
< GATHERING FACTS >
-----------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
ok: [192.168.0.46]
ok: [192.168.0.41]
ok: [192.168.0.80]
ok: [192.168.0.167]
___________________
< TASK: sample motd >
-------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
ok: [192.168.0.80]
ok: [192.168.0.167]
ok: [192.168.0.41]
ok: [192.168.0.46]
____________
< PLAY RECAP >
------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
192.168.0.167 : ok=2 changed=0 unreachable=0 failed=0
192.168.0.41 : ok=2 changed=0 unreachable=0 failed=0
192.168.0.46 : ok=2 changed=0 unreachable=0 failed=0
192.168.0.80 : ok=2 changed=0 unreachable=0 failed=0
Run Ansible Playbook in verbose mode
root@ashulinux:/etc/ansible# ansible-playbook test1.yml -v
___________
< PLAY [lw] >
-----------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
_________________
< GATHERING FACTS >
-----------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
ok: [192.168.0.80]
ok: [192.168.0.167]
___________________
< TASK: sample motd >
-------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
ok: [192.168.0.80] => {"changed": false, "dest": "/etc/motd", "gid": 0, "group": "root", "md5sum": "21984c82bbf4ebb7e6dad67d0fc652f7", "mode": "0644", "owner": "root", "path": "/etc/motd", "secontext": "system_u:object_r:etc_runtime_t:s0", "size": 21, "state": "file", "uid": 0}
ok: [192.168.0.167] => {"changed": false, "dest": "/etc/motd", "gid": 0, "group": "root", "md5sum": "21984c82bbf4ebb7e6dad67d0fc652f7", "mode": "0644", "owner": "root", "path": "/etc/motd", "secontext": "system_u:object_r:etc_runtime_t:s0", "size": 21, "state": "file", "uid": 0}
____________
< PLAY RECAP >
------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
192.168.0.167 : ok=2 changed=0 unreachable=0 failed=0
192.168.0.80 : ok=2 changed=0 unreachable=0 failed=0
root@ashulinux:/etc/ansible#
root@ashulinux:/etc/ansible# ansible-playbook test1.yml
____________
< PLAY [all] >
------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
_________________
< GATHERING FACTS >
-----------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
ok: [192.168.0.46]
ok: [192.168.0.41]
ok: [192.168.0.80]
ok: [192.168.0.167]
___________________
< TASK: sample motd >
-------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
ok: [192.168.0.80]
ok: [192.168.0.167]
ok: [192.168.0.41]
ok: [192.168.0.46]
____________
< PLAY RECAP >
------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
192.168.0.167 : ok=2 changed=0 unreachable=0 failed=0
192.168.0.41 : ok=2 changed=0 unreachable=0 failed=0
192.168.0.46 : ok=2 changed=0 unreachable=0 failed=0
192.168.0.80 : ok=2 changed=0 unreachable=0 failed=0
Run Ansible Playbook in verbose mode
root@ashulinux:/etc/ansible# ansible-playbook test1.yml -v
___________
< PLAY [lw] >
-----------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
_________________
< GATHERING FACTS >
-----------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
ok: [192.168.0.80]
ok: [192.168.0.167]
___________________
< TASK: sample motd >
-------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
ok: [192.168.0.80] => {"changed": false, "dest": "/etc/motd", "gid": 0, "group": "root", "md5sum": "21984c82bbf4ebb7e6dad67d0fc652f7", "mode": "0644", "owner": "root", "path": "/etc/motd", "secontext": "system_u:object_r:etc_runtime_t:s0", "size": 21, "state": "file", "uid": 0}
ok: [192.168.0.167] => {"changed": false, "dest": "/etc/motd", "gid": 0, "group": "root", "md5sum": "21984c82bbf4ebb7e6dad67d0fc652f7", "mode": "0644", "owner": "root", "path": "/etc/motd", "secontext": "system_u:object_r:etc_runtime_t:s0", "size": 21, "state": "file", "uid": 0}
____________
< PLAY RECAP >
------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
192.168.0.167 : ok=2 changed=0 unreachable=0 failed=0
192.168.0.80 : ok=2 changed=0 unreachable=0 failed=0
root@ashulinux:/etc/ansible#
Run Ansible playbook in step mode
root@ashulinux:/etc/ansible# ansible-playbook test1.yml --step
Note: ansible playbook for apache web server configuration management
root@ashulinux:/etc/ansible# cat install_apache.yml
# installing apache
---
- hosts: adhoc
user: root
tasks:
- name: installing package
action: yum name=httpd state=installed
- name: start service
service:
name: httpd
state: restarted
- name: check yum repo
copy: src=/info.txt dest=/var/www/html/index.html
Important:
Here hosts == pointing to all hosts inside /etc/ansible/hosts
Note: One more sample file:
- name: install and configure webservers
hosts: apache
remote_user: ec2-user
sudo: yes
roles:
- apache
-------------------------------------------
-------------------------------------------
If we want to log into our host machines using a different username and with sudo privileges, we need to use the “remote_user” and “sudo: yes” parameter in our site.yml file. There can be additional parameters too, but they’re not needed right now. Here, we have also defined roles granted to hosts in the [webservers] group.
------------------------------------------
------------------------------------------
Method for error free playbook methods according to YAML syntax:
i) creating apache web server yml file
[root@be04c6686478 ansible]# cat apache_final.yml
---
- hosts: webserver
vars:
http_port: 80
max_client: 300
remote_user: root
tasks:
- name: installing httpd and check
yum:
name: httpd
state: latest
- name: start the apache service
service:
name: httpd
state: started
enabled: yes
ii) Run yml file
[root@be04c6686478 ansible]# ansible-playbook apache_final.yml
PLAY [webserver] **************************************************************
GATHERING FACTS ***************************************************************
ok: [192.168.0.183]
TASK: [installing httpd and check] *******************************************
ok: [192.168.0.183]
TASK: [start the apache service] **********************************************
changed: [192.168.0.183]
PLAY RECAP ********************************************************************
192.168.0.183 : ok=3 changed=1 unreachable=0 failed=0
iii) managing containers using ansible
Here hosts == pointing to all hosts inside /etc/ansible/hosts
Note: One more sample file:
- name: install and configure webservers
hosts: apache
remote_user: ec2-user
sudo: yes
roles:
- apache
-------------------------------------------
-------------------------------------------
If we want to log into our host machines using a different username and with sudo privileges, we need to use the “remote_user” and “sudo: yes” parameter in our site.yml file. There can be additional parameters too, but they’re not needed right now. Here, we have also defined roles granted to hosts in the [webservers] group.
------------------------------------------
------------------------------------------
Method for error free playbook methods according to YAML syntax:
i) creating apache web server yml file
[root@be04c6686478 ansible]# cat apache_final.yml
---
- hosts: webserver
vars:
http_port: 80
max_client: 300
remote_user: root
tasks:
- name: installing httpd and check
yum:
name: httpd
state: latest
- name: start the apache service
service:
name: httpd
state: started
enabled: yes
ii) Run yml file
[root@be04c6686478 ansible]# ansible-playbook apache_final.yml
PLAY [webserver] **************************************************************
GATHERING FACTS ***************************************************************
ok: [192.168.0.183]
TASK: [installing httpd and check] *******************************************
ok: [192.168.0.183]
TASK: [start the apache service] **********************************************
changed: [192.168.0.183]
PLAY RECAP ********************************************************************
192.168.0.183 : ok=3 changed=1 unreachable=0 failed=0
iii) managing containers using ansible
- launching containers
---
- hosts: test
remote_user: root
tasks:
- name: creating container
docker_container:
name: ashutoshhtest1
image: centos6shh
detach: true
2. Launching multiple containers
---
- hosts: test
remote_user: root
tasks:
- name: creating container
docker_container:
name: "container{{ item }}"
image: centos6shh
detach: true
with_sequence: count=6
3. removing containers
---
- hosts: test
remote_user: root
tasks:
- name: removing container
docker_container:
name: ashutoshh
state: absent
4. starting containers
---
- hosts: test
remote_user: root
tasks:
- name: starting container
docker_container:
name: ashutoshhne
image: centos6shh
state: started
5. stoping containers
---
- hosts: test
remote_user: root
tasks:
- name: stop a running container
docker_container:
name: ashutoshhne
state: stopped
6. container with volume
---
- hosts: test
remote_user: root
tasks:
- name: creating container with volume
docker_container:
name: ashutoshhtest2
image: centos6shh
command: ping localhost
detach: true
volumes:
- /data
This is an Excellent Information on Devops Online Training
ReplyDeleteCheck it through Devops Online Training Bangalore for more info.
ReplyDeleteThis information really helped me a lot. It was very informative.
ReplyDeleteDevops Services