[ansible-project] replace line in multiple files using lineinfile could anyone please help me.

22 views
Skip to first unread message

SARDARNI NK

unread,
Oct 22, 2019, 3:48:33 PM10/22/19
to Ansible Project
---
- hosts: localhost
gather_facts: no
vars:
SVNRepName_Location: /mydata/myserver/ConServer1

tasks:

# copy config files to the backup folder it's working
- name: Copy each file to backupfolder
copy:
src: "{{ item }}"
dest: "{{ SVNRepName_Location}}/backup-October2019"
with_fileglob:
- "{{ SVNRepName_Location }}/conf/*"

# looking for line is in a file, or replace an existing line using a back-referenced regular expression.
- name: looking for password and replacing the field replace line in multiple files using lineinfile
lineinfile:
path: /mydata/myserver/ConServer1/conf/TMSS-KKKU.yml
#The String to Search for Password
regexp: 'Password\s+:\s+[a-zA-Z0-9{\s+}]+'
# The String to Replace
line: ' Password : {{ Password }}'
# backrefs: yes
state: present

Hi All,

I want to use the loop in task replace line in multiple files using lineinfile.

Instead of the path can I use dest: and provide the with_fileglob?

How do I use looping?

I have an example but I am not sure how to use it.
I tried the below link example to replace a line in multiple files. https://groups.google.com/forum/#!msg/ansible-project/wc88DxumT84/Cu-NFe4mxwAJ



Thanks,
Nav

J

unread,
Oct 22, 2019, 10:01:46 PM10/22/19
to Ansible Project
I found this if it works for you.

---
- name: test loop files
hosts: server

gather_facts: no
vars:
SVNRepName_Location: /mydata/myserver/ConServer1
    Password: Ansible
tasks:
- name: list file and register location
raw: find /mydata/myserver/ConServer1 -type f -name "files*"
register: files_found
- name: debug file
debug:
var: files_found.stdout_lines

- name: use the loop in task replace line in multiple file
lineinfile:
dest: "{{item}}"
regexp: SomeLine
line: " Password : {{ Password }}"
state: present
with_items:
- "{{files_found.stdout_lines}}"

SARDARNI NK

unread,
Oct 23, 2019, 11:30:28 AM10/23/19
to Ansible Project

Thank you, J

In (/mydata/myserver/ConServer1  ) i have many files which are different extensions (yml,txt,j2,) how do use (/mydata/myserver/ConServer1 -type f -name "files*") "files*"  do i need to provide same "files*" i don't understand this ? do i need to provide any file extensions or filenames ?
I was testing step by step for task 
  - name: list file and register location
raw: find /mydata/myserver/ConServer1 -type f -name "files*"
register: files_found
- name: debug file
debug:
var: files_found.stdout_lines

I got the output

PLAY [localhost] ***************************************************************

TASK [list file and register location] *****************************************
changed: [localhost] => {"changed": true, "rc": 0, "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}

TASK [debug file] **************************************************************
ok: [localhost] => {
    "files_found.stdout_lines": []
}

PLAY RECAP *********************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0   



Could you please let me know?

Thanks,
Nav

SARDARNI NK

unread,
Oct 23, 2019, 2:27:07 PM10/23/19
to Ansible Project

Thank you, J

It worked for me.

I have updated the  raw: find /mydata/myserver/ConServer1 -type f -name "*.yml" .

Listing all the files and replacing them in multiple files.

Thanks,
Nav

On Tuesday, October 22, 2019 at 10:01:46 PM UTC-4, J wrote:
Reply all
Reply to author
Forward
0 new messages