大网数据广告活动
服务器运维问答

Ansible list variable undefined

· 52CE 我爱测

I have a playbook which runs a custom task to set up an nginx vhost: tasks: - include: tasks/tweaks.yml In this playbook, the following var_files are used: vars_files: - ../config.yml In config.yml I have the following list which…

问题原文

原帖链接 · williamsowen · CC BY-SA 3.0

I have a playbook which runs a custom task to set up an nginx vhost:

  tasks:
    - include: tasks/tweaks.yml

In this playbook, the following var_files are used:

  vars_files:
    - ../config.yml

In config.yml I have the following list which holds the vhost info:

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"

Within tweaks.yml I have the following:

- 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'

This used to work perfectly, but now I receive the following error when running the provision:

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"}

So it appears the variable server_name is not being picked up - can anyone suggest a fix?

已采纳解答

原帖链接 · Konstantin Suvorov · CC BY-SA 3.0

Bare variables in with_items, like with_items: nginx_hosts_custom are deprecated since Ansible 2.0 and are totally unsupported since 2.2.

You should use with_items: "{{ nginx_hosts_custom }}"

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-11-14)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。