Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include vars in role tasks

In a role, I am trying to load some variables from another role. (If that role was included in the current play, the variables would be accessible, but it's not so they're not.)

So I tried this:

- include_vars: ../../another_role/defaults/main.yml

But it doesn't work, no error but the variables are still undefined. So I tried to be smart and symlink the file to vars/another_role_defaults.yml in the role where I want to use the vars and then include it like this:

- include_vars: another_role_defaults.yml

Same result, no error (why doesn't it throw an error if the file cannot be found??) but variables are still undefined. I tried this as well, for good measure, but still no cigar.

- include_vars: ../vars/another_role_defaults.yml

What am I doing wrong?

like image 481
Manuel Meurer Avatar asked Jul 03 '14 14:07

Manuel Meurer


People also ask

How do you use vars in Ansible roles?

When you add a tag to the role option, Ansible applies the tag to ALL tasks within the role. When using vars: within the roles: section of a playbook, the variables are added to the play variables, making them available to all tasks within the play before and after the role.

How do I put vars in playbook?

The include_vars module can be used in a playbook or role to load variables from a file. Simply set the value of include_vars to a local file to load the variables it contains: --- # ./hello_world. yml - name: print greeting hosts: "*" tasks: - include_vars: name_vars.

Can we pass variables in playbook yes or no?

With Ansible, users have the flexibility to accept external input while executing their Ansible playbooks without changing the Ansible playbook content. This can be done via the ability to pass extra variables to an Ansible playbook. This feature is available when using the Ansible CLI or Ansible Tower.

What is Vars in Ansible playbook?

Ansible uses variables to manage differences between systems. With Ansible, you can execute tasks and playbooks on multiple different systems with a single command. To represent the variations among those different systems, you can create variables with standard YAML syntax, including lists and dictionaries.


2 Answers

As of ansible-core 2.11 you can do this:

- include_vars: ../../nrpe/vars/standard_nrpe_checks.yml
  name: standard_nrpe_checks
- debug:
    msg: "{{ standard_nrpe_checks }}"

To get the same result you can also use set_fact with the lookup plugin.

- debug:
    msg: "{{ lookup('file', '../../nrpe/vars/standard_nrpe_checks.yml') }}"

I wanted to mention if you're using collections and distributing a playbook you can use

playbooks
  |_vars/
    |_standard_nrpe_checks.yml

In this instance I'm now able to share vars from within a collection and since it's in the lookup path I don't need to specify it relatively. I.e. instead of having a list be an nrpe role default read it in for both roles and maintain it in one place.

- include_vars: standard_nrpe_checks.yml
  name: standard_nrpe_checks
- debug:
    msg: "{{ standard_nrpe_checks }}"
TASK [namespace.my_collection.icinga : debug] *******************************************
ok: [hostname] =>
  msg: |-
    standard_nrpe_checks:
      check_users:
        script: check_users
like image 195
b0bu Avatar answered Oct 19 '22 17:10

b0bu


It was my own fault in the end... I tested this by using the debug module and tags like this:

- include_vars: ../../another_role/defaults/main.yml

- debug: msg={{ variable }}
  tags: foo

and then executing the playbook like this:

  ansible-playbook -vvvv playbook.yml --tags foo

Once I left out the tags, it works (of course). The problem was that I should have added the tags to the include_vars command as well like this:

- include_vars: ../../another_role/defaults/main.yml
  tags: foo

- debug: msg={{ variable }}
  tags: foo
like image 20
Manuel Meurer Avatar answered Oct 19 '22 17:10

Manuel Meurer