Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devops-school/ac5f0830149ff59899d1773370102528 to your computer and use it in GitHub Desktop.
Save devops-school/ac5f0830149ff59899d1773370102528 to your computer and use it in GitHub Desktop.
hp-ansible-classroom-playbook-june-2019-batch1.md
@Ashu110-8
Copy link

@muralidharbm1411
Copy link

@Chandrakanth-R
Copy link

@pitchela
Copy link

pitchela commented Jul 4, 2019

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

@sowmyaansi
Copy link

@BhanuPrakashP
Copy link

@raghvendrasawara
Copy link

@neetesh80
Copy link

neetesh80 commented Jul 4, 2019

All the playbooks called through role.

https://github.com/neetesh80/ansible_handson

@manju0821
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment