Skip to content

Instantly share code, notes, and snippets.

@darKoram
Created September 24, 2014 18:46
Show Gist options
  • Save darKoram/3f2fbcc9b3a003546f3c to your computer and use it in GitHub Desktop.
Save darKoram/3f2fbcc9b3a003546f3c to your computer and use it in GitHub Desktop.
getting dictionaries templated without u" prefix
The goal is to define a dictionary like
dict1:
int1: 5
str1: "four"
and be able to template output the dictionary as a whole (not leaf by leaf) such that the output looks like
dict1: {"int1": 5, "srt1": "four"}
The actual output prefixes the values on the RHS of the : with a u so it looks like
dict1: {"int1": u"5", "srt1": u"four"}
The output is a conf file and the code that reads it chokes on the u' prefix.
To reproduce:
cat templ.yml
int1: {{int1}}
srt1: {{str1}}
dict1: {{dict1}}
int1_accessed_by_dict: {{dict1['_int1']}}
cat test.yml
# test.yml
- hosts: localhost
vars:
int1: 5
str1: "four"
dict1:
_int1: "{{int1}}"
_str1: "{{str1}}"
tasks:
- debug: msg="{{int1}}"
- debug: msg="{{str1}}"
- debug: msg="{{dict1}}"
- debug: msg="{{dict1['_int1']}}"
- name: test templating
local_action: template src=templ1.yml dest="{{ANSIBLE_21CT_HOME}}/templ_out.yml"
- name: show result
local_action: shell cat "{{ANSIBLE_21CT_HOME}}/templ_out.yml"
Below is the output.
% ansible-playbook -i hosts test.yml
PLAY [localhost] **************************************************************
GATHERING FACTS ***************************************************************
ok: [localhost]
TASK: [debug msg="5"] *********************************************************
ok: [localhost] => {
"msg": "5"
}
TASK: [debug msg="four"] ******************************************************
ok: [localhost] => {
"msg": "four"
}
TASK: [debug msg="{'_int1': u'5', '_str1': u'four'}"] *************************
ok: [localhost] => {
"msg": "{'_int1': u'5', '_str1': u'four'}"
}
TASK: [debug msg="5"] *********************************************************
ok: [localhost] => {
"msg": "5"
}
TASK: [test templating] *******************************************************
changed: [localhost -> 127.0.0.1]
TASK: [show result] ***********************************************************
changed: [localhost -> 127.0.0.1]
PLAY RECAP ********************************************************************
localhost : ok=7 changed=2 unreachable=0 failed=0
% cat templ_out.yml
int1: 5
srt1: four
dict1: {'_int1': u'5', '_str1': u'four'}
int1_accessed_by_dict: 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment