我们如何在使用循环时在ansible剧本中调用变量 [英] How we can call a variable in ansible playbook while using loops

查看:129
本文介绍了我们如何在使用循环时在ansible剧本中调用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文件,这些文件包含server namesserver IP's,我想根据需要在两个文件中更改/替换某些特定的server namesIP addressees.

I have two files where these file contains server names and server IP's, I want to change/replace some specific server names and IP addressees in both the files based on the requirement.

这与有关发布<-,因为它被要求打开一个新帖子.

This is related to This Post <-- as it was asked to open a new post.

在下面的示例文件(file & file2)中,我需要执行以下操作..

In the Below example files(file & file2) i need to do follows..

1-在file1和fil2中,我必须将fostrain01.example.com替换为dbfostrain01.example.com.

1 - In file1 and fil2 i have to replace fostrain01.example.com with dbfostrain01.example.com.

2-在另一行中,我也必须在两个文件中都将171.20.20.18替换为172.20.20.18.

2 - Where in another line i have to replace 171.20.20.18 with 172.20.20.18 in both the files as well.

# cat /etc/file1
fostrain01.example.com
fostrain02.example.com
ServerIPS 171.20.20.16 171.20.20.17 171.20.20.18 171.20.20.19 171.20.20.20

# cat /etc/fil2
fostrain01.example.com
fostrain02.example.com  
ServerIPS 171.20.20.16 171.20.20.17 171.20.20.18 171.20.20.19 171.20.20.20

我的剧本:

---
- name: Replace file contents
  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 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 }}" }

在上面的剧本中,我已经定义了每个部分的变量,如您所见.

In the above playbook, i have defined the variables for each sections as you can see.

在使用loop时,我在playbook的替换模块的path部分的path部分中缺少对How i can use or reference files variable的了解.

I am missing to understand about How i can use or reference files variable in the path section of replace module in my playbook above while using loop.

请澄清一下,我说的是下面的一个.

Just to clarify, i'm talking about below one..

    files:
      - /etc/file1
      - /etc/file2

我认为这适合上述剧本中的方法,因为我知道这样做的另一种方法.

I am looking this to be fitted within the approach in the above playbook, as i know the other way around of doing it.

对不起,如果我不能说清楚的话.

I am sorry, if i could not make it more clear.

推荐答案

文件是一个变量列表,就像任何变量列表一样,这些项目都可以通过索引为0的键进行访问.

So file is a list of variable, as any list of varibales, the items are accessible via a 0-indexed key.

  • 因此,在您的情况下,files列表的第一个元素包含/etc/file1是 可以通过

  • So in you case, the first element of the files list, containing /etc/file1 is accessible either via

files.0

files[0]

  • 包含/etc/file1的第二个可以通过

  • The second, containing /etc/file1 is accessible either via

    files.1
    

    files[1]
    

  • 以此类推,等等.

  • And so on, and so forth.

    但是还有很多其他方法可以做到这一点:

    But there is a lot of other ways to do this:

    1. 您可以使用 product 来合并两个列表
    2. 您可以使用loop_control参数 extended loop_control的a>参数,它提供扩展变量ansible_loop.index0
    1. you could use product to merge your two lists
    2. you could use the loop_control parameter index_var to create your own index
    3. you could use the extended parameter of loop_control, that provides the extended variable ansible_loop.index0


    所有这些,要使它更通用一点,如果我是你,我会使用


    All this said, to make it a little bit more generic, if I were you, I would use the product filter and go with a more simpler:

    - name: Replace elements in file
      replace:
        path: "{{ item.0 }}"
        regexp: "{{ item.1.from }}"
        replace: "{{ item.1.to }}"
        backup: yes
      vars:
        paths:
          - /etc/file1
          - /etc/file2
        replaces:
          - from: "fostrain01.example.com"
            to: "dbfoxtrain01.example.com"
          - from: "^(.*)171\\.20\\.20\\.18(.*)$"
            to: "\\g<1>172.20.20.18\\g<2>"
      loop: "{{ paths | product(replaces) | list }}"
    

    因为,那么没有理由要转换您的列表,它已经可以用于您的replace任务.
    请注意,可以在播放或任务级别定义变量,如果在其他任务中需要replaces变量,只需将其恢复到播放级别即可.

    Because, then there is no reason to transform your list, it is already has it should for your replace task.
    Mind that variables can be defined both at play or task level, if you need the replaces variable in other tasks, just bring it back up to the play level.

    这篇关于我们如何在使用循环时在ansible剧本中调用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆