简体   繁体   English

使用 ansible 从 cat /etc/fstab 中查找挂载点

[英]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我将创建一个剧本,检查与vars: whitelist相关的fstype:extMount_points :白名单,因此它将遍历 vars 以检查 mount_point 是否存在,如果它存在,则输出应该与此类似,否则将被忽略

/ /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. / /boot /home /opt /var /var/opt /var/tmp /var/log /var/log/audit这是我使用'xfs'的剧本,因为我的机器中没有ext。 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

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 .首先,它使用selectattr仅保留fstypeext2ext3ext4之一的那些挂载。 Then it uses map to extract the mount point from each entry.然后它使用map从每个条目中提取挂载点。 The result is a list, for example ["/", "/usr", "/var"] .结果是一个列表,例如["/", "/usr", "/var"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM