Ansible Playbook Programming using Include

Example 1 - Using When Condition

------------first.yaml----------------------
---
- hosts: localhost
  tasks:
    - debug:
        msg: "Hello Everyoone"
    - include: second.yaml

- name: Include a play after another play
  include: third.yaml
------------second.yaml----------------------
---
- debug:
    msg: "This is a Second Yaml"


------------third.yaml----------------------
---
- hosts: localhost
  tasks:
    - debug:
        msg: "This is a Third Yaml"
    

Example 1 - Using When Condition

# Program 1
- hosts: localhost
  tasks:
    - debug:
        msg: play1

- name: Include a play after another play
  include: otherplays.yaml
    

Example 2 - Using When Condition

# Program 2
- hosts: all
  tasks:
    - debug:
        msg: task1

    - name: Include task list in play
      include: stuff.yaml

    - debug:
        msg: task10
    

Example 3 - Using When Condition

# Program 3
- hosts: all
  tasks:
    - debug:
        msg: task1

    - name: Include task list in play only if the condition is true
      include: "{{ hostvar }}.yaml"
      static: no
      when: hostvar is defined
    

Example 4 - Using When Condition

# Program 4
- name: this is a play at the top level of a file
  hosts: all
  remote_user: root
  tasks:
  - name: say hi
    tags: foo
    shell: echo "hi..."
    
- include: intro_example.yml
    

Example 5 - Using When Condition

#my_tasks.yml
- name: Check PID of existing Java process
  shell: "ps -ef | grep [j]ava"
  register: java_status

  - debug: var=java_status.stdout

#check_java_pid.yml
---
- hosts: all
  gather_facts: no

  tasks:
    - include my_tasks.yml

#check_java_pid.yml
---
- hosts: instance_1
  gather_facts: no

  tasks:
    - include: my_tasks.yml
    

Avail Rajesh Kumar as trainer at 50% Discount
Puppet Online Training
Puppet Classroom TrainingEnroll Now