1

I have a folder with several sub folders and also files inside. I need to change a string in all files at once. I can do for just one file but in other files in sub folders it does not affect them.

- name: Find all files with .json extension
      find:
        paths: /test/configuration/nginx/trials/{{ name }}/trial-admin-app/
        patterns: 'app-config.json'
      register: repos   



    - name:  Change string inside file    
      replace:
        dest: /test/configuration/nginx/trials/{{ name }}/trial-admin-app/                          #app-config.json    
        regexp: '{trial-name}'
        replace: '{{ name }}'
      with_items: "{{ repos.stdout_lines }}"

How do I change all files in the folder who also have sub folders?

2
  • You could use a combination of find with sed in the execute option.
    – FelixJN
    Sep 27, 2019 at 12:54
  • I recommend against find and sed in this case because you'll have to jump through hoops to make ansible aware of whether anything actually changed. Idempotency should be properly represented in automation - it's important to know whether things actually changed. The module can do this fine, it just needs to be configured properly. Use the tool how it's designed. ansible is more sophisticated than a shell script.
    – erik258
    Sep 27, 2019 at 15:08

1 Answer 1

2

The find module you're using has a recurse parameter but it is off by default.

- name: Find all files with .json extension
      find:
        paths: /test/configuration/nginx/trials/{{ name }}/trial-admin-app/
        patterns: 'app-config.json'
        recurse: yes
      register: repos   

By turning it on, I think you'll get the behavior you want.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .