I am trying to create an environment specific configuration file consisting of key = value pairs that are read from a csv file. The csv file contains configuration settings for many environments and I need to generate a configuration file for each environment depending on inventory used by playbook.
The CSV file settings.csv
Key|siteA-test|siteA-prod|siteB-test|siteB-prod|Comment
param1|true|true|false|false|Comment for param1
param2|http://test|http://prod|||Comment for param2
param3|ansible_variable_A_test|ansible_variable_A_prod|ansible_variable_B_test|ansible_variable_B_prod|Variable must be taken from secret vault
param4|ansible_variable_C_test|ansible_variable_C_prod|ansible_variable_D_test| ansible_variable_D_prod|Another variable from variable file
Intended file for siteA-test would look like:
param1=true # Comment for param1
param2=http://test # Comment for param2
param3=secret_value_from_vault # Variable must be taken from secret vault
param4=1984 # Another variable from variable file
I will write this out to a file using lineinfile module.
So far I have (somewhat ugly) figured out how to get values for specific environments, by creating a dictionary of keys first and then iterating over that dict. I don't like that I have to read CSV file twice, but this works.
- hosts: hostgroupA
tasks:
- name: Read CSV file and return a dictionary
community.general.read_csv:
path: settings.csv
delimiter: '|'
key: Key
strict: yes
register: config
delegate_to: localhost
- ansible.builtin.debug:
msg: "Key {{ lookup('csvfile', '{{item.key}} file=settings.csv col=1 delimiter=|') }}, value: {{ lookup('csvfile', '{{item.key}} file=settings.csv col=2 delimiter=|')}} "
loop: "{{ config.dict| dict2items }}"
delegate_to: localhost
The problem is that I don't know if it is possible to replace variable names for param3 and param4 with real values from vaults or other variable files.
shellmodule to parse the file withsed,awk,cut,tr, or other tools and write the vars file in yaml format, then have another play to load that newly created file.