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

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

问题描述

我有两个文件,其中这些文件包含 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.

我在上面的 playbook 的替换模块的 path 部分中不了解 如何使用或引用文件变量 而使用 loop.

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.

  • 所以在你的情况下,包含 /etc/file1files 列表的第一个元素是可以通过

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

files.0

files[0]

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

    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 参数 index_var 创建自己的索引
    3. 您可以使用 extended<loop_control 的/code> 参数,提供扩展变量 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


    说了这么多,为了让它更通用一点,如果我是你,我会使用 product 过滤器并使用更简单的过滤器:


    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 playbook 中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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