Grazie Phil per la soluzione; nel caso in cui qualcuno ottiene sempre nella stessa situazione come me, ecco una (più complessa) Variante:
---
# this is just to avoid a call to |default on each iteration
- set_fact:
postconf_d: {}
- name: 'get postfix default configuration'
command: 'postconf -d'
register: command
# the answer of the command give a list of lines such as:
# "key = value" or "key =" when the value is null
- name: 'set postfix default configuration as fact'
set_fact:
postconf_d: >
{{
postconf_d |
combine(
dict([ item.partition('=')[::2]|map('trim') ])
)
with_items: command.stdout_lines
Questo darà il seguente output (spogliata per l'esempio):
"postconf_d": {
"alias_database": "hash:/etc/aliases",
"alias_maps": "hash:/etc/aliases, nis:mail.aliases",
"allow_min_user": "no",
"allow_percent_hack": "yes"
}
Andando ancora inoltre, analizzare le liste del 'valore':
- name: 'set postfix default configuration as fact'
set_fact:
postconf_d: >-
{% set key, val = item.partition('=')[::2]|map('trim') -%}
{% if ',' in val -%}
{% set val = val.split(',')|map('trim')|list -%}
{% endif -%}
{{ postfix_default_main_cf | combine({key: val}) }}
with_items: command.stdout_lines
...
"postconf_d": {
"alias_database": "hash:/etc/aliases",
"alias_maps": [
"hash:/etc/aliases",
"nis:mail.aliases"
],
"allow_min_user": "no",
"allow_percent_hack": "yes"
}
alcune cose da notare:
in questo caso è necessaria per "trim" tutto (utilizzando il >-
in YAML e -%}
in Jinja), altrimenti si ottiene un errore del tipo:
FAILED! => {"failed": true, "msg": "|combine expects dictionaries, got u\" {u'...
ovviamente il {% if ..
è lontano
nel caso postfix, val.split(',')|map('trim')|list
potrebbe essere stato semplificato in val.split(', ')
, ma volevo sottolineare il fatto è necessario |list
altrimenti si ottiene un errore del tipo:
"|combine expects dictionaries, got u\"{u'...': <generator object do_map at ...
Spero che questo può aiutare.
Si noti che, poiché ansible v2.2, with_items richiede il jinja2 wrapping esplicito. Quindi il primo esempio sarebbe:
– sarraz