user2023
user2023

Reputation: 478

Ansible aclling two diffrent file variables into single task looping through multiple files

Content of fil1

# cat file1
fostrain01.example.com
fostrain02.example.com
fostrain03.example.com

Content of file2

# cat fil2
ServerIPS 171.20.20.16 171.20.20.17 171.20.20.18 171.20.20.19 171.20.20.20
ServerIPS 171.20.20.21 171.20.20.22 171.20.20.23 171.20.20.24 171.20.20.25 

In the below playbook, its replacing the contents on two lines one is hostname as str and in another line replacing the ip So, i have taken the two different task 1) Replace strings in file & 2) Replace ip in file to accomplish this calling the variable defined.

Playbook:

- name: Replace string in hosts file
  hosts: all
  gather_facts: false
  vars:
    files:
      - /etc/file1
      - /etc/file2
    from_str: "fostrain01.example.com"
      to_str: "dbfoxtrain01.example.com"
     from_ip: "^(.*)171\\.20\\.20\\.18(.*)$"
       to_ip: "\\g<1>172.20.20.18\\g<2>"
  tasks:
    - name: Replace strings in file
      replace:
        path: "{{ item}}"
        regexp: "{{ from_str }}"
        replace: "{{ to_str }}"
        backup: yes
      loop: "{{ files }}"

    - name: Replace ip in file
      replace:
        path: "{{ item}}"
        regexp: "{{ from_ip }}"
        replace: "{{ to_ip }}"
        backup: yes
      loop: "{{ files }}"

Can we write the task as follows something where we don't really write to two different tasks, i tried but not getting how to loops through "{{ files }} with below approach.

  tasks:
    - name: Replace elements in file
      replace:
        path: "{{ item.path }}"
        regexp: "{{ item.From }}"
        replace: "{{ item.To }}"
        backup: yes
      loop:
        # Replace the desired string
        - { path: "{{ item }}", From: "{{ from_str }}", To: "{{ to_str }}" }
        # Replace the desired ip
        - { path: "{{ item}}", From: "{{ from_ip }}", To: "{{ to_ip}}" }

What is Desired Change:

task 1) Replace strings in file & task 2) Replace ip in file to be clubbed into One.

Expected Results:

# cat file1
dbfoxtrain01.example.com  <-- Changed
fostrain02.example.com
fostrain03.example.com

# cat fil2
ServerIPS 171.20.20.16 171.20.20.17 172.20.20.18 171.20.20.19 171.20.20.20  <-- changed here ^172
ServerIPS 171.20.20.21 171.20.20.22 171.20.20.23 171.20.20.24 171.20.20.25 

Upvotes: 3

Views: 435

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 67984

The task below does the job

    - replace:
        path: "{{ item.path }}"
        regexp: "{{ item.from }}"
        replace: "{{ item.to }}"
      loop:
        - path: file1
          from: 'fostrain01\.example\.com'
          to: 'dbfoxtrain01.example.com'
        - path: file2
          from: '171\.20\.20\.18'
          to: '172.20.20.18'

Example

shell> tree .
.
├── file1
├── file1.orig
├── file2
├── file2.orig
└── test.yml

0 directories, 5 files

shell> cat file1
fostrain01.example.com
fostrain02.example.com
fostrain03.example.com

shell> cat file2
ServerIPS 171.20.20.16 171.20.20.17 171.20.20.18 171.20.20.19 171.20.20.20
ServerIPS 171.20.20.21 171.20.20.22 171.20.20.23 171.20.20.24 171.20.20.25

shell> cat test.yml 
- hosts: localhost
  gather_facts: false
  tasks:
    - replace:
        path: "{{ item.path }}"
        regexp: "{{ item.from }}"
        replace: "{{ item.to }}"
      loop:
        - path: file1
          from: 'fostrain01\.example\.com'
          to: 'dbfoxtrain01.example.com'
        - path: file2
          from: '171\.20\.20\.18'
          to: '172.20.20.18'

shell> ansible-playbook test.yml 

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

TASK [replace] **************************************************
changed: [localhost] => (item={'path': 'file1', 'from': 'fostrain01\\.example\\.com', 'to': 'dbfoxtrain01.example.com'})
changed: [localhost] => (item={'path': 'file2', 'from': '171\\.20\\.20\\.18', 'to': '172.20.20.18'})

PLAY RECAP ******************************************************
localhost: ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0   

shell> cat file1
dbfoxtrain01.example.com
fostrain02.example.com
fostrain03.example.com

shell> cat file2
ServerIPS 171.20.20.16 171.20.20.17 172.20.20.18 171.20.20.19 171.20.20.20
ServerIPS 171.20.20.21 171.20.20.22 171.20.20.23 171.20.20.24 171.20.20.25

Upvotes: 2

Related Questions