Ansible объединение двух разных файловых переменных в одну задачу, цикл по нескольким файлам - PullRequest
3 голосов
/ 13 июля 2020

Содержимое fil1

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

Содержимое 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 

В приведенной ниже книге воспроизведения его содержимое в двух строках заменяется на имя хоста как str и в другой строке, заменяющей ip Итак, я взял две разные задачи: 1) Replace strings in file и 2) Replace ip in file, чтобы выполнить sh это вызов определенной переменной.

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 }}"

Можем ли мы написать задачу, как показано ниже, где мы на самом деле не записываем в две разные задачи, я пробовал, но не понимаю, как пройти через "{{ files }} с подходом ниже.

  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}}" }

Что является желаемым изменением:

задача 1) Replace strings in file и задача 2) Replace ip in file, которые нужно объединить в Одно.

Ожидаемые результаты:

# 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 

1 Ответ

2 голосов
/ 13 июля 2020

Задача ниже выполняет свою работу

    - 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> 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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...