首页 > 解决方案 > 当条件与“ansible_host” Ansible 2.10 不匹配时?

问题描述

我尝试了一些非常简单的方法,但它不起作用:

如果两个字符串相同,我只想执行一个任务:

主机名=1-欧洲-ECV-Site2 ansible_host=1-欧洲-ECV-Site2

这是剧本:

- debug: var=hostname
- debug: var=ansible_host

- name: Gather a virtual machine info
  vmware_guest_info:
    hostname: '{{ vsphere_host }}'
    username: '{{ vsphere_user }}'
    password: '{{ vsphere_password }}'
    validate_certs: false
    datacenter: "{{ vsphere_datacenter }}"
    name: "{{ hostname }}"
    schema: vsphere
    properties:
      - guest.ipAddress
  retries: 60
  delay: 10
  until: gather_vm_info.instance.guest.ipAddress is not none
  register: gather_vm_info
  delegate_to: localhost
  when: ansible_host == hostname

我得到了这个结果,即使我可以通过调试看到两个值相等:

PLAY [Configure vyOS BGP LAN Router] *****************************************************************************************************************************************************

TASK [get_ip_vsphere : debug] ************************************************************************************************************************************************************
ok: [1-Europe-ECV-Site2] => 
  hostname: 1-Europe-ECV-Site2
ok: [1-Europe-ECV-Site3] => 
  hostname: 1-Europe-ECV-Site3
ok: [1-Europe-ECV-Site1-1] => 
  hostname: 1-Europe-ECV-Site1-1
ok: [1-Europe-ECV-Site1-2] => 
  hostname: 1-Europe-ECV-Site1-2

TASK [get_ip_vsphere : debug] ************************************************************************************************************************************************************
ok: [1-Europe-ECV-Site2] => 
  ansible_host: 1-Europe-ECV-Site2
ok: [1-Europe-ECV-Site3] => 
  ansible_host: 1-Europe-ECV-Site3
ok: [1-Europe-ECV-Site1-1] => 
  ansible_host: 1-Europe-ECV-Site1-1
ok: [1-Europe-ECV-Site1-2] => 
  ansible_host: 1-Europe-ECV-Site1-2

TASK [get_ip_vsphere : Gather a virtual machine info] ************************************************************************************************************************************
skipping: [1-Europe-ECV-Site2]
skipping: [1-Europe-ECV-Site3]
skipping: [1-Europe-ECV-Site1-1]
skipping: [1-Europe-ECV-Site1-2]

TASK [get_ip_vsphere : Gather a virtual machine info] ************************************************************************************************************************************
skipping: [1-Europe-ECV-Site2]
skipping: [1-Europe-ECV-Site3]
skipping: [1-Europe-ECV-Site1-1]
skipping: [1-Europe-ECV-Site1-2]

问题:为什么这个条件不匹配?如果我想使用内置变量ansible_host我使用 Ansible 2.10 ,是否需要做一些特别的事情

编辑:

此 playbook 的目的是仅将发现的 IP 地址写入 host_vars 下的清单 YAML 文件(如果尚未定义)。如您在我的示例中所见,如果未检测到 IP 地址,则 ansible_host 等于 VM 的主机名。

为此,我必须检查是否ansible_host == 主机名,然后才必须获取 IP 并将其写入 YAML 库存文件。

另一种解决方案是检查 192.168。不包含在“ansbile_host”中,然后执行任务:

when: "'192.168' in ansible_host"

但这也行不通!

我想念什么?

标签: ansible

解决方案


我不认为问题出在ansible_host变量上。这是因为您正在尝试匹配hostnameansible_host理想情况下应该每次都匹配(查看debug输出)。

例如,当任务vmware_guest_info运行时1-Europe-ECV-Site3

hostname == ansible_host

将会:

"1-Europe-ECV-Site3" == "1-Europe-ECV-Site3"
#=> True

这不是你想要的,即使它运行。如果您希望任务仅在ansible_hostname匹配时运行1-Europe-ECV-Site2,则条件应为:

- name: Gather a virtual machine info
  vmware_guest_info:
    hostname: '{{ vsphere_host }}'
    username: '{{ vsphere_user }}'
    password: '{{ vsphere_password }}'
    validate_certs: false
    datacenter: "{{ vsphere_datacenter }}"
    name: "{{ hostname }}"
    schema: vsphere
    properties:
      - guest.ipAddress
  retries: 60
  delay: 10
  until: gather_vm_info.instance.guest.ipAddress is not none
  register: gather_vm_info
  delegate_to: localhost
  when: ansible_hostname == "1-Europe-ECV-Site2"

推荐阅读