我正在使用以下行动手册来捕获vmware数据中心信息,该信息运行良好,没有任何问题:
---
- 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
但是,当我尝试使用var_prompt并请求变量数据中心的用户输入时,如下所示:
---
- 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"
它跳过调试任务,而不替换数据中心变量的值,该值是用户在when条件中的输入。请建议我如何将变量值和when条件包含在一起。以下是跳过变量的输出
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的名称相同(
- 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: datacenterX
- debug:
msg: "{{ item.name }}"
loop: "{{ datacenterX.datacenter_info }}"
when:
- item.name is defined
- item.name == datacenter
在跟踪中,我们可以看到debug
任务正在使用键(或属性(name = "Datacenter-Test"
和name = "opendc-rookie"
对2个dict
项进行迭代。由于您在item.name == "datacenter"
上有一个条件,因此该条件仅适用于具有属性"name=&"的项;数据中心";。这不是你想要实现的。
如何添加列表值"数据中心测试;以及";opendc菜鸟;作为条件中的变量
这里有两个选项可以过滤数据中心列表,以保留两个选定的名称。
- "简单条件":在CCD_ 7条件下使用简单的CCD_
- "列表条件":直接过滤列表以迭代列表
wanted_datacenters
中定义的项目。此选项适用于输入ansible-playbook loop.yml --extra-vars='{"wanted_datacenters": ["opendc-rookie", "Datacenter-Test"]}'
- hosts: localhost
vars:
datacenter:
- name: opendc-rookie
- name: Datacenter-Test
wanted_datacenters:
- opendc-rookie
- Datacenter-Test
tasks:
- name: "simple condition"
debug:
msg: "{{ item.name }}"
loop: "{{ datacenter }}"
when: item.name == "Datacenter-Test" or item.name == "opendc-rookie"
- name: "list condition"
debug:
msg: "{{ item.name }}"
loop: "{{ datacenter|selectattr('name', 'in', wanted_datacenters)|list }}"
- 使用json_query。当属性丢失时,此筛选器不会失败。例如
shell> cat pb.yml
- hosts: localhost
vars:
datacenter:
datacenter_info:
- config_status: gray
moid: datacenter-1247
name: Datacenter-Test
overall_status: gray
- config_status: gray
moid: datacenter-2
name: opendc-rookie
overall_status: gray
tasks:
- debug:
msg: "{{ _item }}"
vars:
_item: "{{ datacenter.datacenter_info|json_query(_query) }}"
_query: '[?name == `{{ dc_name|d("") }}`]'
给出
shell> ansible-playbook pb.yml -e dc_name=Datacenter-Test
PLAY [localhost] *****************************************************************************
TASK [debug] *********************************************************************************
ok: [localhost] =>
msg:
- config_status: gray
moid: datacenter-1247
name: Datacenter-Test
overall_status: gray
shell> ansible-playbook pb.yml
PLAY [localhost] *****************************************************************************
TASK [debug] *********************************************************************************
ok: [localhost] =>
msg: []
- 您可以迭代列表。例如
- debug:
msg: "{{ item.name }}"
loop: "{{ datacenter.datacenter_info|json_query(_query) }}"
vars:
_query: '[?name == `{{ dc_name|d("") }}`]'
- 如果要测试名称在列表中,请创建一个交集。例如
shell> cat pb.yml
- hosts: localhost
vars:
datacenter:
datacenter_info:
- config_status: gray
moid: datacenter-1247
name: Datacenter-Test
overall_status: gray
- config_status: gray
moid: datacenter-2
name: opendc-rookie
overall_status: gray
names: "{{ datacenter.datacenter_info|json_query('[].name') }}"
dc_names: ''
names_selected: "{{ names|intersect(dc_names.split(',')) }}"
tasks:
- debug:
msg: "{{ item.name }}"
loop: "{{ datacenter.datacenter_info }}"
when: item.name|d('') in names_selected
给出
shell> ansible-playbook pb.yml -e dc_names=Datacenter-Test,opendc-rookie
PLAY [localhost] *****************************************************************************
TASK [debug] *********************************************************************************
ok: [localhost] => (item={'config_status': 'gray', 'moid': 'datacenter-1247', 'name': 'Datacenter-Test', 'overall_status': 'gray'}) =>
msg: Datacenter-Test
ok: [localhost] => (item={'config_status': 'gray', 'moid': 'datacenter-2', 'name': 'opendc-rookie', 'overall_status': 'gray'}) =>
msg: opendc-rookie
shell> ansible-playbook pb.yml -e dc_names=opendc-rookie
PLAY [localhost] *****************************************************************************
TASK [debug] *********************************************************************************
skipping: [localhost] => (item={'config_status': 'gray', 'moid': 'datacenter-1247', 'name': 'Datacenter-Test', 'overall_status': 'gray'})
ok: [localhost] => (item={'config_status': 'gray', 'moid': 'datacenter-2', 'name': 'opendc-rookie', 'overall_status': 'gray'}) =>
msg: opendc-rookie
shell> ansible-playbook
PLAY [localhost] *****************************************************************************
TASK [debug] *********************************************************************************
skipping: [localhost] => (item={'config_status': 'gray', 'moid': 'datacenter-1247', 'name': 'Datacenter-Test', 'overall_status': 'gray'})
skipping: [localhost] => (item={'config_status': 'gray', 'moid': 'datacenter-2', 'name': 'opendc-rookie', 'overall_status': 'gray'})
skipping: [localhost]