1

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?

williamsowen
  • 1,167
  • 3
  • 16
  • 25

2 Answers2

3

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

Konstantin Suvorov
  • 3,996
  • 1
  • 12
  • 13
1

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

Also, template usually knows to look in ../templates (at least when using roles). Are you sure you need to give a relative path? Sticking to your template filename may be enough.

Depending on Ansible version, with_items may need to be set to '{{ nginx_host_custom }}'. Although according to your error message, this is not our case.

I'ld also recommend making sure nginx_hosts_custom is defined in your when clause, such as:

when: drupal_webserver == 'nginx' and nginx_hosts_custom is defined and nginx_hosts_custom != False
SYN
  • 1,751
  • 9
  • 14