Ansible - lookup vars

The lookup plugin is always executed on the control node (that's your Ansible server), which is to say that the lookup plugin does not do something on your managed nodes (the target systems).

To use the vars plugin, the Python vars.py script must be in your Ansible plugins lookup directory, such as /usr/lib/python2.7/site-packages/ansible/plugins/lookup/vars.py.

Let's say foo.yml contains the following. Notice the vars plugin is being used to create the "foo" variable.

---
- hosts: localhost
  vars:
    foo: bar
  tasks:
  - debug:
      msg: "{{ lookup('vars', 'foo') }}"
...

 

Something like this should be returned.

TASK [debug]
ok: [localhost] => {
    "msg": "bar"
}

 

One thing that is kind of unique and powerful about the lookup plugin is being able to search with multiple "parts" of a variable name.

---
- hosts: localhost
  vars:
    password_dev: devpw
    password_prod: prodpw
  tasks:
  - debug:
      msg: "{{ lookup('vars', 'password_' + 'dev' ) }}"
...

 

Or like this.

---
- hosts: localhost
  vars:
    password_dev: devpw
    password_prod: prodpw
    env: dev
  tasks:
  - debug:
      msg: "{{ lookup('vars', 'password_' + env ) }}"
...

 

Which should return the following.

TASK [debug]
ok: [localhost] => {
    "msg": "devpw"
}


Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.





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




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |