简体   繁体   中英

Ansible hostvars undefined

I have a very simple play which saves vars and looks them up in hostvars.

- name: Set hostvars
  hosts: localhost
  vars:
    var_one: "I am a"
    var_two: "test"
  tasks:
    - debug: var=hostvars['localhost']['var_one']
    - debug: var=hostvars['localhost']['var_two']

However, when I run this play the vars aren't defined:

PLAY [Set hostvars] ************************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [debug] *******************************************************************
ok: [localhost] => {
    "hostvars['localhost']['var_one']": "VARIABLE IS NOT DEFINED!"
}

TASK [debug] *******************************************************************
ok: [localhost] => {
    "hostvars['localhost']['var_two']": "VARIABLE IS NOT DEFINED!"
}

How can I save these vars in hostvars?

You can set host facts runtime using set_fact module:

---
- name: Set hostvars
  hosts: localhost
  tasks:
    - set_fact: var_one="I am a"
    - set_fact: var_two="test"
    - debug: var=hostvars['localhost']['var_one']
    - debug: var=hostvars['localhost']['var_two']

Quoting the documentation :

These variables will survive between plays during an Ansible run, but will not be saved across executions even if you use a fact cache.

This is where the difference between facts (variables bound to Ansible target hosts) and regular variables can be seen.

Variables are internally stored in vars structure, so you can access them with:

tasks:
  - debug: var=vars['var_one']
  - debug: var=vars['var_two']

Facts, on the other hand are stored in hostvars .


In either case, unless you were referring to a variable name with a dynamic name, or a fact bound to another host than the one executing the task, you can simply use the variable/fact name by using its name:

tasks:
  - debug: var=var_one
  - debug: var=var_two

Try using

msg=

instead of var=. As per help of debug module

var - A variable name to debug. Mutually exclusive with the 'msg' option.

- name: Set hostvars
  hosts: localhost
  vars:
    var_one: I am a
    var_two: est
  tasks:
    - debug: msg=hostvars['localhost']['var_one']
    - debug: msg=hostvars['localhost']['var_two']
...



PLAY [Set hostvars] ************************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [debug] *******************************************************************
ok: [localhost] => {
    "msg": "hostvars['localhost']['var_one']"
}

TASK [debug] *******************************************************************
ok: [localhost] => {
    "msg": "hostvars['localhost']['var_two']"
}

PLAY RECAP *********************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM