-
-
Save devops-school/ac5f0830149ff59899d1773370102528 to your computer and use it in GitHub Desktop.
program 1:register:
#register-test.yml
- name: test for playbook 1
hosts: common
vars:
myname: "Ashutosh-playbook"
mycomp: "hpe-playbook"
tasks:- name: assign group to user
shell: " find *.html"
args:
chdir: /var/www/html
register: find_output - debug:
var: find_output
...
- name: assign group to user
program 2:
#anotherYAML.yml
- hosts: common
vars:
myname: "Ashutosh-playbook"
mycomp: "hpe-playbook"
tasks:- include: callYAML.yml
...
- include: callYAML.yml
other YAML ===== shiuld have only modeule=====
#- name: calling msg
- debug:
msg: System Ashutosh has uuid {{ ansible_product_uuid }} of company hpe
program3:
======= template and handler================
#handler-new.yml
- name: test for playbook 1
hosts: common
vars:
yourname: "Ashutosh-playbook"
companyname: "hpe-playbook"
age: "11"
emailaddress: "no idea"
tasks:- name: assign group to user
yum:
name: httpd
state: present - name: write the apache config file
template:
src: /etc/ansible/template-example/templates/index.j2
dest: /var/www/html/index.html
notify:- restart apache
handlers: - name: restart apache
service:
name: httpd
state: restarted
...
- restart apache
- name: assign group to user
===============================loop=======================
#loop-test.yml
- name: test for register for unix command playbook 1
hosts: common
tasks:- name: assign group to userA
yum:
name: "{{item}}"
state: present
with_items:- git
- wget
- vim
...
- name: assign group to userA
program1**
- name: Display group variablea and undefined variable
hosts: db
tasks:- debug:
var: port - debug:
var: ansible_os_family - debug:
msg: Hello !!!!!!!!!!!!!!{{mn}}!!!!!!!!!!!!!!!!
- debug:
- debug:
msg: You are !!!!!!!{{undef}}!!!!!!!!!
-
debug:
var: ansible_os_family,port,mn -
name: Display host variable
hosts: web
vars:
http_port: 80
tasks:- debug:
var: ip - debug:
var: ansible_os_family - debug: var=http_port
- debug:
-
name: Display group variable and message
hosts: web
vars:
http_port: 80
tasks:- debug:
msg: "hello :{{ansible_os_family}}, it's me : {{mn}}.................." - debug:
var: ansible_os_family - debug: var=http_port
- debug:
-
name: Display group variable ovrriding host variable and message
hosts: web
vars:
http_port: 80
tasks:- debug:
msg: "hello :{{ansible_os_family}}, my ip : {{ip}}, my name : {{mn}}, my age : {{ma}} " - debug:
var: ansible_os_family - debug: var=http_port
- debug:
-
name: Display playbook variable overriding inventory variables
hosts: web
vars:
http_port: 80
ip: 111
tasks:- debug:
msg: "hello :{{ansible_os_family}}, my ip : {{ip}}, my name : {{mn}}, my age : {{ma}} ####### my company :{{mc}}"
...
- debug:
********program 2 *********
-
name: Display file variable overriding playbook variable
hosts: web
vars_files:- "vars.yaml"
vars:
http_port: 80
ma: 08
ansible_os_family: Default
tasks: - debug:
msg: "hello :{{ansible_os_family}}, my ip : {{ip}}, my name : {{mn}}, my age : {{ma}} ####### my company :{{mc}}"
- "vars.yaml"
-
name: Display of Facts
hosts: web
vars_files:- "vars.yaml"
vars:
http_port: 80
ma: 08
ansible_os_family: Default
tasks: - debug:
msg: "hello :{{ansible_os_family}}, my ip : {{ip}}, my name : {{mn}}, my age : {{ma}} ####### my company :{{mc}}" - debug:
msg: Hai hello !!!!!!{{ansible_python}}!!!!!!!!!
- "vars.yaml"
-
name: Display of Facts
hosts: web
vars_files:- "vars.yaml"
vars:
http_port: 80
ma: 08
ansible_os_family: Default
vars_prompt: - name: myname
prompt: "Whats your name :"
private: no - name: ma
prompt: "Whats your age :"
tasks: - debug: msg="hello {{myname,ma}} and my age {{ma}}"
- "vars.yaml"
-
name: Display file variable overriding playbook variable
hosts: web
vars_files:- "vars.yaml"
vars:
http_port: 80
ma: 08
ansible_os_family: Default
tasks: - debug:
msg: "hello :{{ansible_os_family}}, my ip : {{ip}}, my name : {{mn}}, my age : {{ma}} ####### my company :{{mc}}"
- "vars.yaml"
-
name: Display of Facts
hosts: web
vars:
ma: 08
vars_prompt:- name: myname
prompt: "Whats your name 2:"
private: no - name: ma
prompt: "Whats your age 2:"
tasks: - debug: msg="hello {{myname,ma}} and my age {{ma}}"
...
- name: myname
program 3*****
-
name: Display file variable overriding playbook variable
hosts: common
vars_files:- "vars.yaml"
vars:
http_port: 80
ma: 08
ansible_os_family: Default
vars_prompt: - name: myname
prompt: "Whats your name :"
private: no - name: ma
prompt: "Whats your age :"
tasks:
- name: invars list
include_vars: invars.yaml - debug:
msg: "hello :{{ansible_os_family}}, my ip : {{ip}}, my name : {{mn}}, my age : {{ma}} ####### my company :{{mc}}"
- "vars.yaml"
-
name: Display file variable overriding playbook variable
hosts: all
vars_prompt:- name: myname
prompt: "Whats your name :"
private: no - name: ma
prompt: "Whats your age :"
tasks: - name: invars list
include_vars: invars.yaml - debug:
msg: "hello :{{ansible_os_family}}, my ip : {{ip}}, my name : {{mn}}, my age : {{ma}} ####### my company :{{mc}}"
- name: myname
-
name: Display file variable overriding playbook variable
hosts: common
vars_files:- "group_vars/db"
vars:
mn: bhanu-vars
tasks: - name: invars list
include_vars: group_vars/web - debug:
msg: "hello :{{ansible_os_family}}, ******** my name : {{mn}} ************"
- "group_vars/db"
-
name: Display file variable overriding playbook variable
hosts: all
tasks:- name: invars list
include_vars: host_vars/15.213.56.126 - debug:
msg: "hello :{{ansible_os_family}}, ********* my name : {{mn}} ************"
- name: invars list
-
name: Display file variable overriding playbook variable
hosts: all
vars_files:- "group_vars/db"
tasks: - name: invars list
include_vars: host_vars/15.213.56.126 - debug:
msg: "hello :{{ansible_os_family}}, ********* my name : {{mn}} ************"
- "group_vars/db"
-
name: Display file variable overriding playbook variable
hosts: all
vars_files:- "group_vars/db"
tasks: - name: invars list
include_vars: host_vars/15.213.56.126 - debug:
msg: "hello :{{ansible_os_family}}, ********* my age in db : {{ma}} ************"
...
- "group_vars/db"
**********program 4 *************
-
name: Ansible register variable basic example
hosts: web
tasks:- name: shell
shell: "find *.txt"
args:
chdir: /home/bhanu/
register: find_output - debug:
var: find_output
- name: shell
-
name: Ansible register variable basic example
hosts: web
tasks:- debug:
msg: "hello"
- debug:
-
name: Include a play after another play
import_playbook: varSample4.yaml -
hosts: web
tasks:- debug:
msg: play1
- debug:
-
name: Include a play after another play
include: varSample4.yaml -
name: Ansible register variable basic example
hosts: web
tasks:- name: shell
shell: "find *.txt"
args:
chdir: /home/bhanu/
register: find_output - debug:
var: find_output
- name: shell
-
hosts: web
tasks:- debug:
msg: task1
- debug:
-
name: Include task list in play
include: varSample4.yaml -
hosts: web
tasks:- debug:
msg: task10
...
- debug:
program 5***
- hosts: web
vars:
http_port: 80
max_clients: 200
remote_user: root
tasks:- name: ensure apache is at the latest version
yum:
name: httpd
state: latest - name: write the apache config file
template:
src: /srv/httpd.j2
dest: /etc/httpd.conf
notify:- restart apache
- name: ensure apache is running
service:
name: httpd
state: started
handlers:- name: restart apache
service:
name: httpd
state: restarted
...
- name: restart apache
- name: ensure apache is at the latest version
inventory file*
16.167.12.209
[web]
15.213.56.126 ip=126 #ansible_become=yes ansible_user=root ansible_password=hwroot ansible_become_user=root
[web:vars]
mn=bhanu
mc=HPE
ma=28
ip=123
[db]
15.213.56.127
[db:vars]
port=123
mn=prakash
mc=HPI
[common:children]
db
web
[common:vars]
ip=999
INCLUDING another YAML:
======================
[root@localhost ~]# cat include_another.yaml
---
- name: "Lab exercise - Include another yaml"
hosts: webservices
vars:
myname: "Swetha-playbook"
mycompany: "hp-playbook"
tasks:
- debug:
msg:
- "Inside playbook debug mode: {{ myname }} trying including loop.yaml"
- name: Include a play after another play
include: loop.yaml
...
VARIABLES Examples:
===================
[root@localhost ~]# cat variable1.yaml
---
- name: "Lab exercise 4 - 1"
hosts: webservices
tasks:
- name: Create a group called deploy
group: name=deploy state=present
- debug:
msg:
- "Inside playbook debug mode: {{ myname }}"
...
VARIABLES Examples #2:
=========================
[root@localhost ~]# cat one.yaml
---
- name: "Lab exercise 4 - 1"
hosts: webservices
vars:
myname: "Swetha-playbook"
mycompany: "hp-playbook"
tasks:
- name: Create a group called deploy
group: name=deploy state=present
- debug:
msg:
- "Inside playbook debug mode: {{ myname }}"
...
EXTERNAL Vars Examples:
=====================
[root@localhost ~]# cat ext_var.yaml
---
- name: "Lab exercise 4 - 1"
hosts: common
hosts: db
hosts: webservices
vars:
myname: "Swetha-playbook"
mycompany: "hp-playbook"
vars_files:
- "external_vars.yaml"
vars_prompt:
- name: "myname"
private: no
prompt: "What is your name???"
- name: "mycompany"
private: no
prompt: "What is your company???"
tasks:
- name: Create a group called deploy
group: name=deploy state=present
- name: include default step variables
include_vars: include_external_vars.yaml
- debug:
msg:
- "Inside playbook debug mode: {{ myname }}"
- "Inside playbook debug mode: {{ mycompany }}"
- "Inside playbook debug mode myage defined only in inventory file: {{ myage }}"
- debug:
var: ansible_product_uuid
- debug: var=ansible_product_uuid
- debug: msg="{{ mycompany }} {{ myage }} {{ myname }}"
- debug: msg={{ mycompany }}
- debug: msg={{ myage }}
- debug: msg={{ myname }}
- debug: msg={{ ansible_product_uuid }}
...
[root@localhost ~]#
[root@localhost ~]# cat external_vars.yaml
---
myname: "Swetha-externalfile"
mycompany: "hp-externalfile"
myage: "8-externalfile"
...
REGISTER Example:
=================
On ACS:
=======
[root@localhost ~]# cat register.yaml
---
- name: "Lab exercise 4 - 1"
hosts: webservices
vars:
myname: "Swetha-playbook"
mycompany: "hp-playbook"
tasks:
- name: Ansible register variable basic example
shell: "find *.txt"
args:
chdir: /tmp
register: find_output
- debug:
var: find_output
...
[root@localhost ~]# ansible-playbook -i inventory register.yaml
REGISTER Example#2:
======================
On ACS:
=======
[root@localhost ~]# cat register2.yaml
---
- name: "Lab exercise 4 - 1"
hosts: webservices
vars:
myname: "Swetha-playbook"
mycompany: "hp-playbook"
tasks:
- name: Ansible register with_items example
shell: "find *.txt"
args:
chdir: /tmp
register: with_output
- shell: "cp /tmp/{{ item }} /tmp/{{item}}_bkp"
with_items:
- "{{ with_output.stdout_lines }}"
...
[root@localhost ~]#
Output on ARS:
==============
[root@localhost tmp]# ls -lrt /tmp/*bkp
-rw-r--r--. 1 root root 0 Jul 2 14:06 /tmp/4.txt_bkp
-rw-r--r--. 1 root root 0 Jul 2 14:06 /tmp/5.txt_bkp
-rw-r--r--. 1 root root 0 Jul 2 14:06 /tmp/6.txt_bkp
[root@localhost tmp]#
TEMPLATES & HANDLERS Example:
============================
On ACS:
=======
[root@localhost ~]# cat template.yaml
---
- name: "Lab exercise 4 - 1"
hosts: webservices
vars:
myname: "Swetha-playbook"
mycompany: "hp-playbook"
companyname: "hpenterprise"
yourname: "swethareddy"
age: "8"
emailaddress: "xyz"
http_port: 80
max_clients: 200
remote_user: root
tasks:
- name: ensure apache is at the latest version
yum:
name: httpd
state: latest
- name: write the apache config file
template:
src: /srv/httpd.j2
dest: /etc/httpd.conf
notify:
- restart apache
- name: ensure apache is running
service:
name: httpd
state: started
handlers:
- name: restart apache
service:
name: httpd
state: restarted
...
[root@localhost ~]#
LOOPS Example:
=============
On ACS:
=======
[root@localhost ~]# cat loop.yaml
---
- name: "Lab exercise loops "
hosts: webservices
vars:
tasks:
- command: echo {{ item }}
loop: [ 0, 2, 4, 6, 8, 10 ]
when: item > 5
...
[root@localhost ~]# ansible-playbook -i inventory loop.yaml
PLAY [Lab exercise loops] **************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************
ok: [10.76.137.151]
TASK [command] *************************************************************************************************************
skipping: [10.76.137.151] => (item=0)
skipping: [10.76.137.151] => (item=2)
skipping: [10.76.137.151] => (item=4)
changed: [10.76.137.151] => (item=6)
changed: [10.76.137.151] => (item=8)
changed: [10.76.137.151] => (item=10)
PLAY RECAP *****************************************************************************************************************
10.76.137.151 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
#inventory
[web]
192.168.11.129 ansible_become=yes ansible_user=sanjeev myname=sanjeev mycomp=hpe
192.168.11.136 ansible_become=yes ansible_user=sanjeev myname=sanjeev mycomp=hpe
[locl]
localhost ansible_connection=local ansible_become=yes myname=sanjeev mycomp=hpe
[everything:children]
web
locl
[web:vars]
myname=sanjeevkumar
mycomp=hp
myage=8
variable exercises
---
# playbook to print debug in variables
- name: Deploying a webserver and start it
hosts: web
tasks:
# - name: include module variable
# include_vars: include_vars.yaml
- name: Deploying a webserver
yum: name=httpd state=present
- name: starting the httpd server
service: name=httpd state=started
- name: print a debug mesg
debug:
msg: "this is a debug msg to print myname is {{myname}} my company is {{mycomp}} my age is {{myage}} on a system with uuid"
- debug:
var: ansible_product_uuid
- debug:
msg: "system {{myname}} has uuid {{ansible_product_uuid}}"
# vars:
# myname: sanj
# mycomp: HPISO
# vars_files:
# - "vars.yaml"
# vars_prompt:
# - name: "myname"
# private: yes
# prompt: "What is your name?"
register
---
- name: register the output and print
hosts: localhost
gather_facts: false
tasks:
- name: Ansible register variable basic example
shell: "find *.yaml"
args:
chdir: /home/sanjeev
register: find_output
- debug:
var: find_output
handler and template
---
- hosts: web
vars:
http_port: 80
max_clients: 200
remote_user: root
tasks:
- name: ensure apache is at the latest version
yum:
name: httpd
state: latest
- name: write the apache config file
template:
src: httpd.j2
dest: /etc/httpd.conf
notify:
- restart apache
- name: ensure apache is running
service:
name: httpd
state: started
handlers:
- name: restart apache
service:
name: httpd
state: restarted
include
include.yaml
---
- name: include a play in a file
hosts: localhost
gather_facts: false
tasks:
- debug:
msg: play1
- name: Include a play after another play
include: otherplays.yaml
otherplays.yaml
---
- name: start httpd service
service: name=httpd state=started
...
loop
---
- hosts: all
tasks:
- command: echo {{ item }}
loop: [ 0, 2, 4, 6, 8, 10 ]
when: item > 5
Register Example:
register.yaml
`---
- name: Conditional Program
hosts: commons
tasks:- name: Ansible register variable basic example
shell: "find *.txt"
args:
chdir: /tmp
register: find_output - debug:
var: find_output
...`
- name: Ansible register variable basic example
Include Example:
include_other_playbook.yaml
`---
- name: install and start httpd server
hosts: web
tasks:- name: install httpd server
yum: name=httpd state=present
- name: install httpd server
- include: change_index_httpd.yaml
-
name: start httpd server
service: name=httpd state=started -
name: change the index file
include: sub_playbook.yaml
...`
sub_playbook.yaml
`---
- name: install and start httpd server
hosts: web
tasks:- name: modify index.html file
copy: dest=/var/www/html/index.html content='New Welcome to HPE
'
...`
- name: modify index.html file
Template Example:
templates/index.j2
Congratulations! Nice job {{ ansible_hostname }} from {{ mycompany }}!! You have successfully ran your first playbook! Woohooo! Now get yourself a drink to celebrate!. I am {{myname}}, {{myage}}
Template_Example_playbook.yaml
`---
-
name: install and start httpd server
hosts: webvars_prompt:
- name: myname
prompt: What is your name? - name: myage
prompt: what is your age? - name: mycompany
prompt: what is our company?
tasks:
-
name: Ensure that Apache is installed
yum: name=httpd state=present
when: ansible_os_family == "RedHat" -
name: Start Apache Services
service: name=httpd enabled=yes state=started -
name: Copy Site Files
template:
src: templates/index.j2
dest: /var/www/html/index.html
notify:- Restart Apache
handlers:
- name: Restart Apache
service: name=httpd state=restarted
...`
- name: myname
Program 1------
-
hosts: localhost
tasks:args: chdir: /root/sudhakar name: "Ansible register with_items example" register: with_output shell: "find *.txt"
- shell: "cp /root/sudhakar/{{ item }} /root/sudhakar/{{item}}_bkp"
with_items:
- shell: "cp /root/sudhakar/{{ item }} /root/sudhakar/{{item}}_bkp"
program 2.....
-
hosts: localhost
tasks:- debug:
msg: MAJA MADU
- debug:
-
name: Include a play after another play
include: play1.yaml
...
program 3..
- hosts: localhost
vars:
http_port: 80
max_clients: 200
remote_user: root
tasks:- name: ensure apache is at the latest version
yum:
name: httpd
state: latest - name: write the apache config file
template:
src: /root/httpd.j2
dest: /etc/httpd/httpd.conf
notify:- restart apache
- name: ensure apache is running
service:
name: httpd
state: started
handlers:- name: restart apache
service:
name: httpd
state: restarted
- name: restart apache
- name: ensure apache is at the latest version
[root@localhost ~]# vi playbook12_register_vars.yaml
- name: This sets up an httpd webserver
hosts: web
tasks:- name: Ansible register with_items example
shell: "find *.txt"
args:
chdir: /root/playbook12/
register: with_output - shell: "cp /root/playbook12/{{ item }} /root/playbook12/{{item}}_bkp"
with_items:- "{{ with_output.stdout_lines }}"
...
[root@localhost ~]# vi playbook13_include_otherYML.yaml
- "{{ with_output.stdout_lines }}"
- name: Ansible register with_items example
- name: This sets up an httpd webserver13
hosts: web
tasks: - name: Ansible register with_items example
include: playbook12_register_vars.yaml
...
[root@localhost ~]# vi playbook14_handler_new.yaml
-
handlers:
name: "Restart Apache" service: "name=httpd state=restarted"
hosts: web
sudo: true
tasks:name: "Start Apache Services" service: "name=httpd state=started"
- name: "Copy Site Files"
notify:- "Restart Apache"
template: "src=/root/httpd.j2 dest=/etc/httpd/conf/httpd.conf"
- "Restart Apache"
- name: "Copy Site Files"
1st half assignments:
cat inventory3
[root@see1 ansible]# cat inventorygroup
[test]
127.0.0.1 ansible_connection=local
15.213.56.127 ansible_user=root ansible_become=yes
[test1]
15.213.56.128 ansible_user=root ansible_become=yes
[windows]
13.233.119.65 ansible_user=Administrator ansible_password=WsgPP?V%&JWvp.AwfKcacQp@22I!8QF! ansible_connection=winrm ansible_winrm_transport=basic
[common:children]
test
test1
[root@see1 ansible]#
[root@see1 ansible]# cat inventory_1
[test]
127.0.0.1 ansible_connection=local
15.213.56.127 ansible_user=tuser ansible_become=yes ansible_password=tuser ansible_become_user=tuser
15.213.56.128 ansible_user=tuser ansible_become=yes ansible_password=tuser ansible_become_user=tuser
[root@see1 ansible]# cat day3.yaml
- name: third day
hosts: test
vars_files:- "vars.yaml"
vars_prompt: - name: myname
private: no
prompt: "en hesaro ?"
vars:
myname: "yamlmsred"
myage: "31"
mydesk: "281"
mysal: 10
tasks: - debug:
var: System {{ myname }} has uuid {{ ansible_product_uuid }} {{myage}}
msg: " {{myname}} {{myage}} " - name: include task variable
include_vars: "vars.yaml" - debug:
msg: System {{ myname }} has uuid {{ ansible_product_uuid }} {{myage}} {{mydesk}} - debug:
msg: System {{ myname }} has uuid {{ ansible_product_uuid }} {{myage}} - debug:
msg: sal {{ mysal }} - name: deploytest group creation
group: name=deploytest state=present - name: deploytest user creation
user: name=deploytest group=deploytest shell=/bin/bash - name: service starting
service: name=httpd state=started
...
register
- "vars.yaml"
- name: third day
hosts: test
tasks:-
name: Ansible register variable basic example
shell: "find *.txt"
args:
chdir: test
register: find_output -
debug:
var: find_output -
name: including another play
import_playbook: t4.yaml
...
notify handlers:
-
cat dtemp.yaml
- hosts: test
vars:
http_port: 80
max_clients: 200
remote_user: root
tasks:- name: ensure apache is at the latest version
yum:
name: httpd
state: latest - name: write the apache config file
template:
src: /srv/http.j2
dest: /etc/http.conf
notify:- restart apache
- name: ensure apache is running
service:
name: httpd
state: started
handlers:- name: restart apache
service:
name: httpd
state: restarted
...
- name: restart apache
- name: ensure apache is at the latest version
cat loop.yaml
-
name: Ansible register variable basic example
hosts: test
tasks:- debug:
msg: "hello"
- debug:
-
name: Include a play after another play
import_playbook: t4.yaml -
name: Ansible register variable basic example
hosts: test
tasks:- name: install
yum: name={{ item }} state=installed
with_items:- wireshark
- unzip
- vim
- name: install
...
program 1) name: register
- hosts: all
tasks:-
name: Ansible register variable basic example
shell: "find *.txt"
args:
chdir: /home/Ahamed/
register: find_output -
debug:
var: find_output
-
Make sure that /test should not be empty
...
program 2) name:include another yaml
main yaml file:
-
hosts: deploy
sudo: yestasks:
-
name: Ensure that Apache is installed
yum: name=httpd
when: ansible_os_family == "RedHat" -
name: Start Apache Services
service: name=httpd enabled=yes state=started
handlers:
- include_tasks: more_handlers.yml
...
-
included yaml:
- debug:
msg: apache server is being started...... - name: restart apache
service: name=apache state=restarted - debug:
msg: apache server restart is completed
...
program 3)handlers:
main yaml file:
-
hosts: deploy
sudo: yestasks:
-
name: Ensure that Apache is installed
yum: name=httpd
when: ansible_os_family == "RedHat" -
name: Start Apache Services
service: name=httpd enabled=yes state=started
handlers:
- include_tasks: more_handlers.yml
...
-
included yaml:
- debug:
msg: apache server is being started...... - name: restart apache
service: name=apache state=restarted - debug:
msg: apache server restart is completed
...
program 4)templates:
- name: This sets up an httpd webserver
hosts: deploy
vars:
ansible_ssh_private_key_file: remote.pem
companyname: verizon
pack: httpd
tasks:- name: Install the httpd apps
yum: name=httpd - name: Deploy configuration File
template: src=/home/Ahamed/index.j2 dest=/var/www/html/index.html - name: start the httpd service
service: name=httpd state=started - name: Install common software requirements
yum: pkg={{ item }} state=installed
with_items:- git
- ntp
- vim
- name: Creating a file
file: path=/opt/verizon.txt state=touch
when:- ansible_distribution == "RedHat"
- ansible_distribution_major_version == "7.5"
...
- name: Install the httpd apps
program5) loop:
- name: This sets up an httpd webserver
hosts: deploy
vars:
ansible_ssh_private_key_file: remote.pem
companyname: verizon
pack: httpd
tasks:- name: Install the httpd apps
yum: name=httpd - name: Deploy configuration File
template: src=/home/Ahamed/index.j2 dest=/var/www/html/index.html - name: start the httpd service
service: name=httpd state=started - name: Install common software requirements
yum: pkg={{ item }} state=installed
with_items:- git
- ntp
- vim
- name: Creating a file
file: path=/opt/verizon.txt state=touch
when:- ansible_distribution == "RedHat"
- ansible_distribution_major_version == "7.5"
...
- name: Install the httpd apps
[root@master Training]# cat register.yml
- name: Start http service on multiple machine
hosts: localhost
tasks:- name: Ansible register variable basic.
shell: "find *.txt"
args:
chdir: group_vars
- name: Ansible register variable basic.
args:
state: started
register: find_output
- debug:
msg: Files found in {{ find_output }}
...
[root@master Training]# cat with_item.yaml
- name: Ansible to include with items to display
hosts: localhost
tasks:- name: Ansible register with_items variable.
shell: "find *.txt"
args:
chdir: /root/Training/group_vars
register: with_output - debug:
msg: "Output is {{ with_output }}" - shell: "cp {{ item }} {{ item }}_bkp"
args:
chdir: /root/Training/group_vars
- name: Ansible register with_items variable.
- shell: "echo {{ item }}"
with_items:
- "{{ with_output.stdout_lines }}"
...
[root@master Training]# cat debug_var.yml
- name: Debug message understanding yml
hosts: common
vars:
myname: "Prakash-Playbook"
mycomponay: "HPE-Playbook"
vars_files:
- "vars.yml"
vars_prompt:
- name: myname
private: no
prompt: "what is my name?"
- name: include default variables
include_vars: var.yml
tasks:
- debug: msg="At debug line {{ mycomponay }}-line"
- debug: msg={{ mycomponay }}
- debug:
msg: "{{ mycomponay }}"
- debug:
msg: Myname is {{ myname }} and my company is {{ mycomponay }}
var: mycomponay
...
Program-1
vars.yaml
---
- name: Print the variables to see precedence
#hosts: group1
#hosts: common
hosts: db
vars:
myname: "neetesh-playbook"
mycomp: "hpe-playbook"
vars_files:
- "vars_file.yaml"
vars_prompt:
- name: "myname"
private: no
#private: yes
prompt: "What is your name ?"
- name: "mycomp"
private: no
prompt: "What is your company ?"
#tasks:
# - name: Print the task variables
# ## Include variables using include_vars
# include_vars: vars_file2.yaml
tasks:
- include_vars: "vars_file2.yaml"
- name: Print the variables
debug:
msg: My name is {{ myname }} works with {{ mycomp }}
- name: More ways to print the variables
debug: msg="My name is {{ myname }} works with {{ mycomp }}"
#debug: msg="{{ myname }}"
- name: Some more ways to print the variables
#debug: var=myname
debug:
var: myname
verbosity: 0
...
Program-2
vars2.yaml
---
- name: Print the variables to see precedence
#hosts: group1
#hosts: common
#hosts: db:web ## multiple groups can be passed
hosts: db,web ## multiple groups can be passed
tasks:
- name: Print the variables
debug:
msg: My name is {{ myname }} works with {{ mycomp }}
- name: More ways to print the variables
debug: msg="My name is {{ myname }} works with {{ mycomp }}"
#debug: msg="{{ myname }}"
- name: Some more ways to print the variables
#debug: var=myname
debug:
var: myname
verbosity: 0
...
Program-3
register.yaml
---
- name: Register the output of a shell command
hosts: test
tasks:
- name: Find all the txt files from a dir
shell: "find *.txt"
args:
chdir: /home/tester
register: reg_output
- name: Print all the txt files captured in register variable
debug:
var: reg_output
...
Program-4
include.yaml
---
- name: Register the output of a shell command
hosts: test
tasks:
- debug:
msg: "Playbook 1"
- name: Include a play after another play
include: register.yaml
...
Program-5
handlers.yaml
---
- name: Calling handler to restart the Apache server on notifying the conf change
hosts: test
vars:
http_port: 80
max_clients: 2000
# remote_user: root
tasks:
- name: ensure apache is at the latest version
yum:
name: httpd
state: latest
- name: write the apache config file
template:
src: /home/tester/httpd.j2
dest: /tmp/httpd.conf
notify:
- restart apache
- name: ensure apache is running
service:
name: httpd
state: started
handlers:
- name: restart apache
service:
name: httpd
state: reloaded
...
Program-6
loop.yaml
---
- name: Install software
hosts: test
become: yes
tasks:
- name: Install common software requirements
yum: name={{ item }} state=latest
with_items:
- git
- htop
- vim
- name: Print the list elements
command: echo {{ item }}
loop: [ 0, 2, 4, 6, 8, 10 ]
when: item > 5
...
----------------REGISTERS-------------------
---
- name: This is for eight steps exersise
hosts: web
tasks:
- name: Ansible register variable basic example
shell: "find *.html"
args:
chdir: /var/www/html
register: find_htmlfiles
- debug:
# msg: System {{ myname }} has uuid {{ ansible_product_uuid }}
# var: myage
# var: myname
var: find_htmlfiles
...
----------------INCLUDE-------------------
---
- name: This is for eight steps exersise
hosts: web
tasks:
- name: Ansible register variable basic example
shell: "find *.html"
args:
chdir: /var/www/html
register: find_htmlfiles
# - name: include default step variables at task level
# include_vars: external_tasklevelfile.yaml
- debug:
# msg: System {{ myname }} has uuid {{ ansible_product_uuid }}
# var: myage
# var: myname
var: find_htmlfiles
- name: Include task varaibel here
include: variable.yaml
...
----------------HANDLERS-------------------
---
- name: This is for eight steps exersise
vars:
ansible_user: user1
ansible_password: user1
hosts: web
tasks:
- name: Install the httpd apps
yum: name=httpd
notify: restart
handlers:
- name: restart
service: name=httpd state=started
...
----------------Template-------------------
---
- name: This is for eight steps exersise
hosts: web
tasks:
- name: Copy HTML Content to a file
template: src=/root/index.j2 dest=/var/www/html/index.html
...
----------------LOOP-------------------
---
- name: This is for loop
hosts: web
tasks:
- command: echo {{ item }}
loop: [ 0, 2, 4, 6, 8, 10 ]
when: item > 5
...
INCLUDING another YAML: ====================== [root@localhost ~]# cat include_another.yaml --- - name: "Lab exercise - Include another yaml" hosts: webservices vars: myname: "Swetha-playbook" mycompany: "hp-playbook" tasks: - debug: msg: - "Inside playbook debug mode: {{ myname }} trying including loop.yaml" - name: Include a play after another play include: loop.yaml ... VARIABLES Examples: =================== [root@localhost ~]# cat variable1.yaml --- - name: "Lab exercise 4 - 1" hosts: webservices tasks: - name: Create a group called deploy group: name=deploy state=present - debug: msg: - "Inside playbook debug mode: {{ myname }}" ... VARIABLES Examples #2: ========================= [root@localhost ~]# cat one.yaml --- - name: "Lab exercise 4 - 1" hosts: webservices vars: myname: "Swetha-playbook" mycompany: "hp-playbook" tasks: - name: Create a group called deploy group: name=deploy state=present - debug: msg: - "Inside playbook debug mode: {{ myname }}" ... EXTERNAL Vars Examples: ===================== [root@localhost ~]# cat ext_var.yaml --- - name: "Lab exercise 4 - 1" hosts: common hosts: db hosts: webservices vars: myname: "Swetha-playbook" mycompany: "hp-playbook" vars_files: - "external_vars.yaml" vars_prompt: - name: "myname" private: no prompt: "What is your name???" - name: "mycompany" private: no prompt: "What is your company???" tasks: - name: Create a group called deploy group: name=deploy state=present - name: include default step variables include_vars: include_external_vars.yaml - debug: msg: - "Inside playbook debug mode: {{ myname }}" - "Inside playbook debug mode: {{ mycompany }}" - "Inside playbook debug mode myage defined only in inventory file: {{ myage }}" - debug: var: ansible_product_uuid - debug: var=ansible_product_uuid - debug: msg="{{ mycompany }} {{ myage }} {{ myname }}" - debug: msg={{ mycompany }} - debug: msg={{ myage }} - debug: msg={{ myname }} - debug: msg={{ ansible_product_uuid }} ... [root@localhost ~]# [root@localhost ~]# cat external_vars.yaml --- myname: "Swetha-externalfile" mycompany: "hp-externalfile" myage: "8-externalfile" ... REGISTER Example: ================= On ACS: ======= [root@localhost ~]# cat register.yaml --- - name: "Lab exercise 4 - 1" hosts: webservices vars: myname: "Swetha-playbook" mycompany: "hp-playbook" tasks: - name: Ansible register variable basic example shell: "find *.txt" args: chdir: /tmp register: find_output - debug: var: find_output ... [root@localhost ~]# ansible-playbook -i inventory register.yaml REGISTER Example#2: ====================== On ACS: ======= [root@localhost ~]# cat register2.yaml --- - name: "Lab exercise 4 - 1" hosts: webservices vars: myname: "Swetha-playbook" mycompany: "hp-playbook" tasks: - name: Ansible register with_items example shell: "find *.txt" args: chdir: /tmp register: with_output - shell: "cp /tmp/{{ item }} /tmp/{{item}}_bkp" with_items: - "{{ with_output.stdout_lines }}" ... [root@localhost ~]# Output on ARS: ============== [root@localhost tmp]# ls -lrt /tmp/*bkp -rw-r--r--. 1 root root 0 Jul 2 14:06 /tmp/4.txt_bkp -rw-r--r--. 1 root root 0 Jul 2 14:06 /tmp/5.txt_bkp -rw-r--r--. 1 root root 0 Jul 2 14:06 /tmp/6.txt_bkp [root@localhost tmp]# TEMPLATES & HANDLERS Example: ============================ On ACS: ======= [root@localhost ~]# cat template.yaml --- - name: "Lab exercise 4 - 1" hosts: webservices vars: myname: "Swetha-playbook" mycompany: "hp-playbook" companyname: "hpenterprise" yourname: "swethareddy" age: "8" emailaddress: "xyz" http_port: 80 max_clients: 200 remote_user: root tasks: - name: ensure apache is at the latest version yum: name: httpd state: latest - name: write the apache config file template: src: /srv/httpd.j2 dest: /etc/httpd.conf notify: - restart apache - name: ensure apache is running service: name: httpd state: started handlers: - name: restart apache service: name: httpd state: restarted ... [root@localhost ~]# LOOPS Example: ============= On ACS: ======= [root@localhost ~]# cat loop.yaml --- - name: "Lab exercise loops " hosts: webservices vars: tasks: - command: echo {{ item }} loop: [ 0, 2, 4, 6, 8, 10 ] when: item > 5 ... [root@localhost ~]# ansible-playbook -i inventory loop.yaml PLAY [Lab exercise loops] ************************************************************************************************** TASK [Gathering Facts] ***************************************************************************************************** ok: [10.76.137.151] TASK [command] ************************************************************************************************************* skipping: [10.76.137.151] => (item=0) skipping: [10.76.137.151] => (item=2) skipping: [10.76.137.151] => (item=4) changed: [10.76.137.151] => (item=6) changed: [10.76.137.151] => (item=8) changed: [10.76.137.151] => (item=10) PLAY RECAP ***************************************************************************************************************** 10.76.137.151 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
https://github.com/pitchela/Ansible_Training_July_2019_Swetha
On be-half of Harish
https://github.com/chinnigit/Ansible_Training.git
All the playbooks called through role.
program 1 Loop :
hosts: acs
vars:
listvar1:
tasks:
with_nested:
yum: pkg={{ item }} state=installed
with_items:
program 2 : register.yml
hosts: acs
tasks:
name: Ansible register with_items example
shell: "find test*.yml"
args:
chdir: /root/
register: with_output
shell: "cp {{ item }} {{item}}_bkp"
with_items:
program 3 : handlers_playbook.yml
vars:
http_port: 80
max_clients: 200
remote_user: root
tasks:
yum:
name: httpd
state: latest
template:
src: /root/httpd.j2
dest: /etc/httpd/conf/httpd.conf
notify:
service:
name: httpd
state: started
handlers:
service:
name: httpd
state: restarted
program 4 : variables
vars:
myname: "nandini-playbook"
mycompany: "hpe-playbook"
vars_prompt:
- name: "myname"
private: no
prompt: "what is your name?"
- name: "mycompany"
private: no
prompt: "what is your company name?"
vars_files:
- "/root/external_vars.yml"
tasks:
- name: "task variables"
include_vars: /root/task_vars.yml