当条件与“不匹配时”;ansible“主机”;Ansible 2.10?

当条件与“不匹配时”;ansible“主机”;Ansible 2.10?,ansible,Ansible,我尝试了一些非常简单的方法,但不起作用: 我只想在两个字符串相同的情况下执行任务: 主机名=1-Europe-ECV-Site2 ansible_主机=1-Europe-ECV-Site2 这是剧本: - debug: var=hostname - debug: var=ansible_host - name: Gather a virtual machine info vmware_guest_info: hostname: '{{ vsphere_host }}' us

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

我只想在两个字符串相同的情况下执行任务:

主机名=1-Europe-ECV-Site2 ansible_主机=1-Europe-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\U host,我需要做一些特殊的事情吗?我使用ansible 2.10

编辑:

本手册的目的是仅将发现的IP地址写入host_vars下的清单YAML文件(如果尚未定义)。在我的示例中可以看到,如果没有检测到IP地址,ansible_主机等于VM的主机名

为此,我必须检查是否ansible_host==hostname,只有这样才能获取IP并将其写入YAML清单文件

另一个解决方案是检查192.168是否正确。不包括在“ansbile_主机”中,然后执行任务:

when: "'192.168' in ansible_host"
但这也不行


我遗漏了什么?

我认为问题不在于
ansible\u host
变量。这是因为您正试图将
主机名
ansible\u主机
匹配,每次都应该匹配(查看
调试
输出)

例如,当任务
vmware\u guest\u info
1-Europe-ECV-Site3
上运行时:

hostname == ansible_host
将是:

“1-Europe-ECV-Site3”=“1-Europe-ECV-Site3”
#=>正确
这不是你想要的,即使它运行。如果希望任务仅在
ansible\u主机名
匹配
1-Europe-ECV-Site2
时运行,则条件应为:

-名称:收集虚拟机信息
vmware\u来宾\u信息:
主机名:“{vsphere_host}}”
用户名:“{vsphere_user}}”
密码:“{vsphere_password}}”
验证证书:错误
数据中心:{{vsphere_数据中心}}”
名称:“{{hostname}}”
模式:vsphere
特性:
-guest.ipAddress
重试次数:60次
延误:10
直到:gather\u vm\u info.instance.guest.ipAddress不是none
注册:收集虚拟机信息
委托给:localhost
时间:ansible_主机名==“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: "'192.168' not in 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 != "0.0.0.0"
  register: gather_vm_info
  delegate_to: localhost
  when: "'192.168' not in ansible_host" 


- debug: var=gather_vm_info.instance.guest.ipAddress


- name: Update IP
  block:
    - name: Add ansible_host IP to the YAML inventory file
      delegate_to: localhost
      lineinfile:
        path: host_vars/{{ hostname }}.yml
        line: "  ansible_host: {{ gather_vm_info.instance.guest.ipAddress }}"

    - name: Update dynamic ansible_host with new IP Address
      set_fact: 
        ansible_host: "{{ gather_vm_info.instance.guest.ipAddress }}"
        ansible_hostname: "{{ hostname }}"

  when: "'192.168' not in ansible_host"

我试过几次,它都不起作用,即使你“硬编码”主机名。为了澄清,我编辑了我的原始问题。请看看上面,谢谢你的帮助!!