Ansible任务中when条件结合item使用用户输入变量的问题
Ansible Playbook中结合var_prompt变量与item使用when条件的问题解决
问题说明
使用Ansible Playbook获取VMware数据中心信息时,原Playbook的debug任务when条件可正常匹配数据中心名称,但通过var_prompt获取用户输入的变量后,debug任务的when条件无法正确匹配,导致任务被跳过。需要解决如何在when条件中结合item正确使用用户输入的变量。
原Playbook及执行结果
原Playbook(可正常匹配):
--- - hosts: localhost vars_files: 1credentials.yml tasks: - name: Gather information about all datacenters community.vmware.vmware_datacenter_info: hostname: '{{ vcenter_hostname }}' username: '{{ vcenter_username }}' password: '{{ vcenter_password }}' validate_certs: no delegate_to: localhost register: datacenter - debug: msg: "{{ item.name }}" loop: "{{ datacenter.datacenter_info }}" when: - item.name is defined - item.name == datacenter
执行输出:
PLAY [localhost] ******************************************************************************************************************************************************* TASK [Gathering Facts] ************************************************************************************************************************************************* ok: [localhost] TASK [Gather information about all datacenters] ************************************************************************************************************************ ok: [localhost] TASK [debug] *********************************************************************************************************************************************************** skipping: [localhost] => (item={'name': 'Datacenter-Test', 'moid': 'datacenter-1247', 'config_status': 'gray', 'overall_status': 'gray'}) ok: [localhost] => (item={'name': 'opendc-rookie', 'moid': 'datacenter-2', 'config_status': 'gray', 'overall_status': 'gray'}) => { "msg": "opendc-rookie" } PLAY RECAP ************************************************************************************************************************************************************* localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
修改后Playbook及问题
添加var_prompt后的Playbook(执行时任务被跳过):
--- - hosts: localhost vars_files: 1credentials.yml vars_prompt: - name: datacenter prompt: mention the datacenter name private: no tasks: - name: Gather information about all datacenters community.vmware.vmware_datacenter_info: hostname: '{{ vcenter_hostname }}' username: '{{ vcenter_username }}' password: '{{ vcenter_password }}' validate_certs: no delegate_to: localhost register: datacenter - debug: msg: "{{ item.name }}" loop: "{{ datacenter.datacenter_info }}" when: - item.name is defined - item.name == "datacenter"
执行输出:
mention the datacenter name: opendc-rookie PLAY [localhost] ******************************************************************************************************************************************************* TASK [Gathering Facts] ************************************************************************************************************************************************* ok: [localhost] TASK [Gather information about all datacenters] ************************************************************************************************************************ ok: [localhost] TASK [debug] *********************************************************************************************************************************************************** skipping: [localhost] => (item={'name': 'Datacenter-Test', 'moid': 'datacenter-1247', 'config_status': 'gray', 'overall_status': 'gray'}) skipping: [localhost] => (item={'name': 'opendc-rookie', 'moid': 'datacenter-2', 'config_status': 'gray', 'overall_status': 'gray'}) skipping: [localhost] PLAY RECAP ************************************************************************************************************************************************************* localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
问题原因及解决方法
核心问题
- 变量名冲突:
var_prompt定义的变量名datacenter和后面任务register的变量名完全一致,导致用户输入的变量值被任务执行结果覆盖,when条件实际匹配的是register返回的复杂对象,而非用户输入的字符串。 - when条件写法错误:将变量名用双引号包裹,
item.name == "datacenter"会把"datacenter"当成固定字符串去匹配,而非引用用户输入的变量。
修正方案
- 重命名
var_prompt的变量名,避免和register的变量名冲突,比如改为target_datacenter。 - when条件中直接使用变量名,不要加引号,即
item.name == target_datacenter。
修正后的Playbook及执行效果
修正后的Playbook:
--- - hosts: localhost vars_files: 1credentials.yml vars_prompt: - name: target_datacenter prompt: 请输入数据中心名称 private: no tasks: - name: 收集所有数据中心信息 community.vmware.vmware_datacenter_info: hostname: '{{ vcenter_hostname }}' username: '{{ vcenter_username }}' password: '{{ vcenter_password }}' validate_certs: no delegate_to: localhost register: datacenter_info - debug: msg: "{{ item.name }}" loop: "{{ datacenter_info.datacenter_info }}" when: - item.name is defined - item.name == target_datacenter
执行效果说明
执行时输入目标数据中心名称(比如opendc-rookie),debug任务会正确匹配对应的数据中心,输出其名称,不会被跳过。
内容的提问来源于stack exchange,提问作者pugazhendhi_r
相关产品推荐
相关产品推荐

