[英]find Mount point from cat /etc/fstab with ansible
我將創建一個劇本,檢查與vars: whitelist
相關的fstype:ext
的Mount_points
:白名單,因此它將遍歷 vars 以檢查 mount_point 是否存在,如果它存在,則輸出應該與此類似,否則將被忽略
/ /boot /home /opt /var /var/opt /var/tmp /var/log /var/log/audit
這是我使用'xfs'的劇本,因為我的機器中沒有ext。 你能建議更有效的方法來達到預期的結果嗎
- 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
這有望滿足您的需求:
- 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')}}"
首先,它使用selectattr
僅保留fstype
為ext2
、 ext3
或ext4
之一的那些掛載。 然后它使用map
從每個條目中提取掛載點。 結果是一個列表,例如["/", "/usr", "/var"]
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.