Loops 如何根据输入范围将拆分值传递到ansible中的循环中?

Loops 如何根据输入范围将拆分值传递到ansible中的循环中?,loops,ansible,jinja2,regexp-replace,ansible-template,Loops,Ansible,Jinja2,Regexp Replace,Ansible Template,我对Ansible循环非常陌生。 我正在尝试将文件中的一组ip地址(oldip替换为newip)。 它需要以oldip1:newip1、oldip2:newip2、oldip3:newip3… 我想拆分上面的ip地址对(old:new),然后根据输入列表在循环中将它们传递给Ansiblereplace模块 我正在尝试类似的方法,但我一直在研究如何将ip地址对的动态范围传递到循环中 我的输入变量类似于192.123.12.11;192.123.12.20, 192.123.12.12;192.12

我对Ansible循环非常陌生。
我正在尝试将文件中的一组ip地址(
oldip
替换为
newip
)。
它需要以
oldip1:newip1、oldip2:newip2、oldip3:newip3…

我想拆分上面的ip地址对(
old:new
),然后根据输入列表在循环中将它们传递给Ansible
replace
模块

我正在尝试类似的方法,但我一直在研究如何将ip地址对的动态范围传递到循环中

我的输入变量类似于
192.123.12.11;192.123.12.20, 192.123.12.12;192.123.12.19 , 192.123.12.13;192.123.12.18…
(动态ip对范围)


此循环应根据输入ip对条目继续(5个ip对(
old;new
)执行5次)。

为了实现这一点,您可以使用一些非常有用的方法,即可以通过
vars
属性在任务级别注册变量

例如:

- name: This will display 'bar', the content of foo
  debug:
    msg: "{{ foo }}"
  vars:
    foo: 'bar'
在此基础上,您可以很容易地实现您想要的:

- name: Replace IPs
  replace:
    path: /home/ubuntu/cassandra.properties
    regexp: "{{ oldNewIp[0] | trim }}"
    replace: "{{ oldNewIp[1] | trim }}"
    backup: yes
  vars:
    oldNewIp: "{{ item.split(';') }}"
  with_items: "{{ ipPairs.split(',') }}"
请注意,我添加了一些额外的Jinja,以应对以下事实:在示例输入中,逗号后面确实有一些空格

下面是演示此功能的完整示例:
考虑到剧本

- hosts: local
  vars:
    ipPairs: '192.123.12.11;192.123.12.20, 192.123.12.12;192.123.12.19, 192.123.12.13;192.123.12.18'

  tasks:
    - name: Create an example file to replace in
      copy:
        dest: /tmp/ips.txt
        content: ''

    - name: Populate a file with old IPs
      lineinfile:
        path: /tmp/ips.txt
        line: "{{ oldNewIp[0] | trim }}"
      vars:
        oldNewIp: "{{ item.split(';') }}"
      with_items: "{{ ipPairs.split(',') }}"

    - name: Replace IPs
      replace:
        path: /tmp/ips.txt
        regexp: "{{ oldNewIp[0] | trim }}"
        replace: "{{ oldNewIp[1] | trim }}"
        backup: yes
      vars:
        oldNewIp: "{{ item.split(';') }}"
      with_items: "{{ ipPairs.split(',') }}"
我有以下剧本:

/ # ansible-playbook -i inventory.yaml play.yaml 

PLAY [local] *********************************************************************************************

TASK [Gathering Facts] ***********************************************************************************
[WARNING]: Platform linux on host local is using the discovered Python interpreter at /usr/bin/python3,
but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more
information.
ok: [local]

TASK [Create an example file to replace in] **************************************************************
changed: [local]

TASK [Populate a file with old IPs] ***********************************************************************
changed: [local] => (item=192.123.12.11;192.123.12.20)
changed: [local] => (item= 192.123.12.12;192.123.12.19)
changed: [local] => (item= 192.123.12.13;192.123.12.18)

TASK [Replace IPs] ***************************************************************************************
changed: [local] => (item=192.123.12.11;192.123.12.20)
changed: [local] => (item= 192.123.12.12;192.123.12.19)
changed: [local] => (item= 192.123.12.13;192.123.12.18)

PLAY RECAP ***********************************************************************************************
local                      : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

/ # cat /tmp/ips.txt
192.123.12.20
192.123.12.19
192.123.12.18

为了实现这一点,您可以使用一些非常有用的方法,即您可以通过
vars
属性在任务级别注册变量

例如:

- name: This will display 'bar', the content of foo
  debug:
    msg: "{{ foo }}"
  vars:
    foo: 'bar'
在此基础上,您可以很容易地实现您想要的:

- name: Replace IPs
  replace:
    path: /home/ubuntu/cassandra.properties
    regexp: "{{ oldNewIp[0] | trim }}"
    replace: "{{ oldNewIp[1] | trim }}"
    backup: yes
  vars:
    oldNewIp: "{{ item.split(';') }}"
  with_items: "{{ ipPairs.split(',') }}"
请注意,我添加了一些额外的Jinja,以应对以下事实:在示例输入中,逗号后面确实有一些空格

下面是演示此功能的完整示例:
考虑到剧本

- hosts: local
  vars:
    ipPairs: '192.123.12.11;192.123.12.20, 192.123.12.12;192.123.12.19, 192.123.12.13;192.123.12.18'

  tasks:
    - name: Create an example file to replace in
      copy:
        dest: /tmp/ips.txt
        content: ''

    - name: Populate a file with old IPs
      lineinfile:
        path: /tmp/ips.txt
        line: "{{ oldNewIp[0] | trim }}"
      vars:
        oldNewIp: "{{ item.split(';') }}"
      with_items: "{{ ipPairs.split(',') }}"

    - name: Replace IPs
      replace:
        path: /tmp/ips.txt
        regexp: "{{ oldNewIp[0] | trim }}"
        replace: "{{ oldNewIp[1] | trim }}"
        backup: yes
      vars:
        oldNewIp: "{{ item.split(';') }}"
      with_items: "{{ ipPairs.split(',') }}"
我有以下剧本:

/ # ansible-playbook -i inventory.yaml play.yaml 

PLAY [local] *********************************************************************************************

TASK [Gathering Facts] ***********************************************************************************
[WARNING]: Platform linux on host local is using the discovered Python interpreter at /usr/bin/python3,
but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more
information.
ok: [local]

TASK [Create an example file to replace in] **************************************************************
changed: [local]

TASK [Populate a file with old IPs] ***********************************************************************
changed: [local] => (item=192.123.12.11;192.123.12.20)
changed: [local] => (item= 192.123.12.12;192.123.12.19)
changed: [local] => (item= 192.123.12.13;192.123.12.18)

TASK [Replace IPs] ***************************************************************************************
changed: [local] => (item=192.123.12.11;192.123.12.20)
changed: [local] => (item= 192.123.12.12;192.123.12.19)
changed: [local] => (item= 192.123.12.13;192.123.12.18)

PLAY RECAP ***********************************************************************************************
local                      : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

/ # cat /tmp/ips.txt
192.123.12.20
192.123.12.19
192.123.12.18

开始时你说
old:new
,最后你说
old;new
你的分隔符是什么?一个半音符号
或一个冒号
?我假设你的输入是正确的,你的打字只是在你问题的介绍中,如果我的假设一开始就不正确,请随意纠正我你说
old:new
,最后你说
old;new
你的分隔符是什么?一个半音符号
或一个冒号
?我假设你的输入是正确的,你的打字只是在你问题的介绍中,如果我的假设不正确,请随意纠正我,是这样做的!当然,是这样做的!