11
votes

I am putting together an Ansible Playbook designed to build webservers. However I am stuck when trying to use with_fileglob because Ansible keeps reporting that it's skipping the copy of nginx vhost files.

My script looks like this:

 - name: Nginx | Copy vhost files
   copy: src={{ item }} dest=/etc/nginx/sites-available owner=root group=root  mode=600
   with_fileglob:
     - "{{ templates_dir }}/nginx/sites-available/*"  
   notify
   - nginx-restart:

{{ templates }} has been defined elsewhere as roles/common/templates. In this directory I have a file called webserver1 that I'm hoping Ansible will copy into /etc/nginx/sites-available/

I have found other people discussing this issue but no responses have helped me solve this problem. Why would Ansible be skipping files?

Edit: I should point out that I want to use with_fileglob (rather than straight copy) as I want to iterate over other virtual hosts in the future.

1
OK, it seems that I can only use absolute URLs here. Maybe it's because I am using through Vagrant using vagrant provision but this is not ideal. I thought I could use variables that include the absolute URLs but it seems it has to be static in order to work.Dubby
OK, I found the problem. It seems I grabbed used some old examples when creating variables. I was using: templates_dir: src="{{ base_dir }}/roles/common/templates" when I should have been using ` templates_dir: "{{ base_dir }}/roles/common/templates"`Dubby

1 Answers

15
votes

Look at http://docs.ansible.com/playbooks_loops.html#looping-over-fileglobs, Note 1:

When using a relative path with with_fileglob in a role, Ansible resolves the path relative to the roles//files directory.

So to access a file in the templates directory, you can start the relative path with ../templates