1

I want to replace a string in between two @ characters

Something like this

Hi I am @Something.great@ from a planet @planet.outside.earth@

from a properties file where values are

Something.great = Niel Armstrong planet.outside.earth = Mars

So this should convert the above text to

Hi I am Niel Armstrong from a planet Mars

I have to achieve this using Ansible

Using lookup and replace

EDIT 1:

No For the two answers below. I don't have any idea what keys I have in between N number of files. I have to search a regex in folder having 100 files may contain 1000 keys in between @myKey@ .

So I have to First search what is the value in file in this case myKey which has to come from some regex search and then lookup that search value in a lookup properties file where the value of myKey is present.

My properties file looks like this.

myKey=Ankit
YourKey=Kevin
OtherKey=Vladimir

3 Answers3

2

Use replace module, if the string is in a file

- replace:
    path: "{{ path_to_template }}"
    regexp: "{{ item.regexp }}"
    replace: "{{ item.replace }}"
  loop:
    - regexp: '@Something.great@'
      replace: 'Niel Armstrong'
    - regexp: '@planet.outside.earth@'
      replace: 'Mars'

, or use regex_replace filter if the string is available in a variable

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


If you replace the markers '@@' with '{{ }}' the variables will be substituted. For example
  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

, or with the template

$ cat test.j2 
Hi I am {{ Something.great }} from a planet {{ planet.outside.earth }}

and the task below

- template:
    src: test.j2
    dest: "{{ path_to_file_with_the_substituted_text }}"
Vladimir Botka
  • 27,477
  • 4
  • 17
  • 32
1

Answer:

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

Output:

 TASK [debug] 
 "msg": "Hi I am Niel Armstrong from a planet Mars"
Kevin C
  • 2,487
  • 5
  • 20
  • 42
1

I have found the solution.

First I will search the directory for the specific regex and store it in Register.

  1.   - name: Search Tokens
            find:
              paths: "{{ HOME_DIR }}"
              hidden: yes
              contains: "^.*(@.*@).*$"
              recurse: no
              use_regex: yes
              file_type: file
              patterns: "^(?!.*pattern\\.sh).*$"
            register: filesname
    
  2. I will write the list of files in a separate text file config.txt

  - name: Write list of files to be read for tokens
      lineinfile: 
        path: /config.txt 
        line: "{{item.file.path}}"
        create: yes 
        state: present
      with_items: 
        - "{{filesname}}"
  1. Now I will Loop Through the config file for every file inside read-replaceyaml
 - name: Loop through the tasks
      include_tasks: read-replace.yaml
      with_items: "{{ lookup('file', '/config.txt').splitlines() }}"
      loop_control:
        loop_var: file
  1. In read-replace.yml

I will read file content and replace the token at the same time

 - 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