2014-09-22 9 views
6

का उपयोग कर एक fstab विकल्प जोड़ना मैं अपने /etc/fstab फ़ाइल में nodev जोड़ने की कोशिश कर रहा हूं। मैं नीचे उत्तरदायी कमांड का उपयोग कर रहा हूं लेकिन बिना किसी किस्मत के। मेरा मुद्दा नियमित अभिव्यक्ति के साथ है, मैं regex पर समर्थक नहीं हूँ।Ansible

- name: Add nodev to /etc/fstab 
    lineinfile: 
    dest=/etc/fstab 
    backup=yes 
    backrefs=yes 
    state=present 
    regexp='(^/dev[\w/_-]+(\s+(?!nodev)[\w,]+)*)' 
    line='\1,nodev' 

/etc/fstab से वे पंक्तियां मैं nodev जोड़ने के लिए कोशिश कर रहा हूँ से एक है:

/dev/mapper/ex_sys-ex_home /home /ext4 rw,exec,auto,nouser,sync 1 2 
+0

परीक्षण "कोई भाग्य" परिभाषित करते समय केवल उत्तर परिवर्तनीय {{ point }} आदि के वास्तविक मूल्यों का उपयोग करना याद रखें। वास्तविक परिणाम क्या था? मुझे लगता है कि/etc/fstab कुछ लाइनों में "nodev" जोड़ने के बजाय अपरिवर्तित था? – LarsH

+0

@ लार्सएच एक दूसरे में परिणाम प्रदान करेगा, मैंने regexp को संशोधित किया है (^/dev [\ w/_-] + \ + [[w/_-] + \ + [\ w/_-] + \ s + ((?! nodev) [\ w,] +) *) और मुझे लगता है कि –

+0

काम कर सकता है हाँ, मुझे लगता है कि विकल्प के लिए आपके दोहराने वाले समूह में आपके पास '\ s +' था, मैच को अवरुद्ध कर रहा था। अगर यह काम नहीं करता है तो मुझे बताएं। – LarsH

उत्तर

11

हालांकि यह सबसे खूबसूरत जवाब नहीं हो सकता है, यह मेरे लिए काम किया।

- name: Ensure fstab uses nodev 
    mount: 
    name: "{{ item.mount }}" 
    src: "{{ item.device }}" 
    fstype: "{{ item.fstype }}" 
    opts: "{{ item.options }},nodev" 
    state: present 
    with_items: ansible_mounts 
    when: item.options.find(",") >= 0 and item.options.find("nodev") == -1 
+0

असल में मुझे यह बहुत अच्छा और संक्षिप्त लगता है। – jojek

1

हमने माउंट विकल्पों को जोड़ने, सेट करने या हटाने के लिए एक तृतीय-पक्ष उत्तरदायी मॉड्यूल विकसित किया है। इसकी जांच - पड़ताल करें!

- mountopts: 
     name:/
     option: nodev 

https://github.com/Uberspace/ansible-mountopts

0

यहां पहुंचे एक जवाब की तलाश में, को बंद कर अपने प्रयोग के मामले के लिए अपने खुद के रोलिंग:

main.yml

- include: fstab-opts.yml point=/tmp opts=noexec,nodev,nosuid,noatime 
- include: fstab-opts.yml point=/backup opts=noatime 

fstab-opts.yml

--- 

- name: 'Ensure {{ point }} flags' 
    lineinfile: 
    path: /etc/fstab 
    # uses "(not-spaces spaces /tmp spaces)(not-spaces)(the rest)" pattern to match column content and capture args 
    regexp: '^([^ ]+[ ]+\{{ point }}[ ]+[^ ]+[ ]+)([^ ]+)(.*)' 
    line: '\1{{ opts }}\3' 
    backrefs: yes 
    register: fstab 

- name: 'If {{ point }} changed, remount' 
    command: mount -o remount {{ point }} 
    when: fstab.changed 
0

जो के जवाब से प्रेरित मैंने इस संस्करण को बनाया है जो /etc/fstab में एक विशिष्ट लाइन में एक ही विकल्प जोड़ देगा यदि यह पहले से नहीं है। यह पहले से मौजूद लाइन के किसी अन्य विकल्प भी रखेगा।

main.yml

- import_tasks: fstab-opt-present.yml point=/home opt=nodev 

fstab-ऑप्ट present.yml

- name: '/etc/fstab: Set opt "{{ opt }}" for mount point {{ point }}' 
    lineinfile: 
    path: /etc/fstab 
    backup: yes 
    backrefs: yes 
    regexp: '^(\S+\s+{{ point }}\s+\S+\s+)(?!(?:\S*,)?{{ opt }}(?:,\S*)?\s+)(\S+)(\s+.+)$' 
    line: '\1{{ opt }},\2\3' 
    register: fstab 

- name: 'If {{ point }} changed, remount' 
    command: 'mount {{ point }} -o remount' 
    when: fstab.changed 

https://regex101.com/ निर्माण और regexps इस तरह के परीक्षण के लिए एक बहुत उपयोगी उपकरण है। बस "मल्टीलाइन" विकल्प को सक्षम करें और "प्रतिस्थापन" पैनल खोलें और आप अपने /etc/fstab में भी पेस्ट कर सकते हैं और देख सकते हैं कि आपकी रेगेक्स कौन सी रेखाएं मेल खाती है और यह उनके साथ क्या करेगी।