Ansible 使用json在变量构造上找不到值时跳过任务

Ansible 使用json在变量构造上找不到值时跳过任务,ansible,Ansible,剧本是: --- - hosts: ahgroup become: yes vars: volumes_group_name: "{{ ansible_hostname|upper }}_VG01" device: /dev/sdc partition_number: 1 tasks: - name: Create partition parted: device: "{{device}}"

剧本是:

---
- hosts: ahgroup
  become: yes
  vars:
    volumes_group_name: "{{ ansible_hostname|upper }}_VG01"
    device: /dev/sdc
    partition_number: 1

  tasks:
  - name: Create partition
    parted:
     device: "{{device}}"
     number: "{{partition_number}}"
     label: gpt
     state: present

  - name: Check pvs busy
    shell: pvs --noheadings "{{device}}{{partition_number}}" --reportformat json 2>/dev/null
    register: vgmon

  - debug: msg="{{ vgmon.stdout }}"

  - name: Create volumes group
    lvg:
     vg: "{{volumes_group_name}}"
     pvs: "{{device}}{{partition_number}}"
     pesize: "4"
    when: (vgmon.stdout | from_json).report[].pv[].vg_name == ''
如果此PV上已存在卷组,则需要跳过此任务。 在“Check pvs busy”(检查pvs忙碌)任务中使用的命令在配置VG时生成输出:

# pvs --noheadings /dev/sdc1 --reportformat json 2>/dev/null
  {
      "report": [
          {
              "pv": [
                  {"pv_name":"/dev/sdc1", "vg_name":"DATA_VG01", "pv_fmt":"lvm2", "pv_attr":"a--", "pv_size":"<256.00g", "pv_free":"<101.00g"}
              ]
          }
      ]
  }
比如说

-调试:
msg:{vgmon.stdout.report | json_查询('[].pv[].vg_name')}
给予

msg:
-数据单元VG01
使用此列表测试项目,例如:

-调试:
msg:“{volumes\u group\u name}}在列表中。”
时间:vgmon.stdout.report中的卷\组\名称|
json_查询('[].pv[].vg_名称')
变量:
卷\组\名称:数据\ VG01
给予

msg:DATA\u VG01在列表中。
将跳过此任务

-调试:
msg:“{volumes\u group\u name}}不在列表中。”
何时:卷\组\名称不在vgmon.stdout.report中|
json_查询('[].pv[].vg_名称')
变量:
卷\组\名称:数据\ VG01

(作为旁注)

让你的生活更轻松,使用yaml回调插件,例如

-调试:
变量:vgmon.stdout
给予

任务[调试]*************************************************************
确定:[本地主机]=>
vgmon.stdout:
报告:
-pv:
-pv_attr:a--
pv_fmt:lvm2

pv_free:不幸的是,它仍然失败,出现错误:{“msg”:“条件检查”卷组名称不在vgmon.stdout.report | json_查询('[].pv[].vg_名称')失败。错误是:{如果卷组名称不在vgmon.stdout.report | json_查询('[].pv[].vg_名称')%}真{%else%}假{%endif%}上发生意外的模板类型错误):类型为“NoneType”的参数不可编辑
$ ansible-playbook test2.yaml

PLAY [databases] ***********************************************************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************************************************
Friday 28 May 2021  16:10:45 +0200 (0:00:00.070)       0:00:00.070 ************
ok: [192.168.1.101]

TASK [Create partition] ****************************************************************************************************************************************************
Friday 28 May 2021  16:10:48 +0200 (0:00:03.134)       0:00:03.204 ************
ok: [192.168.1.101]

TASK [Check pvs busy] ******************************************************************************************************************************************************
Friday 28 May 2021  16:10:49 +0200 (0:00:01.203)       0:00:04.407 ************
changed: [192.168.1.101]

TASK [debug] ***************************************************************************************************************************************************************
Friday 28 May 2021  16:10:50 +0200 (0:00:01.132)       0:00:05.540 ************
ok: [192.168.1.101] => {
    "msg": "  {\n      \"report\": [\n          {\n              \"pv\": [\n                  {\"pv_name\":\"/dev/sdc1\", \"vg_name\":\"DATA_VG01\", \"pv_fmt\":\"lvm2\", \"pv_attr\":\"a--\", \"pv_size\":\"<256.00g\", \"pv_free\":\"<101.00g\"}\n              ]\n          }\n      ]\n  }"
}

TASK [Create volumes group] ************************************************************************************************************************************************
Friday 28 May 2021  16:10:50 +0200 (0:00:00.121)       0:00:05.662 ************
fatal: [192.168.1.101]: FAILED! => {"msg": "The conditional check '(vgmon.stdout | from_json).report[].pv[].vg_name == ''' failed. The error was: error while evaluating conditional ((vgmon.stdout | from_json).report[].pv[].vg_name == ''): list object has no element ()\n\nThe error appears to be in '/home/otds/ansible-pipeline/test2.yaml': line 24, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n  - name: Create volumes group\n    ^ here\n"}