0

I have created a variable to disable sites in nginx within my main set of tasks. Since this is a one time task, meaning once domain1.com is disabled I can comment the entire line out. When I do, I receive an error

" {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'None' has no attribute 'domain'".

What can I do to modify my task to only run when there are domains listed within the variable?

nginx_sites_disabled:
#- domain: "domain1.com"

- name: Disable sites
    file:
    path: /etc/nginx/sites-enabled/{{ item.domain }}
    state: absent
  with_items: "{{nginx_sites_disabled}}"
  notify:
  - Reload nginx
GreenLion
  • 5
  • 3

2 Answers2

3

Apply default filter within your task:

- name: Disable sites
    file:
    path: /etc/nginx/sites-enabled/{{ item.domain }}
    state: absent
  with_items: "{{ nginx_sites_disabled | default([]) }}"
  notify:
    - Reload nginx

Related answer: Ansible: apply when to complete loop

Konstantin Suvorov
  • 65,183
  • 9
  • 162
  • 193
1

You don't need to comment out the lines when the work is done.

Like most of Ansible modules, file module is idempotent : if the desired state is absent and the file isn't there, it won't do anything.

Just leave nginx_sites_disabled list unchanged.

By the way, if you still need nginx_sites_disabled to be an empty list, you need to write this :

---
nginx_sites_disabled: []

otherwise, nginx_sites_disabled will be equal to None. That's why you have this error.

Eric Citaire
  • 4,355
  • 1
  • 29
  • 49