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
    • 最新
    • 标签
主页 / user-393283

Bogdan Stoica's questions

Martin Hope
Bogdan Stoica
Asked: 2021-10-16 01:13:43 +0800 CST

ansible main.yml if else 条件

  • 0

所以我正在运行一个 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 个回答
  • 955 Views
Martin Hope
Bogdan Stoica
Asked: 2021-10-07 22:11:37 +0800 CST

来自 JSON 格式的 Ansible jinja2 模板作为额外变量提供

  • 0

我有这个 jinja2 模板:

# {{ ansible_managed }}

{% for vhost in nginx_vhosts %}
{%- if vhost.name == item.name -%}

# redirect www to non-www
server {
    listen {{ nginx_port }};
    listen [::]:{{ nginx_port }};
    port_in_redirect off;

    server_name www.{{ vhost.name }};
    return 301 http://{{ vhost.name }}$request_uri;
}
{%- endif -%}
{%- endfor -%}

一个带有 yaml 文件vhosts.ym l 的 ansible 角色,其中包含如下定义:

nginx_vhosts:
      - name: "test1.com"
        repo: "git1"
        branch: master
        state: present
      - name: "test2.com"
        repo: "git2"
        branch: master
        state: present
...
      - name: "test101.com"
        repo: "git101"
        branch: master
        state: present

playbook.yml中的一个任务:

- name: "Generate nginx vhost configuration file"
  template:
    src: templates/nginx-vhost-template.j2
    dest: "{{ nginx_vhosts_dir }}/{{ item.name }}.conf"
    owner: "{{ nginx_user }}"
    group: "{{ nginx_group }}"
    mode: 0640
  with_items:
    - "{{ nginx_vhosts }}"
  when:
    - item.state == 'present'
  notify:
    - nginx-restart

我跑了一个像这样的任务:

ansible-playbook -l web1 playbook.yml --tags=nginx-vhost-config

这工作正常,它将从模板在远程服务器上创建一个 nginx vhost 配置文件,如 domain1.com.conf 等,用于所有找到的定义。

假设在 vhosts.yml 文件中我有 test1.com 到 test100.com,我将添加假设 test101.com 并且我想严格为该 test101.com 而不是所有以前的主机运行任务。所以我尝试了这样的事情:

ansible-playbook -l web1 playbook.yml --tags=nginx-vhost-config -e "{ 'nginx_vhosts': { 'name': 'test101.com', 'state': 'present', 'repo': 'git101', 'branch': 'master' }}"

这样做的问题是在尝试替换 jinja2 模板中的值时会导致错误。

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ansible.errors.AnsibleUndefinedVariable: 'ansible.parsing.yaml.objects.AnsibleUnicode object' has no attribute 'name'

我也尝试过使用循环而不是with_items但没有运气。

我知道在使用额外变量时,提供的内容是 JSON 格式,但我无法找到另一种方法将 vhosts.yml 中的内容作为单个条目的额外变量传递。有什么办法可以使它起作用吗?

也许有更好的方法吗?

ansible ansible-playbook jinja jinja2
  • 1 个回答
  • 2491 Views

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