我有一个运行自定义任务来设置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"}
所以看来variablesserver_name没有被拿起 – 任何人都可以提出一个修复?
在with_itemsvariables,像with_items: nginx_hosts_custom ,自从Ansible 2.0被弃用,并且从2.2开始完全不被支持。
你应该使用with_items: "{{ nginx_hosts_custom }}"
在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模板(至less在使用angular色时)。 你确定你需要给一个相对path吗? 坚持你的模板文件名可能就足够了。
根据Ansible版本,可能需要将with_items设置为'{{ nginx_host_custom }}' 。 虽然根据你的错误信息,这不是我们的情况。
我也build议确保nginx_hosts_custom是在你的when子句中定义的,比如:
when: drupal_webserver == 'nginx' and nginx_hosts_custom is defined and nginx_hosts_custom != False