AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1080617
Accepted
Bogdan Stoica
Bogdan Stoica
Asked: 2021-10-16 01:13:43 +0800 CST2021-10-16 01:13:43 +0800 CST 2021-10-16 01:13:43 +0800 CST

ansible main.yml if else 条件

  • 772

所以我正在运行一个 ansible 角色,它在 defaults/role 文件夹中有一个文件 main.yml 。该文件的内容是这样的:

---
api_secrets:
  'API_PROFILE': "{{ api_profile }}"
  'SERVER_ADDRESS': "{{ server_address }}"
  'MGMT_SERVER_ADDRESS': "{{ management_server_address }}"

现在我想在 MGMT_SERVER_ADDRESS 之后包含 api_secrets 块,如下所示:

{% if '"port" in mgmt_ports' %}
'MGMT_SERVER_PORT': "{{ management_server_port1 }}"
'MGMT_SERVER_USER': "{{ user1 }}"
{% else %}
'MGMT_SERVER_PORT': "{{ management_server_port2 }}"
'MGMT_SERVER_USER': "{{ user2 }}"
{% endif %}

从这里开始,在服务器上创建一个文件,其中包含上述内容,当然用它们的实际值替换变量。

无论我如何尝试,它总是会导致不同的错误。我尝试使用“{% if ... endif %}”,也使用 ''

错误是这样的:

ERROR! Syntax Error while loading YAML.
  found character that cannot start any token

The error appears to be in '/opt/ansible/roles/api/defaults/main.yml': line 55, column 2, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

{% if '"port" in mgmt_ports' %}
 ^ here

我也试过这样:

   "{% if (port in mgmt_ports) %}
   'MGMT_SERVER_PORT': "{{ management_server_port1 }}"
   {% else %}
   'MGMT_SERVER_PORT': "{{ management_server_port2 }}"
   {% endif %}"

在这种情况下,错误是:

ERROR! Syntax Error while loading YAML.
  could not find expected ':'

The error appears to be in '/opt/ansible/roles/api/defaults/main.yml': line 56, column 24, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  "{% if (port in mgmt_ports) %}
  'MGMT_SERVER_PORT': "{{ management_server_port1 }}"
                       ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:

    with_items:
      - {{ foo }}

Should be written as:

    with_items:
      - "{{ foo }}"

这样做的正确方法是什么?

我知道使用 jinja2 模板会更容易,但是剧本是这样创建的,我必须坚持这种方法。

ansible ansible-playbook
  • 2 2 个回答
  • 955 Views

2 个回答

  • Voted
  1. Best Answer
    flowerysong
    2021-10-16T07:14:44+08:002021-10-16T07:14:44+08:00

    变量模板化发生在 YAML 解析步骤之后,因此您不能以这种方式使用它来模板化 YAML。

    最简单的方法是将条件移动到各个 Jinja 表达式中:

    api_secrets:
      API_PROFILE: "{{ api_profile }}"
      SERVER_ADDRESS: "{{ server_address }}"
      MGMT_SERVER_ADDRESS: "{{ management_server_address }}"
      MGMT_SERVER_PORT: "{{ management_server_port1 if 'port' in mgmt_ports else management_server_port2 }}"
      MGMT_SERVER_USER: "{{ user1 if 'port' in mgmt_ports else user2 }}"
    

    您也可以使用 Jinja 语句,但这会使相同结果的值更长一些。

    api_secrets:
      API_PROFILE: "{{ api_profile }}"
      SERVER_ADDRESS: "{{ server_address }}"
      MGMT_SERVER_ADDRESS: "{{ management_server_address }}"
      MGMT_SERVER_PORT: "{% if 'port' in mgmt_ports %}{{ management_server_port1 }}{% else %}{{ management_server_port2 }}{% endif %}"
      MGMT_SERVER_USER: "{% if 'port' in mgmt_ports %}{{ user1 }}{% else %}{{ user2 }}{% endif %}"
    
    • 1
  2. Barnabas Busa
    2021-10-16T03:21:53+08:002021-10-16T03:21:53+08:00

    Ansible 并不意味着处理 If-Else-Statements。

    正如您在问题底部提到的那样,使用 jinja2 模板会更容易,但不仅会更容易,而且会使其成为正确的方法。

    因此,不要试图用 if-else 语句来搞乱你的 yaml 文件,而是使用所需的模板参数创建一个 jinja2 文件(看起来你几乎得到了 jinja2 模板结构!)并使用它创建一个配置文件。

    然后您可以执行模板命令以使用正确的参数将模板包含在正确的位置。

    “剧本是这样创建的,我必须坚持这种方法”喊道,提出该政策的人要么不安全,要么只是个孩子。

    • 0

相关问题

  • 重复的 Ansible 任务

  • 无法形成站点中的文件的链接,该链接可用于使用 ansible 在远程服务器中启用的目录站点?

  • 如何执行 ansible 的特定角色?

  • Ansible 和 rbash

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve