Ansible templating skips string after a dash

倖福魔咒の 提交于 2021-01-05 10:47:13

问题


I am templating a file with a set of variables in Ansible.

A few entries in my defaults/main.yaml file are :

jenkins_plugins:
  'ant': '1.8'
  'antisamy-markup-formatter': '1.5'
  'apache-httpcomponents-client-4-api': '4.5.3-2.1'
  'kubernetes': '1.3'

One of this key-value pair is supposed to be injected in this line in my template file config.xml.j2 :

<markupFormatter class="hudson.markup.RawHtmlMarkupFormatter" plugin="antisamy-markup-formatter@{{ jenkins_plugins.antisamy-markup-formatter }}">

So basically my end result should look like :

<markupFormatter class="hudson.markup.RawHtmlMarkupFormatter" plugin="antisamy-markup-formatter@1.5">

But when I run the playbook that calls this templating file, I get the ERROR :

TASK [jenkins : Generate config.xml file.] ****************************************
fatal: [default]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'antisamy'"}

If I remove all the dashes -, it will work fine but that is something I cannot do since I also have to download these plugins and I need to curl on the correct names with dashes.

Why is the jinja templating skipping everything after - ?


回答1:


Python doesn't like dashes - in attribute names, but you can use the map syntax:

{{ jenkins_plugins["antisamy-markup-formatter"] }}

If you have VARIABLE NAMES that contain a dash, you can use vars:

{{ vars["jenkins-plugins"] }}

However, be aware that if you try to define a variable that contains a dash, ansible will throw an error saying that variable names cannot contain dashes. This only works when using "defaults" - but I guess it's a bug.




回答2:


Supernice. But what if the first part (jenkins_plugins) had a dash in it (jenkins-plugins)?

Fix for the above question by @Andreas Wederbrand

Use .get() on the dict to fetch the key.

[root@ip-172-31-39-79 ~]# ansible --version
ansible 2.8.5


[root@ip-172-31-39-79 ~]# cat a.yml 
---
- name: "Getting package facts"
  hosts: localhost
  tasks: 

    - name: Gather the rpm package facts
      package_facts:
        manager: auto

    - name: Print the rpm package facts
      debug:
        var: ansible_facts.packages.zlib[0].name

    - name: Print the rpm package facts
      debug:
        msg: "{{ ansible_facts.packages.get('vim-minimal')[0].name }}"


PLAY [Getting package facts] **************************************************************

TASK [Gathering Facts] ********************************************************************
ok: [localhost]

TASK [Gather the rpm package facts] *******************************************************
ok: [localhost]

TASK [Print the rpm package facts] ********************************************************
ok: [localhost] => {
    "ansible_facts.packages.zlib[0].name": "zlib"
}

TASK [Print the rpm package facts] ********************************************************
ok: [localhost] => {
    "msg": "vim-minimal"
}

PLAY RECAP ********************************************************************************
localhost                  : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  



回答3:


Older ansible versions does not allow - dash/hyphen in variables. Ansible 2.8.5 seems allow dash/hyphen in the variables.



来源:https://stackoverflow.com/questions/52396669/ansible-templating-skips-string-after-a-dash

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!