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 / 问题 / 1079813
Accepted
Bogdan Stoica
Bogdan Stoica
Asked: 2021-10-07 22:11:37 +0800 CST2021-10-07 22:11:37 +0800 CST 2021-10-07 22:11:37 +0800 CST

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

  • 772

我有这个 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 1 个回答
  • 2491 Views

1 个回答

  • Voted
  1. Best Answer
    flowerysong
    2021-10-08T05:25:02+08:002021-10-08T05:25:02+08:00

    您正在传递一个对象/字典,但您的代码需要一个列表。您需要在传入时将其包装在列表中,或者在使用时考虑不同的可能结构。

    nginx_vhosts您应该首先在模板中直接使用当前循环项来减少引用的位置数量:

    # {{ ansible_managed }}
    
    # redirect www to non-www
    server {
        listen {{ nginx_port }};
        listen [::]:{{ nginx_port }};
        port_in_redirect off;
    
        server_name www.{{ item.name }};
        return 301 http://{{ item.name }}$request_uri;
    }
    

    然后你可以稍微修改你传入的结构:

    "{ 'nginx_vhosts': [{ 'name': 'test101.com', 'state': 'present', 'repo': 'git101', 'branch': 'master' }]}"
    

    或者稍微修改你的循环:

    - 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"
      loop: "{{ [ nginx_vhosts ] | flatten }}"
      when:
        - item.state == 'present'
      notify:
        - nginx-restart
    
    • 1

相关问题

  • 重复的 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