Hello!
I have the following vars/main.yml file I am sourcing for an Ansible task. Ultimately, the purpose being that if there is a list of sites needed, they can be put in the var and looped over.
Code:
vars/main.yml
new_site:
- sitename
The playbook task looks like so
Code:
playbook.yml
tasks:
- name: Include local vars
include_vars:
dir: vars
- name: Ensure proper ownership of the images and cache dirs
ansible.builtin.file:
path: /var/www/html/{{ item.site }}/{{ item.dir }}
owner: apache
group: apache
mode: 0755
with_items:
- { site: "{{ new_site }}", dir: 'cache' }
- { site: "{{ new_site }}", dir: 'images' }
when: "'all_sites' in group_names"
This is pulling in the expected 'sitename' but there is a surrounding
[' '] added to it and of course, it is failing the task since that is not the name of the dirs.
Code:
TASK [Ensure proper ownership of the images and cache dirs] *************************************************
failed: [host1] (item={'site': ['sitename'], 'dir': 'cache'}) => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "ansible_loop_var": "item", "changed": false, "item": {"dir": "cache", "site": ["sitename"]}, "msg": "file (/var/www/html/['sitename']/cache) is absent, cannot continue", "path": "/var/www/html/['sitename']/cache", "state": "absent"}
failed: [host1] (item={'site': ['sitename'], 'dir': 'images'}) => {"ansible_loop_var": "item", "changed": false, "item": {"dir": "images", "site": ["sitename"]}, "msg": "file (/var/www/html/['sitename']/images) is absent, cannot continue", "path": "/var/www/html/['sitename']/images", "state": "absent"}
Admittedly, I'm not very familiar with/understand what is presumably a more advanced method of trying to reference a main variable list within 'with_items' the way I have it now.