参见ansible'中的键;使用变量名而不是字符串的hostvars字典

参见ansible'中的键;使用变量名而不是字符串的hostvars字典,ansible,Ansible,我的主机在一个剧本中定义,我在一个主机中定义一个变量,然后尝试在另一个主机中使用hostvars引用它。一个简单的示例演示了我需要什么: topmost.yml包括两个文件: [build@localhost ~]$ cat topmost.yml - include: top.yml - include: test.yml top.yml定义两个变量,并对每个变量调用add\u host: [build@localhost ~]$ cat top.yml - hosts: all

我的
主机
在一个剧本中定义,我在一个主机中定义一个变量,然后尝试在另一个主机中使用
hostvars
引用它。一个简单的示例演示了我需要什么:

topmost.yml
包括两个文件:

[build@localhost ~]$ cat topmost.yml 
- include: top.yml

- include: test.yml
top.yml
定义两个变量,并对每个变量调用
add\u host

[build@localhost ~]$ cat top.yml 
- hosts: all
  name: Define hosts
  connection: local

  vars:
  - webserver: 10.193.219.244
  - nfs: 172.100.139.200

  tasks:

  - name: Add webserver
    add_host:
      hostname: "{{ webserver }}"
      groups: "webserver_host"

  - name: Add nfs server
    add_host:
      hostname: "{{ nfs }}"
      groups: "nfs_server"
最后,在
test.yml
中,我想创建一个临时文件(在
webserver\u host
上),然后在
nfs\u server
下的部分中使用它

[build@localhost ~]$ cat test.yml 

 - hosts: webserver_host
  name: Print variables defined earlier
  remote_user: root

  tasks:
    - name: Create a temporary file
      tempfile:
        state: file
      register: temp_file

    - debug: var=temp_file.path

- hosts: nfs_server
  name: Print variables defined earlier
  remote_user: root

  tasks:
    # How should I refer to temp_file.path using a variable??
    - debug: var=hostvars[" {{ webserver }}"]['temp_file']['path']
运行
topmost.yml的输出

[build@localhost ~]$ ansible-playbook -i 172.100.139.200,10.193.219.244 topmost.yml   
[DEPRECATION WARNING]: 'include' for playbook includes. You should use 'import_playbook' instead. This feature will be removed in version 
2.8. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.

PLAY [Define hosts] ***********************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************
ok: [10.193.219.244]
ok: [172.100.139.200]

TASK [Add webserver] **********************************************************************************************************************
changed: [172.100.139.200]

TASK [Add nfs server] *********************************************************************************************************************
changed: [10.193.219.244]

PLAY [Print variables defined earlier] ****************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************
ok: [10.193.219.244]

TASK [Create a temporary file] ************************************************************************************************************
changed: [10.193.219.244]

TASK [debug] ******************************************************************************************************************************
ok: [10.193.219.244] => {
    "temp_file.path": "/tmp/ansible.VlvhO5"
}

PLAY [Print variables defined earlier] ****************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************
ok: [172.100.139.200]

TASK [debug] ******************************************************************************************************************************
fatal: [172.100.139.200]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'webserver' is undefined\n\nThe error appears to have been in '/home/build/test.yml': line 18, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n  tasks:\n    - debug: var=hostvars[\" {{ webserver }}\"]['temp_file']['path']\n      ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes.  Always quote template expression brackets when they\nstart a value. For instance:\n\n    with_items:\n      - {{ foo }}\n\nShould be written as:\n\n    with_items:\n      - \"{{ foo }}\"\n"}
        to retry, use: --limit @/home/build/topmost.retry

PLAY RECAP ********************************************************************************************************************************
10.193.219.244             : ok=5    changed=2    unreachable=0    failed=0   
172.100.139.200            : ok=3    changed=1    unreachable=0    failed=1   

首先,您的
debug
任务语法错误,应该是:

debug:
  msg: "{{ hostvars[webserver]['temp_file']['path'] }}"
其次,您在第一个重头戏中定义了
webserver
变量,在该重头戏之外它是不可用的

您需要将变量传递到内存中的资源清册,以便能够从其他重头戏中引用其值:

name: Add nfs server
add_host:
  hostname: "{{ nfs }}"
  groups: "nfs_server"
  webserver: "{{ webserver }}"

如果用例是配置所有主机,那么选项是循环组webserver\u主机


非常感谢您的解决方案。这是毒品。
- hosts: nfs_server
  name: Print variables defined earlier
  tasks:
- debug: var=hostvars["{{ item }}"]['temp_file']['path']
  loop: "{{ hostvars['localhost']['groups']['webserver_host'] }}"
PLAY [Print variables defined earlier]     
*************************************************************************
TASK [debug] 
*************************************************************************
ok: [192.168.1.13] => (item=None) => {
"hostvars[\"192.168.1.12\"]['temp_file']['path']": "/tmp/ansible.DZIGlA"
}