murkywaters
murkywaters

Reputation: 115

How can I pass Ansible playbook variables from a master playbook to a linked playbook?

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

Upvotes: 2

Views: 3889

Answers (2)

Mayuresh Gaitonde
Mayuresh Gaitonde

Reputation: 61

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 }}"

Upvotes: 3

Tj Kellie
Tj Kellie

Reputation: 6476

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 }}"

Upvotes: 3

Related Questions