2
votes

I'm trying to find out how I can pass Ansible playbook variables defined in a 'master' playbook into other playbooks that I've already referenced in my master playbook.

For example, with the below 'master' playbook, I'd like to pass the values of sethostname and setipaddress to playbook[1-3].yml referenced in my tasks section. This would be akin to calling functions in other programming languages.

---
- hosts: all
  become: yes

  vars_prompt:
   - name: "sethostname"
     prompt: "What will be the machine's hostname?"
     private: no

   - name: "setipaddress"
     prompt: "What will be the machine's IP address?"
     private: no

  tasks:
    - include: playbook1.yml
    - include: playbook2.yml
    - include: playbook3.yml
2
① You don't include playbooks in the code you posted. You include tasks. ② Variables you define are available to tasks you include this way.techraf

2 Answers

5
votes

As of Ansible 2.4 you can now import playbooks, meaning they will get pre-processed at the time the playbook is parsed, and will run in the order you import them. Check out the docs for the full info on import vs. include here http://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse_includes.html

You can pass vars to them just like you can to other include_* tasks.

  tasks:
    - import_playbook: playbook1.yml
      vars:
        sethostname: "{{ sethostname }}"
        setipaddress "{{ setipaddress }}"
2
votes

Kellie's answer is not exactly accurate , playbooks may not be imported at task level only at the topmost level of a playbook. So this works:

- import_playbook: playbook1.yml
  vars:
    sethostname: "{{ sethostname }}"
    setipaddress "{{ setipaddress }}"