Search code examples
ansibleansible-2.xansible-inventoryansible-facts

Validate registered value for all nodes using shell command


I have below task that search for a string in the mount output for all nodes and am registering it as well. But I want to validate that all nodes did have the string and not just validate the last node as register variable is i believe replaced everytime for every node. any hint how can I assure that every node has that string?

I can achieve this easily at playlevel but I want to achieve this at role level in task.

      - name: "Verify LVP Mounts sdd exists on CPU Nodes for mount_device"
        shell: "mount | grep sdd"
        register: lvp_mount
        ignore_errors: yes
        failed_when: False
        delegate_to: "{{ item }}"
        with_items: "{{ groups['kube-cpu-node'] }}"
      - debug:
          msg: "Assert SDD Mounts present"
        # this check is only for last node, but I want this to be done for all nodes.
        failed_when: lvp_mount.results[0].stdout_lines[0] is not search('sdd')

Adding output from the first answer below which is for only one node. but when i have 10 nodes, this will repeat 10 times. so I want to loop control and just list sdd present on node1, sdd absent on node2 ...etc

TASK [team-services-pre-install-checks : Make sure sdd mount is present in loop on all cpu nodes] ***************************************************************************
Monday 02 December 2019  22:27:45 +0000 (0:00:00.061)       0:00:04.203 *******
ok: [localhost] => (item={'stderr_lines': [], 'ansible_loop_var': u'item', u'end': u'2019-12-02 22:28:39.803095', u'stderr': u'', u'stdout': u'/dev/sdd1 on /local-volumes/postgres/b5b296cf-02cd-41b3-b090-4e2dc77c6867 type ext4 (rw,relatime,data=ordered)\n/dev/sdd2 on /local-volumes/postgres/bc5b6f6b-d2fa-4966-b896-ada49d0de0e5 type ext4 (rw,relatime,data=ordered)\n/dev/sdd3 on /local-volumes/postgres/5b3941cb-4f40-4831-b6e4-d80ce78c7a79 type ext4 (rw,relatime,data=ordered)\n/dev/sdd4 on /local-volumes/postgres/34ace6a3-8a30-4091-be2a-b0ae19117b94 type ext4 (rw,relatime,data=ordered)\n/dev/sdd5 on /local-volumes/postgres/6b5d88d5-b963-4b36-b09f-1a27071f503f type ext4 (rw,relatime,data=ordered)\n/dev/sdd6 on /local-volumes/postgres/04b2aaf2-0008-4c1f-ab7f-3ea405832728 type ext4 (rw,relatime,data=ordered)\n/dev/sdd7 on /local-volumes/postgres/a278000d-737e-4f56-81be-e29460592b18 type ext4 (rw,relatime,data=ordered)\n/dev/sdd8 on /local-volumes/postgres/f3c47bf0-e5f7-4254-9e4a-59346a2268f6 type ext4 (rw,relatime,data=ordered)\n/dev/sdd9 on /local-volumes/postgres/e10e7e57-807c-47e1-8a0f-ee72eb977922 type ext4 (rw,relatime,data=ordered)\n/dev/sdd9 on /var/lib/kubelet/pods/27411faf-0ca9-11ea-9681-ac1f6b4e34d6/volumes/kubernetes.io~local-volume/local-pv-2e0dc3c0 type ext4 (rw,relatime,data=ordered)\n/dev/sdd6 on /var/lib/kubelet/pods/d6f6421a-0cbd-11ea-9681-ac1f6b4e34d6/volumes/kubernetes.io~local-volume/local-pv-499b92f1 type ext4 (rw,relatime,data=ordered)\n/dev/sdd8 on /var/lib/kubelet/pods/c75f08bf-0cc1-11ea-9681-ac1f6b4e34d6/volumes/kubernetes.io~local-volume/local-pv-31875a2b type ext4 (rw,relatime,data=ordered)', u'changed': True, 'failed': False, u'delta': u'0:00:00.010980', u'cmd': u'mount | grep sdd', 'item': u'compute0394', u'rc': 0, u'invocation': {u'module_args': {u'warn': True, u'executable': None, u'_uses_shell': True, u'strip_empty_ends': True, u'_raw_params': u'mount | grep sdd', u'removes': None, u'argv': None, u'creates': None, u'chdir': None, u'stdin_add_newline': True, u'stdin': None}}, 'stdout_lines': [u'/dev/sdd1 on /local-volumes/postgres/b5b296cf-02cd-41b3-b090-4e2dc77c6867 type ext4 (rw,relatime,data=ordered)', u'/dev/sdd2 on /local-volumes/postgres/bc5b6f6b-d2fa-4966-b896-ada49d0de0e5 type ext4 (rw,relatime,data=ordered)', u'/dev/sdd3 on /local-volumes/postgres/5b3941cb-4f40-4831-b6e4-d80ce78c7a79 type ext4 (rw,relatime,data=ordered)', u'/dev/sdd4 on /local-volumes/postgres/34ace6a3-8a30-4091-be2a-b0ae19117b94 type ext4 (rw,relatime,data=ordered)', u'/dev/sdd5 on /local-volumes/postgres/6b5d88d5-b963-4b36-b09f-1a27071f503f type ext4 (rw,relatime,data=ordered)', u'/dev/sdd6 on /local-volumes/postgres/04b2aaf2-0008-4c1f-ab7f-3ea405832728 type ext4 (rw,relatime,data=ordered)', u'/dev/sdd7 on /local-volumes/postgres/a278000d-737e-4f56-81be-e29460592b18 type ext4 (rw,relatime,data=ordered)', u'/dev/sdd8 on /local-volumes/postgres/f3c47bf0-e5f7-4254-9e4a-59346a2268f6 type ext4 (rw,relatime,data=ordered)', u'/dev/sdd9 on /local-volumes/postgres/e10e7e57-807c-47e1-8a0f-ee72eb977922 type ext4 (rw,relatime,data=ordered)', u'/dev/sdd9 on /var/lib/kubelet/pods/27411faf-0ca9-11ea-9681-ac1f6b4e34d6/volumes/kubernetes.io~local-volume/local-pv-2e0dc3c0 type ext4 (rw,relatime,data=ordered)', u'/dev/sdd6 on /var/lib/kubelet/pods/d6f6421a-0cbd-11ea-9681-ac1f6b4e34d6/volumes/kubernetes.io~local-volume/local-pv-499b92f1 type ext4 (rw,relatime,data=ordered)', u'/dev/sdd8 on /var/lib/kubelet/pods/c75f08bf-0cc1-11ea-9681-ac1f6b4e34d6/volumes/kubernetes.io~local-volume/local-pv-31875a2b type ext4 (rw,relatime,data=ordered)'], 'failed_when_result': False, u'start': u'2019-12-02 22:28:39.792115'}) => {
    "ansible_loop_var": "item",
    "changed": false,
    "item": {
        "ansible_loop_var": "item",
        "changed": true,
        "cmd": "mount | grep sdd",
        "delta": "0:00:00.010980",
        "end": "2019-12-02 22:28:39.803095",
        "failed": false,
        "failed_when_result": false,
        "invocation": {
            "module_args": {
                "_raw_params": "mount | grep sdd",
                "_uses_shell": true,
                "argv": null,
                "chdir": null,
                "creates": null,
                "executable": null,
                "removes": null,
                "stdin": null,
                "stdin_add_newline": true,
                "strip_empty_ends": true,
                "warn": true
            }
        },
        "item": "compute0394",
        "rc": 0,
        "start": "2019-12-02 22:28:39.792115",
        "stderr": "",
        "stderr_lines": [],
        "stdout": "/dev/sdd1 on /local-volumes/postgres/b5b296cf-02cd-41b3-b090-4e2dc77c6867 type ext4 (rw,relatime,data=ordered)\n/dev/sdd2 on /local-volumes/postgres/bc5b6f6b-d2fa-4966-b896-ada49d0de0e5 type ext4 (rw,relatime,data=ordered)\n/dev/sdd3 on /local-volumes/postgres/5b3941cb-4f40-4831-b6e4-d80ce78c7a79 type ext4 (rw,relatime,data=ordered)\n/dev/sdd4 on /local-volumes/postgres/34ace6a3-8a30-4091-be2a-b0ae19117b94 type ext4 (rw,relatime,data=ordered)\n/dev/sdd5 on /local-volumes/postgres/6b5d88d5-b963-4b36-b09f-1a27071f503f type ext4 (rw,relatime,data=ordered)\n/dev/sdd6 on /local-volumes/postgres/04b2aaf2-0008-4c1f-ab7f-3ea405832728 type ext4 (rw,relatime,data=ordered)\n/dev/sdd7 on /local-volumes/postgres/a278000d-737e-4f56-81be-e29460592b18 type ext4 (rw,relatime,data=ordered)\n/dev/sdd8 on /local-volumes/postgres/f3c47bf0-e5f7-4254-9e4a-59346a2268f6 type ext4 (rw,relatime,data=ordered)\n/dev/sdd9 on /local-volumes/postgres/e10e7e57-807c-47e1-8a0f-ee72eb977922 type ext4 (rw,relatime,data=ordered)\n/dev/sdd9 on /var/lib/kubelet/pods/27411faf-0ca9-11ea-9681-ac1f6b4e34d6/volumes/kubernetes.io~local-volume/local-pv-2e0dc3c0 type ext4 (rw,relatime,data=ordered)\n/dev/sdd6 on /var/lib/kubelet/pods/d6f6421a-0cbd-11ea-9681-ac1f6b4e34d6/volumes/kubernetes.io~local-volume/local-pv-499b92f1 type ext4 (rw,relatime,data=ordered)\n/dev/sdd8 on /var/lib/kubelet/pods/c75f08bf-0cc1-11ea-9681-ac1f6b4e34d6/volumes/kubernetes.io~local-volume/local-pv-31875a2b type ext4 (rw,relatime,data=ordered)",
        "stdout_lines": [
            "/dev/sdd1 on /local-volumes/postgres/b5b296cf-02cd-41b3-b090-4e2dc77c6867 type ext4 (rw,relatime,data=ordered)",
            "/dev/sdd2 on /local-volumes/postgres/bc5b6f6b-d2fa-4966-b896-ada49d0de0e5 type ext4 (rw,relatime,data=ordered)",
            "/dev/sdd3 on /local-volumes/postgres/5b3941cb-4f40-4831-b6e4-d80ce78c7a79 type ext4 (rw,relatime,data=ordered)",
            "/dev/sdd4 on /local-volumes/postgres/34ace6a3-8a30-4091-be2a-b0ae19117b94 type ext4 (rw,relatime,data=ordered)",
            "/dev/sdd5 on /local-volumes/postgres/6b5d88d5-b963-4b36-b09f-1a27071f503f type ext4 (rw,relatime,data=ordered)",
            "/dev/sdd6 on /local-volumes/postgres/04b2aaf2-0008-4c1f-ab7f-3ea405832728 type ext4 (rw,relatime,data=ordered)",
            "/dev/sdd7 on /local-volumes/postgres/a278000d-737e-4f56-81be-e29460592b18 type ext4 (rw,relatime,data=ordered)",
            "/dev/sdd8 on /local-volumes/postgres/f3c47bf0-e5f7-4254-9e4a-59346a2268f6 type ext4 (rw,relatime,data=ordered)",
            "/dev/sdd9 on /local-volumes/postgres/e10e7e57-807c-47e1-8a0f-ee72eb977922 type ext4 (rw,relatime,data=ordered)",
            "/dev/sdd9 on /var/lib/kubelet/pods/27411faf-0ca9-11ea-9681-ac1f6b4e34d6/volumes/kubernetes.io~local-volume/local-pv-2e0dc3c0 type ext4 (rw,relatime,data=ordered)",
            "/dev/sdd6 on /var/lib/kubelet/pods/d6f6421a-0cbd-11ea-9681-ac1f6b4e34d6/volumes/kubernetes.io~local-volume/local-pv-499b92f1 type ext4 (rw,relatime,data=ordered)",
            "/dev/sdd8 on /var/lib/kubelet/pods/c75f08bf-0cc1-11ea-9681-ac1f6b4e34d6/volumes/kubernetes.io~local-volume/local-pv-31875a2b type ext4 (rw,relatime,data=ordered)"
        ]
    },
    "msg": "sdd mount is present"
}

Solution

    1. Simply loop over the results you get from your registered var lvp_mount.
    2. If you look for a substring that would exists in any output line of your result, search on stdout at once (so you don't have to loop over stdout_lines).
    3. If you want to assert something, then you should consider using the assert module.

    Quick illustration (not tested) to put these points together.

    - name: Make sure sdd mount is present
      assert:
        that:
          - item.stdout is search('sdd')
        fail_msg: "sdd mount is absent"
        success_msg: "sdd mount is present"
      loop: "{{ lvp_mount.results }}"
      loop_control:
        label: "{{ item.some_meaningful_var }}"
    

    Edit after small side question in comment: The loop_control parameter is to limit the output to only a meaningful value in your item. Choose a var in each result that makes sense in your case. You can even set this to empty string if you wish