ansible - 当条件与“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_host
变量上。这是因为您正在尝试匹配hostname
,ansible_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"
推荐阅读
- python - DataFrame.to_csv() 在传递压缩参数时表现不同
- spring - Springboot JPA(Hibernate)没有按名称获取表和列
- python - Python猜2个数字游戏
- entity-framework-core - 多个使用 EFCore 持久性的自动匿名状态机不起作用
- r - 将变量函数标签添加到 R 中的三元图
- javascript - 调用 foreach 后我得到未定义
- ios - typeText 方法的 SwiftUI TextEditor UI 测试失败
- azure - Azure 应用服务计划横向扩展更改事件
- python - 在两个类“QWidget”之间共享属性
- excel - 如果值与上一行中的值不同,则为单元格分配背景颜色