Home > other >  Ansible: How to take values from yaml input file and populate in output file of indeterminate format
Ansible: How to take values from yaml input file and populate in output file of indeterminate format

Time:06-07

I have 2 files, one of an indeterminate but consistent format (which could be some sort of html format...), and one of a yaml format. I want the values of the yaml file to be input into the other file. I've used include_vars for the yaml input file and can reference/manipulate them properly. It's the output file, and the value replacement process which is the problem.

The overall question is: In an ansible playbook, is it possible using a loop with something like "blockinfile module" or "lineinfile module" to facilitate this process for over 100 values in both files?

Examples of files: Input file/variables in yaml format:

---
client_host_port: 6000
first_eth_port: 55
this_value_for_stuff: 233
this_value_for_stuff_2: 133

Output file examples in the indeterminate/oddhtml format:

<client_host_port             value="fillwithyamlfilevalue"/>
<first_eth_port               value="fillwithyamlfilevalue"/>
Purpose: To define for the system the first ethernet port to use.
<this_value_for_stuff         value="fillwithyamlfilevalue"/>
<this_value_for_stuff_2       value="fillwithyamlfilevalue"/>
Note: value 2 is a backup value.

Etc, etc so on and so forth. In the real files, there are hundreds of values, but the each match "keyname" value defined in the input file will have a counterpart "keyname" in the output file. Notice they will also have some comments beneath some of the values. These can and should be ignored in this process.

Right now I'm "guessing" that the line to be replaced is done probably with a regex based on the variable name from the input file, but not really sure as to the best method to do this. I'm hoping there is a way to do this with a loop as opposed to 100 tasks.
Also what's the best way to reference the output file's values? Just with some sort of regex? It doesn't really matter as long as it is consistent and easy to work with. Unfortunately I can't change the output file format, it's used by a program made long ago which cannot be easily changed.
Any suggestions would be appreciated, and thanks in advance!

(Note, I know the yaml file isn't in fully correct format concerning whitespace, but ansible can still parse it just fine, and makes it easier with regex in theory.)

CodePudding user response:

Given:

  1. the following vars/my_values.yml variable file:
    ---
    client_host_port: 6000
    first_eth_port: 55
    this_value_for_stuff: 233
    this_value_for_stuff_2: 133
    
  2. the following templates/my_resultfile.weirdo.j2 template
    {% for name, value in my_options.items() %}
    <{{ "%-25s" | format(name) }} value="{{ value }}"/>
    {% endfor %}
    
  3. and the following my_playbook.yml
    ---
    - hosts: localhost
      gather_facts: false
    
      tasks:
        - name: load variables from yaml file
          include_vars:
            file: my_values.yml
            name: my_options
    
        - name: output vars in weirdo format with template
          template:
            src: my_result_file.weirdo.j2
            dest: /tmp/my_result_file.weirdo
    

Running the playbook gives:

$ ansible-playbook my_playbook.yml 

PLAY [localhost] *************************************************************

TASK [load variables from yaml file] *****************************************
ok: [localhost]

TASK [output vars in weirdo format with template] *******************************
changed: [localhost]

PLAY RECAP *******************************************************************
localhost: ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

And results in the following templated file:

$ cat /tmp/my_result_file.weirdo 
<client_host_port          value="6000"/>
<first_eth_port            value="55"/>
<this_value_for_stuff      value="233"/>
<this_value_for_stuff_2    value="133"/>
  • Related