【发布时间】:2015-06-14 08:30:12
【问题描述】:
我正在编写一个确保节点出现在/etc/fstab 中的剧本。
我使用循环来防止代码重复。
逻辑是首先使用 grep 检查该行是否出现(使用 perl 正则表达式,因为它是多行)并将结果存储在寄存器中。
然后我只想添加不在 fstab 文件中的行。为此,我需要遍历列表(带有 grep 返回代码的寄存器)和字典(包含 fstab 条目)。
并行循环出现错误。我尝试按照these 的步骤进行操作。
One or more undefined variables: 'str object' has no attribute 'item'
tasks/fstab.yaml:
---
- name: Make dirs
sudo: yes
file: path={{ item.value }} state=directory
with_dict:
"{{ fstab.paths }}"
- name: Check whether declared in fstab
sudo: no
command: grep -Pzq '{{ item.value }}' /etc/fstab
register: is_declared
with_dict:
"{{ fstab.regexs }}"
- name: Add the missing entries
sudo: yes
lineinfile: dest=/etc/fstab line="{{ item.1.item.value }}"
when: item.0.rc == 1
with_together:
- "{{ is_declared.results }}"
- "{{ fstab.entries }}"
vars/main.yml:
---
fstab:
paths:
a: "/mnt/a"
b: "/mnt/b"
regexs:
a: '\n# \(a\)\nfoo1'
b: '\n# \(b\)\nfoo2'
entries:
a: "\n# (a)\nfoo1"
b: "\n# (b)\nfoo2"
- 我不是故意使用模板(我想向现有文件添加条目,而不是覆盖它们)。
更新:我看到 ansible 有处理 fstab 的模块“mount”。但是我仍在寻找解决此问题的方法,因为以后可能会再次需要它。
【问题讨论】: