ansible loop over list and dictionary at the same time -
i writing playbook ensure nodes appear in /etc/fstab
.
i using loops prevent code duplication.
the logic first check if line appears using grep (with perl regex because multi line) , store results in register.
then want add lines not in fstab file. achieve need loop on list (the register grep return codes) , dictionary (that contains fstab entries).
i having errors parallel loop. tried follow these steps.
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 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"
- i not using template on purpose (i want add entries existing files , not on write them).
update: see ansible has module "mount" deals fstab. still looking solution issue because might needed again later on.
i have couple ideas why original approach failing, let's scratch moment. looks you're overcomplicating things- why not use complex list var tie together, , use regexp arg lineinfile module instead of separate regex task? (though sample data should work fine without regexp param) like:
--- - name: make dirs sudo: yes file: path={{ item.path }} state=directory with_items: fstab - name: add missing entries sudo: yes lineinfile: dest=/etc/fstab line={{ item.entry }} regexp={{ item.regex }} with_items: fstab
fstab: - path: /mnt/a regex: '\n# \(a\)\nfoo1' entry: "\n# (a)\nfoo1" - path: /mnt/b regex: '\n# \(b\)\nfoo2' entry: '\n# (b)\nfoo2'
Comments
Post a Comment