passing "variable" from one playbook to another playbook

Viewed 40

i'm trying to call one playbook1.yaml into master-playbook.yaml and i want to use variable that defined through set_facts into playbook1 into master playbook. but i dont know what is correct way to do...please help me here with better example..

playbook1.yaml:

  - name: local_ip_fact
    set_fact:
           local_ip: "{{ sites[site]['peer_ip'] | ipaddr('address') }}"
    when: "site in sites.keys()"

master-playbook.yaml:

---
  - import_playbook: playbook1.yaml:
  - hosts: '{{ localhost | default("PALO_HOST") }}'
    connection: local
    become: yes
    gather_facts: true
1 Answers

Q: "In the master playbook, use variables defined in playbook1."

A: Use hostvars. See Caching facts. For example, given the tree and the content

shell> tree .
.
├── ansible.cfg
├── group_vars
│   └── all
│       └── sites.yml
├── hosts
├── master.yml
└── playbook1.yml
shell> cat group_vars/all/sites.yml 
sites:                                                                                    
  test_11:                                                                                
    peer_ip: 10.2.0.61                                                                    
  test_12:                                                                                
    peer_ip: 10.2.0.62
shell> cat hosts
[test]
test_11
test_12
test_13
shell> cat playbook1.yml 
- hosts: test
  gather_facts: false
  vars:
    site: "{{ inventory_hostname }}"
  tasks:
    - name: local_ip_fact
      set_fact:
        local_ip: "{{ sites[site]['peer_ip']|ansible.utils.ipaddr('address') }}"
      when: site in sites.keys()|list
shell> cat master.yml 
- import_playbook: playbook1.yml
- hosts: localhost
  gather_facts: false
  tasks:
    - debug:
        msg: "{{ item }}: {{ hostvars[item]['local_ip'] }}"
      loop: "{{ sites.keys()|list }}"

gives

shell> ansible-playbook master.yml 

PLAY [test] **********************************************************************************

TASK [local_ip_fact] *************************************************************************
skipping: [test_13]
ok: [test_11]
ok: [test_12]

PLAY [localhost] *****************************************************************************

TASK [debug] *********************************************************************************
ok: [localhost] => (item=test_11) => 
  msg: 'test_11: 10.2.0.61'
ok: [localhost] => (item=test_12) => 
  msg: 'test_12: 10.2.0.62'

PLAY RECAP ***********************************************************************************
localhost: ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test_11  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test_12  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test_13  : ok=0    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0

The scheme can be simplified by creating a dictionary in group_vars. For example,

shell> cat group_vars/all/local_ips.yml 
_hosts: "{{ groups.test|intersect(sites.keys()|list) }}"
_ips: "{{ _hosts|map('extract', sites, 'peer_ip')|list }}"
local_ips: "{{ dict(_hosts|zip(_ips)) }}"

Then the playbook

shell> cat master.yml
- hosts: localhost
  gather_facts: false
  tasks:
    - debug:
        var: local_ips

gives

shell> ansible-playbook master.yml 

PLAY [localhost] *****************************************************************************

TASK [debug] *********************************************************************************
ok: [localhost] => 
  local_ips:
    test_11: 10.2.0.61
    test_12: 10.2.0.62

PLAY RECAP ***********************************************************************************
localhost: ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
Related