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 / 问题 / 814855
Accepted
williamsowen
williamsowen
Asked: 2016-11-15 11:19:35 +0800 CST2016-11-15 11:19:35 +0800 CST 2016-11-15 11:19:35 +0800 CST

Ansible 列表变量未定义

  • 772

我有一个运行自定义任务来设置 nginx 虚拟主机的剧本:

  tasks:
    - include: tasks/tweaks.yml

在本剧本中,var_files使用了以下内容:

  vars_files:
    - ../config.yml

在config.yml我有以下包含虚拟主机信息的列表:

nginx_hosts_custom:
  - server_name: "mytest.dev"
    root: /drupal/www/mytest
    is_php: true
    common_config: true
    remote_host: "12.34.56.78"
    remote_root: "/web/ubuntu/www/mytest/public"
    pem: "mytest.pem"

在tweaks.yml我有以下内容:

- name: "Extra Nginx hosts for the Drupal sites"
  template:
    src: ../templates/nginx-vhost.conf.j2
    dest: "{{ nginx_vhost_path }}/{{ item.server_name.split(' ')[0] }}.conf"
    force: yes
    owner: root
    group: root
    mode: 0644
  with_items: nginx_hosts_custom
  notify: restart nginx
  when: drupalvm_webserver == 'nginx'

这曾经完美地工作,但现在我在运行配置时收到以下错误:

fatal: [owenvm]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'unicode object' has no attribute 'server_name'\n\nThe error appears to have been in /tasks/tweaks.yml': line 20, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: \"Extra Nginx hosts for the Drupal sites\"\n  ^ here\n"}

所以看起来变量server_name没有被拾取 - 任何人都可以建议修复吗?

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

2 个回答

  • Voted
  1. Best Answer
    Konstantin Suvorov
    2016-11-15T11:31:14+08:002016-11-15T11:31:14+08:00

    with_items, like中的裸变量with_items: nginx_hosts_custom自 Ansible 2.0 起已弃用,自 2.2 起完全不受支持。

    你应该使用with_items: "{{ nginx_hosts_custom }}"

    • 3
  2. SYN
    2016-11-15T11:28:03+08:002016-11-15T11:28:03+08:00

    在tweaks.yml

    - name: "Extra Nginx hosts for the Drupal sites"
      template:
        src: ../templates/nginx-vhost.conf.j2
        dest: "{{ nginx_vhost_path }}/{{ item['server_name'].split(' ') | first }}.conf"
        force: yes
        owner: root
        group: root
        mode: 0644
      with_items: nginx_hosts_custom
      notify: restart nginx
      when: drupalvm_webserver == 'nginx'
    

    此外,模板通常知道要查看../templates(至少在使用角色时)。您确定需要提供相对路径吗?坚持您的模板文件名可能就足够了。

    根据 Ansible 版本,with_items可能需要设置为'{{ nginx_host_custom }}'. 尽管根据您的错误消息,这不是我们的情况。

    我还建议确保nginx_hosts_custom在您的when条款中定义,例如:

    when: drupal_webserver == 'nginx' and nginx_hosts_custom is defined and nginx_hosts_custom != False
    
    • 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