-
-
Save devops-school/ac5f0830149ff59899d1773370102528 to your computer and use it in GitHub Desktop.
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.
#inventory
variable exercises
register
handler and template
include
loop