Created
July 3, 2019 10:29
-
-
Save devops-school/ac5f0830149ff59899d1773370102528 to your computer and use it in GitHub Desktop.
hp-ansible-classroom-playbook-june-2019-batch1.md
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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
[root@master Training]# cat register.yml
hosts: localhost
tasks:
shell: "find *.txt"
args:
chdir: group_vars
args:
state: started
...
[root@master Training]# cat with_item.yaml
hosts: localhost
tasks:
shell: "find *.txt"
args:
chdir: /root/Training/group_vars
register: with_output
msg: "Output is {{ with_output }}"
args:
chdir: /root/Training/group_vars
- shell: "echo {{ item }}"
...
[root@master Training]# cat debug_var.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
var: mycomponay
...