简体   繁体   English

使用条件设置变量时出错 - Linux上的Ansible Playbook

[英]Error when set variable with condition - Ansible Playbook on Linux

Getting error when using an if/else condition to set a variable in a playbook on Red Hat. 使用if / else条件在Red Hat上的playbook中设置变量时出错。

I tried a few different permutations of quotes, brackets etc. 我尝试了一些不同的引号,括号等排列。

Executed as "ansible-playbook -e env=dev playbook.yaml" 被执行为“ansible-playbook -e env = dev playbook.yaml”

Set Vars: 设置Vars:

vars:

  certenv: "{{ '-eng.dev.' if {{env}} == 'eng' else '.dev.' if {{env}} == 'dev' else '.uat.' if {{env}} == 'stg' else '.prd.' if {{env}} == 'prd' }}"

Task 任务

   - name: Update server.xml Cert

     lineinfile:

      dest: "{{tomcat}}/conf/server.xml"

      regexp: '^(.*)certificateFile(.*)$'

      line: 'certificateFile="{{tomcat}}/webapps/{{appwar}}/certificates/app{{certenv}}domain.cer"'

Error 错误

fatal: [localhost]: FAILED! => {"msg": "An unhandled exception occurred while templating '{{ '-eng.dev.' if {{env}} == 'eng' else '.dev.' if {{env}} == 'dev' else '.uat.' if {{env}} == 'stg' else '.prd.' if {{env}} == 'prd' }}'. 
Error was a <class 'ansible.errors.AnsibleError'>, original message: template error while templating string: expected token ':', got '}'. String: {{ '-eng.dev.' if {{env}} == 'eng' else '.dev.' if {{env}} == 'dev' else '.uat.' if {{env}} == 'stg' else '.prd.' if {{env}} == 'prd' }}"}

Expecting the certenv variable to be set as ".dev." 期望将certenv变量设置为“.dev”。 for example. 例如。

An option would be to use selectattr and map . 一个选项是使用selectattrmap The play below 下面的剧本

vars:
  cert_options:
    eng: "-eng.dev."
    dev: ".dev."
    stg: ".uat."
    prd: ".prd."
  env: "stg"
tasks:
  - set_fact:
      certenv: "{{ cert_options|dict2items|selectattr('key', 'match', env)|map(attribute='value')|list }}"
  - debug:
      var: item
    loop: "{{ certenv }}"

gives: 得到:

"item": ".uat."

This hint might help to solve the problem. 此提示可能有助于解决问题。

The fixed syntax of the code is below. 代码的固定语法如下。 (not tested) (未测试)

- name: Update server.xml Cert
  lineinfile:
    dest: "{{ tomcat }}/conf/server.xml"
    regexp: "^(.*)certificateFile(.*)$"
    line: "certificateFile={{ tomcat }}/webapps/{{ appwar }}/certificates/app{{ certenv }}domain.cer"

For jinja2, the syntax is: 对于jinja2,语法是:

vars:
  certenv: "{% if env == 'eng' %}-eng.dev.{% elif env == 'dev' %}.dev.{% elif env == 'stg' %}.uat.{% elif env == 'prd' %}.prd.{% endif %}"

You do not need to put the variables in {{...}} because they are inside the jinja2 markers {%...%} . 您不需要将变量放在{{...}}因为它们位于jinja2标记{%...%}

You can also make it more readable: 您还可以使其更具可读性:

vars:
  certenv:
    "{% if env == 'eng' %}
        -eng.dev.
     {% elif env == 'dev' %}
        .dev.
     {% elif env == 'stg' %}
        .uat.
     {% elif env == 'prd' %}
        .prd.
     {% endif %}"

As per comments: Modify your variable to 根据评论:将变量修改为

certenv: "{{ '-eng.dev.' if env == 'eng' else '.dev.' if env == 'dev' else '.uat.' if env == 'stg' else '.prd.' if env == 'prd' }}"

and run your playbook with: 并运行你的剧本:

ansible-playbook myplaybook.yml -e "env=dev".

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM