Ansible - 使用循环变量设置事实?

Ansible - Set-fact with loop variable?

老实说,我不太确定我正在尝试做的事情在技术上叫做什么,但我会尽力解释。

End_result:

我想要一个列表 and/or JSON 看起来类似于这样的数据:

"ansible_facts": {
        "fact_pcs_resource_status": [
            {
                "resource": "nfs-transfer01_18",
                "type": "(ocf::heartbeat:exportfs)",
                "state": "Started",
                "target": "testserver07.local.net"
            },
            {
                "resource": "nfs-transfer03_18",
                "type": "(ocf::heartbeat:exportfs)",
                "state": "Started",
                "target": "testserver07.local.net"
            },
            {
                "resource": "nfs-transfer01_19",
                "type": "(ocf::heartbeat:exportfs)",
                "state": "Started",
                "target": "testserver07.local.net"
            }

我卡在哪里(解释):

逻辑思维

- name: set_fact_pcs_status
  ansible.builtin.set_fact:
    fact_pcs_status: "{{ fact_pcs_status|default([]) + [ {'resource': item.0.name, 'type': item.1, 'state': item.2, 'target': item.3 } ] }}"
  with_together:
    - "{{ var_pcs_resources }}"
    - "{{ type }}"
    - "{{ state }}"
    - "{{ target }}"
  vars:
    regex_expression: "{{ var_pcs_resources.name }}\s+(\(.*\)):\s+(\S*)\s+(\S*)\s+(\(.*\)):\s+(\S*)\s+(\S*)"
    type: "{{ reg_command_pcs_status.stdout | regex_findall(regex_expression, '\1') }}"
    state: "{{ reg_command_pcs_status.stdout | regex_findall(regex_expression, '\2') }}"
    target: "{{ reg_command_pcs_status.stdout | regex_findall(regex_expression, '\3') }}"

现在上面的方法不起作用,我迷路了。我相信我真正需要的是一个循环来完成我想要的 'loop_control' 但不确定。

所以在'thought'代码中:

  1. 从变量中提取资源名称'var_pcs_resources'
  2. 将其用作循环索引(因为应该只有这些资源 在 PCS 上配置)
  3. RegEx通过'reg_command_pcs_status.stdout'找到'类型, 状态和目标'
  4. 循环的每次迭代都会改变'regex_express'“{{ var_pcs_resources.name }}" 部分

我试图将其分解为不同的事实 captures/creations 但我无法超越需要将变量动态调用到 regex_expression.

的地步

信息:

Ansible:

ansible 2.10.7
  config file = /app/ansible/ansible_linux_playbooks/ansible.cfg
  configured module search path = ['/etc/ansible/library/modules']
  ansible python module location = /usr/local/lib/python3.6/site-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 3.6.8 (default, Aug 24 2020, 17:57:11) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]

Raw_data:

PCS-status 命令输出:

TASK [vcd_nfs_cluster : command_pcs_status] ***************************************************************************************************************************************************
task path: /app/ansible/ansible_linux_playbooks/roles/vcd_nfs_cluster/tasks/verify_pcs_status.yml:3
Wednesday 07 April 2021  09:12:20 -0400 (0:00:01.065)       0:00:04.597 ******* 
ok: [testserver06.local.net] => {
    "changed": false,
    "cmd": [
        "pcs",
        "status"
    ],
    "delta": "0:00:00.273728",
    "end": "2021-04-07 09:12:22.729576",
    "rc": 0,
    "start": "2021-04-07 09:12:22.455848"
}

STDOUT:

Cluster name: nfsclst02
Stack: corosync
Current DC: testserver07.local.net (version 1.1.23-1.el7_9.1-9acf116022) - partition with quorum
Last updated: Wed Apr  7 09:12:22 2021
Last change: Wed Mar 31 10:33:35 2021 by root via cibadmin on testserver06.local.net

2 nodes configured
16 resource instances configured

Online: [ testserver06.local.net testserver07.local.net ]

Full list of resources:

 nfsclst02      (stonith:fence_vmware_soap):    Started testserver06.local.net
 Resource Group: nfsclst02grp01
     nfsclst02-lvm      (ocf::heartbeat:LVM):   Started testserver07.local.net
     lvm_transfer       (ocf::heartbeat:Filesystem):    Started testserver07.local.net
     nfs-daemon (ocf::heartbeat:nfsserver):     Started testserver07.local.net
     nfs-transfer01_18  (ocf::heartbeat:exportfs):      Started testserver07.local.net
     nfs-transfer03_18  (ocf::heartbeat:exportfs):      Started testserver07.local.net
     nfs-transfer01_19  (ocf::heartbeat:exportfs):      Started testserver07.local.net
     nfs-transfer03_19  (ocf::heartbeat:exportfs):      Started testserver07.local.net
     nfs-transfer01_20  (ocf::heartbeat:exportfs):      Started testserver07.local.net
     nfs-transfer03_20  (ocf::heartbeat:exportfs):      Started testserver07.local.net
     nfs-transfer01_21  (ocf::heartbeat:exportfs):      Started testserver07.local.net
     nfs-transfer03_21  (ocf::heartbeat:exportfs):      Started testserver07.local.net
     nfs-transfer01_22  (ocf::heartbeat:exportfs):      Started testserver07.local.net
     nfs-transfer03_22  (ocf::heartbeat:exportfs):      Started testserver07.local.net
     nfs_vip    (ocf::heartbeat:IPaddr2):       Started testserver07.local.net
     nfs-notify (ocf::heartbeat:nfsnotify):     Started testserver07.local.net

Daemon Status:
  corosync: active/enabled
  pacemaker: active/enabled
  pcsd: active/enabled

资源变量:

var_pcs_resources:

  - name: 'nfsclst02-lvm'
    type: 'ocf:heartbeat:LVM'
    opts: >-
      volgrpname=vg02_lvm_transfer
      exclusive=true
      --group nfsclst02grp01

  - name: 'lvm_transfer'
    type: 'ocf:heartbeat:Filesystem'
    opts: >-
      device=/dev/vg02_lvm_transfer/lvm_transfer
      directory=/storage/lvm_transfer
      fstype=ext4
      --group nfsclst02grp01

  - name: 'nfs-daemon'
    type: 'ocf:heartbeat:nfsserver'
    opts: >-
      nfs_shared_infodir=/storage/lvm_transfer/nfsinfo
      nfs_no_notify=true
      --group nfsclst02grp01

  - name: 'nfs-transfer01_18'
    type: 'ocf:heartbeat:exportfs'
    opts: >-
      --force
      clientspec={{ var_vcdc01_ip }}/32
      options=rw,sync,no_subtree_check,no_root_squash
      directory=/storage/lvm_transfer/exports/transfer01
      fsid=0
      --group nfsclst02grp01

  - name: 'nfs-transfer03_18'
    type: 'ocf:heartbeat:exportfs'
    opts: >-
      --force
      clientspec={{ var_vcdc01_ip }}/32
      options=rw,sync,no_subtree_check,no_root_squash
      directory=/storage/lvm_transfer/exports/transfer03
      fsid=1
      --group nfsclst02grp01

  - name: 'nfs-transfer01_19'
    type: 'ocf:heartbeat:exportfs'
    opts: >-
      --force
      clientspec={{ var_vcdc02_ip }}/32
      options=rw,sync,no_subtree_check,no_root_squash
      directory=/storage/lvm_transfer/exports/transfer01
      fsid=0
      --group nfsclst02grp01

  - name: 'nfs-transfer03_19'
    type: 'ocf:heartbeat:exportfs'
    opts: >-
      --force
      clientspec={{ var_vcdc02_ip }}/32
      options=rw,sync,no_subtree_check,no_root_squash
      directory=/storage/lvm_transfer/exports/transfer03
      fsid=1
      --group nfsclst02grp01

  - name: 'nfs-transfer01_20'
    type: 'ocf:heartbeat:exportfs'
    opts: >-
      --force
      clientspec={{ var_vcdc03_ip }}/32
      options=rw,sync,no_subtree_check,no_root_squash
      directory=/storage/lvm_transfer/exports/transfer01
      fsid=0
      --group nfsclst02grp01

  - name: 'nfs-transfer03_20'
    type: 'ocf:heartbeat:exportfs'
    opts: >-
      --force
      clientspec={{ var_vcdc03_ip }}/32
      options=rw,sync,no_subtree_check,no_root_squash
      directory=/storage/lvm_transfer/exports/transfer03
      fsid=1
      --group nfsclst02grp01

  - name: 'nfs-transfer01_21'
    type: 'ocf:heartbeat:exportfs'
    opts: >-
      --force
      clientspec={{ var_vcdc04_ip }}/32
      options=rw,sync,no_subtree_check,no_root_squash
      directory=/storage/lvm_transfer/exports/transfer01
      fsid=0
      --group nfsclst02grp01

  - name: 'nfs-transfer03_21'
    type: 'ocf:heartbeat:exportfs'
    opts: >-
      --force
      clientspec={{ var_vcdc04_ip }}/32
      options=rw,sync,no_subtree_check,no_root_squash
      directory=/storage/lvm_transfer/exports/transfer03
      fsid=1
      --group nfsclst02grp01

  - name: 'nfs-transfer01_22'
    type: 'ocf:heartbeat:exportfs'
    opts: >-
      --force
      clientspec={{ var_vcdc05_ip }}/32
      options=rw,sync,no_subtree_check,no_root_squash
      directory=/storage/lvm_transfer/exports/transfer01
      fsid=0
      --group nfsclst02grp01

  - name: 'nfs-transfer03_22'
    type: 'ocf:heartbeat:exportfs'
    opts: >-
      --force
      clientspec={{ var_vcdc05_ip }}/32
      options=rw,sync,no_subtree_check,no_root_squash
      directory=/storage/lvm_transfer/exports/transfer03
      fsid=1
      --group nfsclst02grp01

  - name: 'nfs_vip'
    type: 'ocf:heartbeat:IPaddr2'
    opts: "ip={{ var_nfsclst02_vip }} cidr_netmask=24 --group nfsclst02grp01"

  - name: 'nfs-notify'
    type: 'ocf:heartbeat:nfsnotify'
    opts: "source_host={{ var_nfsclst02_vip }} --group nfsclst02grp01"

好像有什么东西坏了:

  • 在你的正则表达式中
  • 你给循环带来的复杂性

使用 regex_findall 你可以获得所有匹配的列表,所以你不需要有多个 regex_findall.

结果列表实际上是一个包含所有匹配项的列表,然后对于每个匹配行,您使用正则表达式的捕获组捕获的片段。

所以,给定:

regex_expression: "{{ item.name }}\s+(\S+):\s+(\S+)\s+(\S+)"
matches: "{{ reg_command_pcs_status.stdout | regex_findall(regex_expression) }}"

在字符串上

nfsclst02-lvm      (ocf::heartbeat:LVM):   Started testserver07.local.net
  • matches.0.0 将是 (ocf::heartbeat:LVM),因此,您的 type
  • matches.0.1 将是 Started,因此,您的 state
  • matches.0.2 将是 testserver07.local.net,因此,您的 target

现在,因为每次搜索只有一个匹配项,所以您可以简化这个和 flatten 列表的列表,以

结尾
regex_expression: "{{ item.name }}\s+(\S+):\s+(\S+)\s+(\S+)"
matches: "{{ reg_command_pcs_status.stdout | regex_findall(regex_expression) | flatten }}"

这会将匹配简化为:

  • matches.0 将是 (ocf::heartbeat:LVM),因此,您的 type
  • matches.1 将是 Started,因此,您的 state
  • matches.2 将是 testserver07.local.net,因此,您的 target

所以你的任务最终是:

- name: set_fact_pcs_status
  ansible.builtin.set_fact:
    fact_pcs_status: "{{ fact_pcs_status|default([]) + [ {'resource': item.name, 'type': matches.0, 'state': matches.1, 'target': matches.2 } ] }}"
  loop: "{{ var_pcs_resources }}"
  vars:
    regex_expression: "{{ item.name }}\s+(\S+):\s+(\S+)\s+(\S+)"
    matches: "{{ reg_command_pcs_status.stdout | regex_findall(regex_expression) | flatten }}"

鉴于剧本:

- hosts: all
  gather_facts: no

  tasks:
    - ansible.builtin.set_fact:
        fact_pcs_status: "{{ fact_pcs_status|default([]) + [ {'resource': item.name, 'type': matches.0, 'state': matches.1, 'target': matches.2 } ] }}"
      loop: "{{ var_pcs_resources }}"
      vars:
        regex_expression: "{{ item.name }}\s+(\S+):\s+(\S+)\s+(\S+)"
        matches: "{{ reg_command_pcs_status.stdout | regex_findall(regex_expression) | flatten }}"
        var_vcdc01_ip: foo
        var_nfsclst02_vip: bar
        var_vcdc02_ip: bar_ip
        var_vcdc03_ip: baz
        var_vcdc04_ip: qux
        var_vcdc05_ip: quux
        reg_command_pcs_status:
          stdout: >-
            Cluster name: nfsclst02
            Stack: corosync
            Current DC: testserver07.local.net (version 1.1.23-1.el7_9.1-9acf116022) - partition with quorum
            Last updated: Wed Apr  7 09:12:22 2021
            Last change: Wed Mar 31 10:33:35 2021 by root via cibadmin on testserver06.local.net

            2 nodes configured
            16 resource instances configured

            Online: [ testserver06.local.net testserver07.local.net ]

            Full list of resources:

            nfsclst02      (stonith:fence_vmware_soap):    Started testserver06.local.net
            Resource Group: nfsclst02grp01
                nfsclst02-lvm      (ocf::heartbeat:LVM):   Started testserver07.local.net
                lvm_transfer       (ocf::heartbeat:Filesystem):    Started testserver07.local.net
                nfs-daemon (ocf::heartbeat:nfsserver):     Started testserver07.local.net
                nfs-transfer01_18  (ocf::heartbeat:exportfs):      Started testserver07.local.net
                nfs-transfer03_18  (ocf::heartbeat:exportfs):      Started testserver07.local.net
                nfs-transfer01_19  (ocf::heartbeat:exportfs):      Started testserver07.local.net
                nfs-transfer03_19  (ocf::heartbeat:exportfs):      Started testserver07.local.net
                nfs-transfer01_20  (ocf::heartbeat:exportfs):      Started testserver07.local.net
                nfs-transfer03_20  (ocf::heartbeat:exportfs):      Started testserver07.local.net
                nfs-transfer01_21  (ocf::heartbeat:exportfs):      Started testserver07.local.net
                nfs-transfer03_21  (ocf::heartbeat:exportfs):      Started testserver07.local.net
                nfs-transfer01_22  (ocf::heartbeat:exportfs):      Started testserver07.local.net
                nfs-transfer03_22  (ocf::heartbeat:exportfs):      Started testserver07.local.net
                nfs_vip    (ocf::heartbeat:IPaddr2):       Started testserver07.local.net
                nfs-notify (ocf::heartbeat:nfsnotify):     Started testserver07.local.net

            Daemon Status:
              corosync: active/enabled
              pacemaker: active/enabled
              pcsd: active/enabled
        var_pcs_resources:
          - name: 'nfsclst02-lvm'
            type: 'ocf:heartbeat:LVM'
            opts: >-
              volgrpname=vg02_lvm_transfer
              exclusive=true
              --group nfsclst02grp01
          - name: 'lvm_transfer'
            type: 'ocf:heartbeat:Filesystem'
            opts: >-
              device=/dev/vg02_lvm_transfer/lvm_transfer
              directory=/storage/lvm_transfer
              fstype=ext4
              --group nfsclst02grp01
          - name: 'nfs-daemon'
            type: 'ocf:heartbeat:nfsserver'
            opts: >-
              nfs_shared_infodir=/storage/lvm_transfer/nfsinfo
              nfs_no_notify=true
              --group nfsclst02grp01
          - name: 'nfs-transfer01_18'
            type: 'ocf:heartbeat:exportfs'
            opts: >-
              --force
              clientspec={{ var_vcdc01_ip }}/32
              options=rw,sync,no_subtree_check,no_root_squash
              directory=/storage/lvm_transfer/exports/transfer01
              fsid=0
              --group nfsclst02grp01
          - name: 'nfs-transfer03_18'
            type: 'ocf:heartbeat:exportfs'
            opts: >-
              --force
              clientspec={{ var_vcdc01_ip }}/32
              options=rw,sync,no_subtree_check,no_root_squash
              directory=/storage/lvm_transfer/exports/transfer03
              fsid=1
              --group nfsclst02grp01
          - name: 'nfs-transfer01_19'
            type: 'ocf:heartbeat:exportfs'
            opts: >-
              --force
              clientspec={{ var_vcdc02_ip }}/32
              options=rw,sync,no_subtree_check,no_root_squash
              directory=/storage/lvm_transfer/exports/transfer01
              fsid=0
              --group nfsclst02grp01
          - name: 'nfs-transfer03_19'
            type: 'ocf:heartbeat:exportfs'
            opts: >-
              --force
              clientspec={{ var_vcdc02_ip }}/32
              options=rw,sync,no_subtree_check,no_root_squash
              directory=/storage/lvm_transfer/exports/transfer03
              fsid=1
              --group nfsclst02grp01
          - name: 'nfs-transfer01_20'
            type: 'ocf:heartbeat:exportfs'
            opts: >-
              --force
              clientspec={{ var_vcdc03_ip }}/32
              options=rw,sync,no_subtree_check,no_root_squash
              directory=/storage/lvm_transfer/exports/transfer01
              fsid=0
              --group nfsclst02grp01
          - name: 'nfs-transfer03_20'
            type: 'ocf:heartbeat:exportfs'
            opts: >-
              --force
              clientspec={{ var_vcdc03_ip }}/32
              options=rw,sync,no_subtree_check,no_root_squash
              directory=/storage/lvm_transfer/exports/transfer03
              fsid=1
              --group nfsclst02grp01
          - name: 'nfs-transfer01_21'
            type: 'ocf:heartbeat:exportfs'
            opts: >-
              --force
              clientspec={{ var_vcdc04_ip }}/32
              options=rw,sync,no_subtree_check,no_root_squash
              directory=/storage/lvm_transfer/exports/transfer01
              fsid=0
              --group nfsclst02grp01
          - name: 'nfs-transfer03_21'
            type: 'ocf:heartbeat:exportfs'
            opts: >-
              --force
              clientspec={{ var_vcdc04_ip }}/32
              options=rw,sync,no_subtree_check,no_root_squash
              directory=/storage/lvm_transfer/exports/transfer03
              fsid=1
              --group nfsclst02grp01
          - name: 'nfs-transfer01_22'
            type: 'ocf:heartbeat:exportfs'
            opts: >-
              --force
              clientspec={{ var_vcdc05_ip }}/32
              options=rw,sync,no_subtree_check,no_root_squash
              directory=/storage/lvm_transfer/exports/transfer01
              fsid=0
              --group nfsclst02grp01
          - name: 'nfs-transfer03_22'
            type: 'ocf:heartbeat:exportfs'
            opts: >-
              --force
              clientspec={{ var_vcdc05_ip }}/32
              options=rw,sync,no_subtree_check,no_root_squash
              directory=/storage/lvm_transfer/exports/transfer03
              fsid=1
              --group nfsclst02grp01
          - name: 'nfs_vip'
            type: 'ocf:heartbeat:IPaddr2'
            opts: "ip={{ var_nfsclst02_vip }} cidr_netmask=24 --group nfsclst02grp01"
          - name: 'nfs-notify'
            type: 'ocf:heartbeat:nfsnotify'
            opts: "source_host={{ var_nfsclst02_vip }} --group nfsclst02grp01"
          

    - ansible.builtin.debug:
        msg: "{{ fact_pcs_status }}"

这产生了回顾:

PLAY [all] *******************************************************************************************************

TASK [ansible.builtin.set_fact] **********************************************************************************
ok: [localhost] => (item={'name': 'nfsclst02-lvm', 'type': 'ocf:heartbeat:LVM', 'opts': 'volgrpname=vg02_lvm_transfer exclusive=true --group nfsclst02grp01'})
ok: [localhost] => (item={'name': 'lvm_transfer', 'type': 'ocf:heartbeat:Filesystem', 'opts': 'device=/dev/vg02_lvm_transfer/lvm_transfer directory=/storage/lvm_transfer fstype=ext4 --group nfsclst02grp01'})
ok: [localhost] => (item={'name': 'nfs-daemon', 'type': 'ocf:heartbeat:nfsserver', 'opts': 'nfs_shared_infodir=/storage/lvm_transfer/nfsinfo nfs_no_notify=true --group nfsclst02grp01'})
ok: [localhost] => (item={'name': 'nfs-transfer01_18', 'type': 'ocf:heartbeat:exportfs', 'opts': '--force clientspec=foo/32 options=rw,sync,no_subtree_check,no_root_squash directory=/storage/lvm_transfer/exports/transfer01 fsid=0 --group nfsclst02grp01'})
ok: [localhost] => (item={'name': 'nfs-transfer03_18', 'type': 'ocf:heartbeat:exportfs', 'opts': '--force clientspec=foo/32 options=rw,sync,no_subtree_check,no_root_squash directory=/storage/lvm_transfer/exports/transfer03 fsid=1 --group nfsclst02grp01'})
ok: [localhost] => (item={'name': 'nfs-transfer01_19', 'type': 'ocf:heartbeat:exportfs', 'opts': '--force clientspec=bar_ip/32 options=rw,sync,no_subtree_check,no_root_squash directory=/storage/lvm_transfer/exports/transfer01 fsid=0 --group nfsclst02grp01'})
ok: [localhost] => (item={'name': 'nfs-transfer03_19', 'type': 'ocf:heartbeat:exportfs', 'opts': '--force clientspec=bar_ip/32 options=rw,sync,no_subtree_check,no_root_squash directory=/storage/lvm_transfer/exports/transfer03 fsid=1 --group nfsclst02grp01'})
ok: [localhost] => (item={'name': 'nfs-transfer01_20', 'type': 'ocf:heartbeat:exportfs', 'opts': '--force clientspec=baz/32 options=rw,sync,no_subtree_check,no_root_squash directory=/storage/lvm_transfer/exports/transfer01 fsid=0 --group nfsclst02grp01'})
ok: [localhost] => (item={'name': 'nfs-transfer03_20', 'type': 'ocf:heartbeat:exportfs', 'opts': '--force clientspec=baz/32 options=rw,sync,no_subtree_check,no_root_squash directory=/storage/lvm_transfer/exports/transfer03 fsid=1 --group nfsclst02grp01'})
ok: [localhost] => (item={'name': 'nfs-transfer01_21', 'type': 'ocf:heartbeat:exportfs', 'opts': '--force clientspec=qux/32 options=rw,sync,no_subtree_check,no_root_squash directory=/storage/lvm_transfer/exports/transfer01 fsid=0 --group nfsclst02grp01'})
ok: [localhost] => (item={'name': 'nfs-transfer03_21', 'type': 'ocf:heartbeat:exportfs', 'opts': '--force clientspec=qux/32 options=rw,sync,no_subtree_check,no_root_squash directory=/storage/lvm_transfer/exports/transfer03 fsid=1 --group nfsclst02grp01'})
ok: [localhost] => (item={'name': 'nfs-transfer01_22', 'type': 'ocf:heartbeat:exportfs', 'opts': '--force clientspec=quux/32 options=rw,sync,no_subtree_check,no_root_squash directory=/storage/lvm_transfer/exports/transfer01 fsid=0 --group nfsclst02grp01'})
ok: [localhost] => (item={'name': 'nfs-transfer03_22', 'type': 'ocf:heartbeat:exportfs', 'opts': '--force clientspec=quux/32 options=rw,sync,no_subtree_check,no_root_squash directory=/storage/lvm_transfer/exports/transfer03 fsid=1 --group nfsclst02grp01'})
ok: [localhost] => (item={'name': 'nfs_vip', 'type': 'ocf:heartbeat:IPaddr2', 'opts': 'ip=bar cidr_netmask=24 --group nfsclst02grp01'})
ok: [localhost] => (item={'name': 'nfs-notify', 'type': 'ocf:heartbeat:nfsnotify', 'opts': 'source_host=bar --group nfsclst02grp01'})

TASK [ansible.builtin.debug] *************************************************************************************
ok: [localhost] => 
  msg:
  - resource: nfsclst02-lvm
    state: Started
    target: testserver07.local.net
    type: (ocf::heartbeat:LVM)
  - resource: lvm_transfer
    state: Started
    target: testserver07.local.net
    type: (ocf::heartbeat:Filesystem)
  - resource: nfs-daemon
    state: Started
    target: testserver07.local.net
    type: (ocf::heartbeat:nfsserver)
  - resource: nfs-transfer01_18
    state: Started
    target: testserver07.local.net
    type: (ocf::heartbeat:exportfs)
  - resource: nfs-transfer03_18
    state: Started
    target: testserver07.local.net
    type: (ocf::heartbeat:exportfs)
  - resource: nfs-transfer01_19
    state: Started
    target: testserver07.local.net
    type: (ocf::heartbeat:exportfs)
  - resource: nfs-transfer03_19
    state: Started
    target: testserver07.local.net
    type: (ocf::heartbeat:exportfs)
  - resource: nfs-transfer01_20
    state: Started
    target: testserver07.local.net
    type: (ocf::heartbeat:exportfs)
  - resource: nfs-transfer03_20
    state: Started
    target: testserver07.local.net
    type: (ocf::heartbeat:exportfs)
  - resource: nfs-transfer01_21
    state: Started
    target: testserver07.local.net
    type: (ocf::heartbeat:exportfs)
  - resource: nfs-transfer03_21
    state: Started
    target: testserver07.local.net
    type: (ocf::heartbeat:exportfs)
  - resource: nfs-transfer01_22
    state: Started
    target: testserver07.local.net
    type: (ocf::heartbeat:exportfs)
  - resource: nfs-transfer03_22
    state: Started
    target: testserver07.local.net
    type: (ocf::heartbeat:exportfs)
  - resource: nfs_vip
    state: Started
    target: testserver07.local.net
    type: (ocf::heartbeat:IPaddr2)
  - resource: nfs-notify
    state: Started
    target: testserver07.local.net
    type: (ocf::heartbeat:nfsnotify)

PLAY RECAP *******************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

我最终创建了一个稍微不同的输出,我可以通过 JMES 轻松查询以查找任何状态。


    ## Build an array (fact) of only names of variable 'var_pcs_resources'
    - name: set_fact_pcs_resource_names
      ansible.builtin.set_fact:
        fact_pcs_resource_names: "{{ fact_pcs_resource_names | default([]) + [ {'name': item.name} ] }}"
        cacheable: yes
      loop: "{{ var_pcs_resources }}"
      run_once: True

    ## Build an array (fact) using 'fact_pcs_resource_name' as the variable
    - name: set_fact_pcs_resource_status
      ansible.builtin.set_fact:
        fact_pcs_resource_status: "{{ fact_pcs_resource_status | default([]) + [ {'name': item.name, 'type': type, 'state': state, 'target': target } ] }}"
        cacheable: yes
      loop: "{{ ansible_facts.fact_pcs_resource_names }}"
      vars:
        regex_expression: "{{ item.name }}\s+\((.*)\):\s+(\S*)\s+(\S*)"
        type: "{{ reg_command_pcs_status.stdout | regex_search(regex_expression, '\1') | first }}"
        state: "{{ reg_command_pcs_status.stdout | regex_search(regex_expression, '\2') | first }}"
        target: "{{ reg_command_pcs_status.stdout | regex_search(regex_expression, '\3') | first }}"
      run_once: True

这将产生以下输出:

TASK [vcd_nfs_cluster : debug_vars] *****************************************************************************************************************************************************************
Friday 09 April 2021  11:41:12 -0400 (0:00:02.215)       0:00:14.389 ********** 
ok: [testserver06.local.net] => {
    "ansible_facts.fact_pcs_resource_status": [
        {
            "name": "nfsclst02-lvm",
            "state": "Started",
            "target": "testserver07.local.net",
            "type": "ocf::heartbeat:LVM"
        },
        {
            "name": "lvm_transfer",
            "state": "Started",
            "target": "testserver07.local.net",
            "type": "ocf::heartbeat:Filesystem"
        },
        {
            "name": "nfs-daemon",
            "state": "Started",
            "target": "testserver07.local.net",
            "type": "ocf::heartbeat:nfsserver"
        },
        {
            "name": "nfs-transfer01_18",
            "state": "Started",
            "target": "testserver07.local.net",
            "type": "ocf::heartbeat:exportfs"
        },
        {
            "name": "nfs-transfer03_18",
            "state": "Started",
            "target": "testserver07.local.net",
            "type": "ocf::heartbeat:exportfs"
        },
        {
            "name": "nfs-transfer01_19",
            "state": "Started",
            "target": "testserver07.local.net",
            "type": "ocf::heartbeat:exportfs"
        },
        {
            "name": "nfs-transfer03_19",
            "state": "Started",
            "target": "testserver07.local.net",
            "type": "ocf::heartbeat:exportfs"
        },
        {
            "name": "nfs-transfer01_20",
            "state": "Started",
            "target": "testserver07.local.net",
            "type": "ocf::heartbeat:exportfs"
        },
        {
            "name": "nfs-transfer03_20",
            "state": "Started",
            "target": "testserver07.local.net",
            "type": "ocf::heartbeat:exportfs"
        },
        {
            "name": "nfs-transfer01_21",
            "state": "Started",
            "target": "testserver07.local.net",
            "type": "ocf::heartbeat:exportfs"
        },
        {
            "name": "nfs-transfer03_21",
            "state": "Started",
            "target": "testserver07.local.net",
            "type": "ocf::heartbeat:exportfs"
        },
        {
            "name": "nfs-transfer01_22",
            "state": "Started",
            "target": "testserver07.local.net",
            "type": "ocf::heartbeat:exportfs"
        },
        {
            "name": "nfs-transfer03_22",
            "state": "Started",
            "target": "testserver07.local.net",
            "type": "ocf::heartbeat:exportfs"
        },
        {
            "name": "nfs_vip",
            "state": "Started",
            "target": "testserver07.local.net",
            "type": "ocf::heartbeat:IPaddr2"
        },
        {
            "name": "nfs-notify",
            "state": "Started",
            "target": "testserver07.local.net",
            "type": "ocf::heartbeat:nfsnotify"
        }
    ]
}