使用变量名而不是字符串引用ansible的hostvars词典中的键

时间:2018-08-22 05:17:59

标签: ansible

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

topmost.yml包括两个文件:

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

- include: test.yml

top.yml定义了两个变量,并在每个变量上调用add_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_host上),然后在nfs_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   

2 个答案:

答案 0 :(得分:2)

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

debug:
  msg: "{{ hostvars[webserver]['temp_file']['path'] }}"

第二,您在第一局中定义了webserver变量,该变量在该局之外不可用。

您需要将变量传递到内存清单中,以便能够从其他游戏中引用其值:

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

答案 1 :(得分:1)

如果用例将配置所有主机,则可以选择将组 webserver_host 循环。

<div class="wrap-search-header flex-w p-l-15"  style="height: 50px;width: 350px;">
    <input class="plh3 search-input" type="text" name="search"   placeholder="Search...">
    <button class="flex-c-m trans-04 hov-btn1 search-btn">
        <i class="zmdi zmdi-search"></i>
    </button>
</div>

$('.search-btn').on('click', function(){ 
    var searchinput = $('.search-input').value;
    console.log(searchinput)
})