Bootstrap FreeKB - Ansible - Get variables from a file using include_vars
Ansible - Get variables from a file using include_vars

Updated:   |  Ansible articles

Following are the differet ways that variables can be set in Ansible. This list is in the order of precedence, where the option higher in the list takes precedence over options lower in the list.

Often, variables are created in a playbook, like this.

---
- hosts: all
  vars:
    foo: hello
    bar: world
  tasks:
  - debug:
      var: foo
  - debug:
      var: bar
...

 

Instead of creating variables in a playbook, variables can be created in a file, such as vars.yml or vars.json. Let's say vars.yml contains the following.

foo: Hello

 

include_vars can then be used to load the variables in the file. In this example, the vars.yml file could be in the same directory as the playbook, or in the vars directory (vars/vars.yml).

---
- hosts: all
  tasks:
  - name: include vars.yml
    include_vars: vars.yml

  - debug:
      var: foo

  - debug:
      var: bar
...

 

Or like this.

---
- hosts: all
  tasks:
  - name: include vars.yml
    include_vars:
      file: vars.yml

  - debug:
      var: foo

  - debug:
      var: bar
...

 

Let's say vars.yml does not exist and your playbook is using vars_files.

---
- hosts: all
  vars_files:
  - vars.yml
  tasks:
  - debug:
      var: foo
...

 

This will cause the playbook to fail, returning something like this.

ERROR! vars file vars.yml was not found
Could not find file on the Ansible Controller.
If you are using a module and expect the file to exist on the remote, see the remote_src option

 

If you don't want your playbook to fail if the vars file does not exist, or if you want to print some improved output, you could use stat to determine if the vars file exists and then use when to do something based on whether the vars file does or does not exist.

---
- hosts: all
  pre_tasks:
  - stat:
      path: vars.yml
    register: out

  - name: include vars.yml
    include_vars: vars.yml
    when: out.stat.exists == true 

  tasks:
  - debug:
      var: foo
...

 

 

AVOID TROUBLE

The -e or --extra-vars command line optionExtra Variables in Tower and set_fact module will take precedence over include_vars.

 

If the file is successfully loaded, something like this should be returned.

TASK [include vars.yml]
ok: [server1.example.com]

TASK [debug]
ok: [server1.example.com] => {
    "foo": "Hello"
}

 

On the other hand, let's say vars.yml does not exist. In this scenario, something like this should be returned.

TASK [include vars.yml]
fatal: [server1.example.com]: FAILED! => {"ansible_facts": {}, "ansible_included_var_files": [], "changed": false, "message": "Could not find or access 'vars.yml'\nSearched in:\n\t/usr/local/ansible/vars/vars.yml\n\t/usr/local/ansible/vars.yml\n\t/usr/local/ansible/vars/vars.yml\n\t/usr/local/ansible/vars.yml on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote_src option"}

 

The when parameter can be used to only include variables when a certain condition is met.

---
- hosts: all
  tasks:
    - include_vars: vars.yml
      when: foo is not defined

 

The name parameter can be used to store the variables in vars.yml in a variable, such as my_included_vars.

---
- hosts: all
  tasks:
    - name: include_vars vars.yml
      include_vars:
        file: vars.yml
        name: my_included_vars

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 32f18b in the box below so that we can be sure you are a human.