【问题标题】:Ansible templating skips string after a dashAnsible 模板在破折号后跳过字符串
【发布时间】:2018-09-19 01:21:48
【问题描述】:

我在 Ansible 中用一组变量模板化一个文件。

我的defaults/main.yaml 文件中的一些条目是:

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

这个键值对中的一个应该被注入到我的模板文件config.xml.j2的这一行中:

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

所以基本上我的最终结果应该是这样的:

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

但是当我运行调用这个模板文件的剧本时,我得到了错误:

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

如果我删除所有破折号-,它会正常工作,但这是我无法做到的,因为我还必须下载这些插件,并且我需要使用破折号卷曲正确的名称。

为什么 jinja 模板在 - 之后跳过所有内容?

【问题讨论】:

    标签: ansible jinja2


    【解决方案1】:

    Python 不喜欢属性名称中的破折号-,但您可以使用映射语法:

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

    如果您有包含破折号的变量名称,您可以使用vars

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

    但是,请注意,如果您尝试定义一个包含破折号的变量,ansible 会抛出一个错误,指出变量名称不能包含破折号。这仅在使用“默认值”时有效 - 但我想这是一个错误。

    【讨论】:

    • 超级好。但是如果第一部分(jenkins_plugins)中有一个破折号(jenkins-plugins)呢?
    • @AndreasWederbrand 我确实找到了解决方案(请参阅更新的答案),但我认为这是一个错误,甚至可以通过“默认值”定义这样的变量
    【解决方案2】:

    超赞。但是如果第一部分(jenkins_plugins)有一个破折号怎么办? 它(詹金斯插件)?

    修复@Andreas Wederbrand 提出的上述问题

    在 dict 上使用 .get() 来获取密钥。

    [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】:

      较旧的 ansible 版本不允许在变量中使用 - 破折号/连字符。 Ansible 2.8.5 似乎允许在变量中使用破折号/连字符。

      【讨论】:

      • 在这种情况下,参考或文档链接更合适。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-08
      • 1970-01-01
      相关资源
      最近更新 更多