Python 我想通过使用ansible在属性文件中查找来替换分隔符之间的字符串

Python 我想通过使用ansible在属性文件中查找来替换分隔符之间的字符串,python,ansible,devops,Python,Ansible,Devops,我想替换两个@字符之间的字符串 像这样的 嗨,我是@Something.great@来自行星@planet.outside.earth@ 从属性文件 价值在哪里 很好,尼尔·阿姆斯特朗 planet.outside.earth=火星 因此,这应该将上面的文本转换为 嗨,我是来自火星的尼尔·阿姆斯特朗 我必须使用Ansible来实现这一点 使用查找和替换 编辑1: 下面的两个答案是否定的。 我不知道我在N个文件之间有什么密钥。 我必须在包含100个文件的文件夹中搜索正则表达式,其中@myKey@之

我想替换两个@字符之间的字符串

像这样的

嗨,我是@Something.great@来自行星@planet.outside.earth@

从属性文件 价值在哪里

很好,尼尔·阿姆斯特朗 planet.outside.earth=火星

因此,这应该将上面的文本转换为

嗨,我是来自火星的尼尔·阿姆斯特朗

我必须使用Ansible来实现这一点

使用查找和替换

编辑1:

下面的两个答案是否定的。 我不知道我在N个文件之间有什么密钥。 我必须在包含100个文件的文件夹中搜索正则表达式,其中@myKey@之间可能包含1000个键

因此,我必须首先搜索文件中的值,在本例中是myKey,它必须来自某个正则表达式搜索,然后在存在myKey值的查找属性文件中查找该搜索值

我的属性文件如下所示

myKey=Ankit
YourKey=Kevin
OtherKey=Vladimir
答复:

---
- hosts: local_test # local_test
  vars:
    string: "Hi I am @Something.great@ from a planet @planet.outside.earth@"
    something:
      great: "Niel Armstrong"
    planet:
      outside:
        earth: "Mars"
  gather_facts: false
  tasks:
    - debug:
        msg: "{{ string | replace('@Something.great@', something.great) | replace ('@planet.outside.earth@', planet.outside.earth) }}"
输出:

 TASK [debug] 
 "msg": "Hi I am Niel Armstrong from a planet Mars"
答复:

---
- hosts: local_test # local_test
  vars:
    string: "Hi I am @Something.great@ from a planet @planet.outside.earth@"
    something:
      great: "Niel Armstrong"
    planet:
      outside:
        earth: "Mars"
  gather_facts: false
  tasks:
    - debug:
        msg: "{{ string | replace('@Something.great@', something.great) | replace ('@planet.outside.earth@', planet.outside.earth) }}"
输出:

 TASK [debug] 
 "msg": "Hi I am Niel Armstrong from a planet Mars"
如果字符串在文件中,请使用模块

- replace:
    path: "{{ path_to_template }}"
    regexp: "{{ item.regexp }}"
    replace: "{{ item.replace }}"
  loop:
    - regexp: '@Something.great@'
      replace: 'Niel Armstrong'
    - regexp: '@planet.outside.earth@'
      replace: 'Mars'
,如果变量中有字符串,则使用筛选器

- set_fact:
    string: "{{ string | regex_replace( item.regexp, item.replace) }}"
  loop:
    - regexp: '@Something.great@'
      replace: 'Niel Armstrong'
    - regexp: '@planet.outside.earth@'
      replace: 'Mars'

如果将标记“@@”替换为“{}”,则变量将被替换。比如说

  vars:
    string: "Hi I am {{ Something.great }} from a planet {{ planet.outside.earth }}"
    Something:
      great: "Niel Armstrong"
    planet:
      outside:
        earth: "Mars"
  tasks:
    - debug:
        var: string
,或使用模板

$ cat test.j2 
Hi I am {{ Something.great }} from a planet {{ planet.outside.earth }}
下面的任务是什么

- template:
    src: test.j2
    dest: "{{ path_to_file_with_the_substituted_text }}"
如果字符串在文件中,请使用模块

- replace:
    path: "{{ path_to_template }}"
    regexp: "{{ item.regexp }}"
    replace: "{{ item.replace }}"
  loop:
    - regexp: '@Something.great@'
      replace: 'Niel Armstrong'
    - regexp: '@planet.outside.earth@'
      replace: 'Mars'
,如果变量中有字符串,则使用筛选器

- set_fact:
    string: "{{ string | regex_replace( item.regexp, item.replace) }}"
  loop:
    - regexp: '@Something.great@'
      replace: 'Niel Armstrong'
    - regexp: '@planet.outside.earth@'
      replace: 'Mars'

如果将标记“@@”替换为“{}”,则变量将被替换。比如说

  vars:
    string: "Hi I am {{ Something.great }} from a planet {{ planet.outside.earth }}"
    Something:
      great: "Niel Armstrong"
    planet:
      outside:
        earth: "Mars"
  tasks:
    - debug:
        var: string
,或使用模板

$ cat test.j2 
Hi I am {{ Something.great }} from a planet {{ planet.outside.earth }}
下面的任务是什么

- template:
    src: test.j2
    dest: "{{ path_to_file_with_the_substituted_text }}"

我找到了解决办法

首先,我将搜索特定regex的目录并将其存储在Register中

  • 我将在单独的文本文件config.txt中写入文件列表

  • 现在,我将遍历read replaceyaml中每个文件的配置文件
  • 在read-replace.yml中
  • 我将读取文件内容,同时替换令牌

     - debug:
            msg="{{lookup('file', '{{file}}')}}"
          register: fileContent
    
        - name: Storing the content of the file
          set_fact:
            fileString: "{{fileContent.msg}}"
    
     - name: Find the tokens (@----@) in the fileString
          set_fact:
            tokens: "{{ fileString | regex_findall(regexp)}}"
          vars:
            regexp: '\@.*?\@'
    
        - name: Print Token Objects
          debug:
            var: tokens
    
        - debug:
            msg="{{ lookup('ini', '{{item}} type=properties file=/lookup.properties') }}"
          with_items:
           - "{{tokens}}"
          register: myresult
    
        - debug:
            var : myresult
    
    
       - name: Replace the token in the configuration files
          replace:
            path: {{file}}
            regexp: '{{token}}' 
            replace: "{{ lookup('ini', '{{token}} type=properties file=/lookup.properties')}}"
          with_items: "{{tokens}}"
          loop_control:
            loop_var: token
    
    

    我找到了解决办法

    首先,我将搜索特定regex的目录并将其存储在Register中

  • 我将在单独的文本文件config.txt中写入文件列表

  • 现在,我将遍历read replaceyaml中每个文件的配置文件
  • 在read-replace.yml中
  • 我将读取文件内容,同时替换令牌

     - debug:
            msg="{{lookup('file', '{{file}}')}}"
          register: fileContent
    
        - name: Storing the content of the file
          set_fact:
            fileString: "{{fileContent.msg}}"
    
     - name: Find the tokens (@----@) in the fileString
          set_fact:
            tokens: "{{ fileString | regex_findall(regexp)}}"
          vars:
            regexp: '\@.*?\@'
    
        - name: Print Token Objects
          debug:
            var: tokens
    
        - debug:
            msg="{{ lookup('ini', '{{item}} type=properties file=/lookup.properties') }}"
          with_items:
           - "{{tokens}}"
          register: myresult
    
        - debug:
            var : myresult
    
    
       - name: Replace the token in the configuration files
          replace:
            path: {{file}}
            regexp: '{{token}}' 
            replace: "{{ lookup('ini', '{{token}} type=properties file=/lookup.properties')}}"
          with_items: "{{tokens}}"
          loop_control:
            loop_var: token