For example, given the configuration file
shell> cat conf.ini
key1=alpha=beta=charlie
key2=value2
key3= value3
The variable below
config_vars: "{{ dict(lookup('file', 'conf.ini').split('\n')|
map('split', '=', 1)|
map('map', 'trim')) }}"
expands to
config_vars:
key1: alpha=beta=charlie
key2: value2
key3: value3
You can remove the last map/trim filter from the pipe if you're sure there are no spaces in the configuration file. But, to be on the safe side, I'd keep it.
Config file at the remote host
The solution in the previous section works at the controller only because the lookup plugins execute at the localhost. Fetch the files first if they are at the remote hosts. For example, given the configuration files
shell> ssh admin@test_11 cat /tmp/conf.ini
key1=alpha=beta=charlie
key2=value2
key3= value3
shell> ssh admin@test_12 cat /tmp/conf.ini
key1=value1
key2= value2
key3=alpha=beta=charlie
the playbook below
- hosts: test_11,test_12
vars:
conf_ini_path: "conf_ini/{{ inventory_hostname }}/tmp/conf.ini"
config_vars: "{{ dict(lookup('file', conf_ini_path).split('\n')|
map('split', '=', 1)|
map('map', 'trim')) }}"
tasks:
- fetch:
dest: conf_ini
src: /tmp/conf.ini
- debug:
var: config_vars
gives (abridged)
TASK [debug] ***********************************************
ok: [test_11] =>
config_vars:
key1: alpha=beta=charlie
key2: value2
key3: value3
ok: [test_12] =>
config_vars:
key1: value1
key2: value2
key3: alpha=beta=charlie
Alow no value
For example, given the configuration file
shell> cat conf.ini
key1=alpha=beta=charlie
key2=value2
key3= value3
key4
The variable below
config_vars: "{{ dict(lookup('file', 'conf.ini').split('\n')|
map('split', '=', 1)|
map('map', 'trim')|
map('json_query', '[]|[[0], [1]]')) }}"
expands to
config_vars:
key1: alpha=beta=charlie
key2: value2
key3: value3
key4: null