medisamm
medisamm

Reputation: 341

find Mount point from cat /etc/fstab with ansible

i would create a playbook that Check Mount_points for fstype:ext related to the vars: whitelist so it will iterate through the vars to check if mount_point exists or not if it exists an output should be similar to this, else it will be ignored

/ /boot /home /opt /var /var/opt /var/tmp /var/log /var/log/audit here is my playbook which was using 'xfs' as i don't have ext in my machine. Could you advise about more efficient way to achieve the desired result

  - hosts: all
    vars:
      whitelist:
        - '/'
        - '/boot'
        - '/home'
        - '/opt'
        - '/var'
        - '/bin'
        - '/usr'  

    tasks:
      - set_fact:
          mount_point: "{{ansible_facts.mounts | selectattr('fstype', 'match', '^xf+') | map(attribute='mount')}}"
      - debug:
          var: mount_point

        loop: "{{ whitelist }}"
        when: item in mount_point

TASK [set_fact] **************************************************************************************************************
ok: [ansible2]
ok: [ansible3]

TASK [debug] *****************************************************************************************************************
ok: [ansible2] => (item=/) => {
    "msg": [
        "/",
        "/boot"
    ]
}
ok: [ansible2] => (item=/boot) => {
    "msg": [
        "/",
        "/boot"
    ]
}
skipping: [ansible2] => (item=/home)
skipping: [ansible2] => (item=/opt)
skipping: [ansible2] => (item=/var)
skipping: [ansible2] => (item=/bin)
skipping: [ansible2] => (item=/usr)
ok: [ansible3] => (item=/) => {
    "msg": [
        "/boot",
        "/"
    ]
}
ok: [ansible3] => (item=/boot) => {
    "msg": [
        "/boot",
        "/"
    ]
}
skipping: [ansible3] => (item=/home)
skipping: [ansible3] => (item=/opt)
skipping: [ansible3] => (item=/var)
skipping: [ansible3] => (item=/bin)
skipping: [ansible3] => (item=/usr)

PLAY RECAP *******************************************************************************************************************
ansible2                   : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ansible3                   : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Upvotes: 1

Views: 730

Answers (1)

Thomas
Thomas

Reputation: 182038

This hopefully does what you need:

    - name: List all mount points on which an ext2/ext3/ext4 file system is mounted
      ansible.builtin.debug:
        msg: "{{ansible_facts.mounts | selectattr('fstype', 'in', ['ext2', 'ext3', 'ext4']) | map(attribute='mount')}}"

First it uses selectattr to keep only those mounts whose fstype is one of ext2, ext3 or ext4. Then it uses map to extract the mount point from each entry. The result is a list, for example ["/", "/usr", "/var"].

Upvotes: 1

Related Questions