raratiru
raratiru

Reputation: 9636

Ansible: Find /path/*/filename and change several lines

This is my directory tree:

.
├── unknown_dir1
│   └── firefox.conf
└── unknown_dir2
    └── firefox.conf

I need to find every instance of file firefox.conf and change some of their parameters. My task is the following:

- name: configure firefox
   lineinfile:
     dest= [?]
     state=present
     regexp=.*{{ item.value }}.*
     line='user_pref("{{ item.value }}", {{ item.key }});'
     insertafter=EOF
     backup=yes
   with_tems:
     - { value: 'browser.startup.page', key: '0' }
     - { value: 'network.cookie.cookieBehavior', key: '3' }

The problem is that firefox.conf files are found under unknown directories.

How could I search for each file instance and specify it in the dest entry of lineinfile module?

I tried to register the output of a find command before the prepended task:

- name: find firefox.confs
  shell: find /vagrant/* -type f -name "firefox.conf"
  register: files_to_change

However I cannot find out how to tackle with both list and dictionaries in the with_items/with_nested/with_subelements/you_name_it section of the configure firefox task.

Upvotes: 1

Views: 1212

Answers (1)

jarv
jarv

Reputation: 5596

I believe this is what you want:

- name: configure firefox
   lineinfile:
     dest={{ item[0] }}
     state=present
     regexp=.*{{ item[1].value }}.*
     line='user_pref("{{ item[1].value }}", {{ item[1].key }});'
     insertafter=EOF
     backup=yes
   with_nested:
    - files_to_change.stdout_lines
    -
     - { value: 'browser.startup.page', key: '0' }
     - { value: 'network.cookie.cookieBehavior', key: '3' }

Explanation:

with_nested will apply the lineinfile replacements on every file found by the output of the previous task.

Upvotes: 2

Related Questions