Skip to content

Instantly share code, notes, and snippets.

@darKoram
Created September 24, 2014 18:46

Revisions

  1. darKoram created this gist Sep 24, 2014.
    47 changes: 47 additions & 0 deletions playbook
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    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.
    42 changes: 42 additions & 0 deletions stdout
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    % 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