将变量与Ansible中的事实连接起来

时间:2018-04-13 12:33:05

标签: ansible

我正在尝试使用变量和前一个任务的结果作为输入。这是我的代码。

---
- hosts: localhost
  vars:
    v_hostname: XXXXXXXXXXXXX
    v_datacentre: DC-UK-LON-GS
    v_username: YYYYYYYY
    v_password: XXXXXXXX
  connection: local
  gather_facts: false

  tasks:
  - name: Find the folder for the VM
    vmware_guest_find:
      hostname: "{{ v_hostname }}"
      datacenter: "{{ v_datacentre }}"
      username: "{{ v_username }}"
      password: "{{ v_password }}"
      validate_certs: no
      name: MYVMNAME
    register: folder

   - debug:
       msg:
         - "{{ folder.folders }}"

   - name: Gather facts from VM
     connection: local
     gather_facts: false
     vmware_guest_facts:
       hostname: "{{ v_hostname }}"
       datacenter: "{{ v_datacentre }}"
       username: "{{ v_username }}"
       password: "{{ v_password }}"
       validate_certs: no
       folder: "{{ v_datacentre }}{{ folder.folders }}"
       name: MYVMNAME

最后一行尝试使用已定义的变量v_datacentre,并附加上一个任务中已注册事实的值。但是,连接会产生一些不需要的包裹字符:

ansible-playbook 2.4.2.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/export/home/ansible/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /bin/ansible-playbook
  python version = 2.7.5 (default, May  3 2017, 07:55:04) [GCC 4.8.5 20150623 (Red Hat 4.8.5-14)]
Using /etc/ansible/ansible.cfg as config file
Parsed /export/home/ansible/inventory_prod inventory source with ini plugin
 [WARNING]: Ignoring invalid attribute: gather_facts

PLAYBOOK: mainvm.yml *****************************************************************************************************************************************
1 plays in mainvm.yml

PLAY [localhost] *********************************************************************************************************************************************
META: ran handlers

TASK [TEST1] *************************************************************************************************************************************************
task path: /export/home/ansible/mainvm.yml:12
Using module file /usr/lib/python2.7/site-packages/ansible/modules/cloud/vmware/vmware_guest_find.py
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: ansible
<127.0.0.1> EXEC /bin/sh -c 'echo ~ && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /export/home/ansible/.ansible/tmp/ansible-tmp-1523621823.18-119950066241401 `" && echo ansible-tmp-1523621823.18-119950066241401="` echo /export/home/ansible/.ansible/tmp/ansible-tmp-1523621823.18-119950066241401 `" ) && sleep 0'
<127.0.0.1> PUT /tmp/tmpM_Pf2B TO /export/home/ansible/.ansible/tmp/ansible-tmp-1523621823.18-119950066241401/vmware_guest_find.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /export/home/ansible/.ansible/tmp/ansible-tmp-1523621823.18-119950066241401/ /export/home/ansible/.ansible/tmp/ansible-tmp-1523621823.18-119950066241401/vmware_guest_find.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python2 /export/home/ansible/.ansible/tmp/ansible-tmp-1523621823.18-119950066241401/vmware_guest_find.py; rm -rf "/export/home/ansible/.ansible/tmp/ansible-tmp-1523621823.18-119950066241401/" > /dev/null 2>&1 && sleep 0'
ok: [localhost] => {
    "changed": false,
    "folders": [
        "/vm/LON-GS-AD/LON-GS-AD-UNIX"
    ],
    "invocation": {
        "module_args": {
            "datacenter": "DC-UK-LON-GS",
            "hostname": "XXXXXXXXXXXXX",
            "name": "MYVMNAME",
            "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "username": "XXXXXXXX",
            "uuid": null,
            "validate_certs": false
        }
    }
}

TASK [debug] *************************************************************************************************************************************************
task path: /export/home/ansible/mainvm.yml:23
ok: [localhost] => {
    "msg": [
        [
            "/vm/LON-GS-AD/LON-GS-AD-UNIX"
        ]
    ]
}

TASK [Gather facts from standalone ESXi server] **************************************************************************************************************
task path: /export/home/ansible/mainvm.yml:28
Using module file /usr/lib/python2.7/site-packages/ansible/modules/cloud/vmware/vmware_guest_facts.py
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: ansible
<127.0.0.1> EXEC /bin/sh -c 'echo ~ && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /export/home/ansible/.ansible/tmp/ansible-tmp-1523621827.79-78942665802166 `" && echo ansible-tmp-1523621827.79-78942665802166="` echo /export/home/ansible/.ansible/tmp/ansible-tmp-1523621827.79-78942665802166 `" ) && sleep 0'
<127.0.0.1> PUT /tmp/tmp43NXf_ TO /export/home/ansible/.ansible/tmp/ansible-tmp-1523621827.79-78942665802166/vmware_guest_facts.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /export/home/ansible/.ansible/tmp/ansible-tmp-1523621827.79-78942665802166/ /export/home/ansible/.ansible/tmp/ansible-tmp-1523621827.79-78942665802166/vmware_guest_facts.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python2 /export/home/ansible/.ansible/tmp/ansible-tmp-1523621827.79-78942665802166/vmware_guest_facts.py; rm -rf "/export/home/ansible/.ansible/tmp/ansible-tmp-1523621827.79-78942665802166/" > /dev/null 2>&1 && sleep 0'
fatal: [localhost]: FAILED! => {
    "changed": false,
    "invocation": {
        "module_args": {
            "datacenter": "XXXXXX",
            "folder": "DC-UK-LON-GS[u'/vm/LON-GS-AD/LON-GS-AD-UNIX']",
            "hostname": "XXXXXXXXXXXXX",
            "name": "MYVMNAME",
            "name_match": "first",
            "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "username": "XXXXXXXX",
            "uuid": null,
            "validate_certs": false
        }
    },
    "msg": "Unable to gather facts for non-existing VM MYVMNAME"
}
        to retry, use: --limit @/export/home/ansible/mainvm.retry

PLAY RECAP ***************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=1

通过调试正确打印folder.folders的结果,但在最后一步中,连接给出了:

"folder": "DC-UK-LON-GS[u'/vm/LON-GS-AD/LON-GS-AD-UNIX']",

我承认自己是一个可靠的新手,但我不明白[u&#39;来自。我只想使用组合字符串作为输入。任何人都可以解释或显示解决方案或指出一些初学者文档,以便进行这种变量使用吗?

感谢。

1 个答案:

答案 0 :(得分:1)

所以经过一些进一步的播放后,vmware_guest_find返回一个列表(并且它在文档中!!!)所以我需要选择第一个项目,如果只有一个组或循环它们。

folder: "{{ v_datacentre }}{{ folder.folders[0] }}"

这样做了。