I have 1 main playbook and 3 playbooks imported in the main playbook.
These 3 imported playbooks are dependent on each other.

1st imported playbook will get host inventory from main play through 
add_host 
2nd  imported playbook will get host inventory from 1st play through 
add_host
3rd imported playbook will get host inventory from its playbook itself.


My requirements are,

1. 3rd playbook should have whatever vars available in playbook 1 and 2    
                                                           -- I am able to 
get this through set_fact and with_nested
2. 2nd playbook should be skipped when there are no hosts added through 
add_host in the 1st playbook.       
3. If 2nd playbook is skipped, 3rd playbook should have vars of the 2nd 
playbook.

I tried to run the below playbooks but when the 2nd playbook is skipped, 
the 3rd playbook is giving an error that it's unable to fetch vars from 2rd 
playbook through hostvars as it's skipped.

error:
: FAILED! => {"msg": "'dict object' has no attribute 'nfs_clients'"}


I need to write a "when condition" in the 3rd playbook to get the vars from 
2nd playbook only if it's executed.

Please assist to achieve the 2nd and 3rd requirement through condition 
check.

Main Play:

- name:
  hosts:  rchadm01
  vars_files:
  - var.yml
  tasks:
    - name: 
      add_host:
        name: "{{ decom_host }}"
        groups: decom_host_group

- name: Get NFS Clients
  import_playbook: sub_plays/play2.yml

- name: Play on NFS clients
  import_playbook: sub_plays/play3.yml
  when: '"nfs_clients" in group_names'

- name: Decom Tasks
  import_playbook:  sub_plays/4_decom_tasks.yml


Play 1:

    - name:                      
      add_host:
        name: "{{ decom_host }}"
        groups: decom_host_group

Play 2:

    - name: 
      add_host:
        name: "{{ item  }}"
        group:  "nfs_clients"
      with_items: "{{ nfs_clients_out.stdout_lines }}"

Play 3:

---
- name: Running Decom Tasks
  hosts:  rchadm01
  vars_files:
  - var.yml
  tasks:
    - name: Fetch Variable from Previous Play
      set_fact:
        open_shares_info: "{{ hostvars[item]['open_shares_info'] }}"
      with_items:
        - '{{groups["decom_host_group"]}}'
    - name: Fetch Variable from Previous Play
      set_fact:
        linked_dirs_list: "{{ hostvars[item[0]]['linked_dirs_list'] }}"
        nfs_hard_mounts:  "{{ hostvars[item[1]]['nfs_hard_mounts'] }}"
      with_nested:
        - '{{groups["nfs_clients"]}}'
        - '{{groups["nfs_clients"]}}'
      when: "'nfs_clients' in group_names"
 

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/e1aeefbc-9972-4fd1-891f-7c9cd3031661%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to