-
-
Notifications
You must be signed in to change notification settings - Fork 606
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Copy files/includes and populate templates/includes and add to includes.d. #242
Copy files/includes and populate templates/includes and add to includes.d. #242
Conversation
You are right, there is no way to currently do that. For the files, you could go with the synchronize module, but that wouldn't work for the templates. You cloud try with - template: src="{{ item }}" dest="/etc/nginx/includes.d/{{ item }}"
with_pipe: find ../templates/includes/ -type f If that works, I would find that more easy to understand than the regex, but that may be just a personal preference. 😉 |
@chriszarate I'll check if I can think of a better method of doing this without the |
Thanks. I checked a few angles, and the problem is that anything that globs is going to pass the full local path. :( |
@chriszarate I got nothing. |
@chriszarate I get failures on this because the subdirectories of
|
Ok, let me take another pass at this. I also want to improve the regex to leave less room for error, |
Here's an approach inspired by @louim's - name: Copy templates/includes to includes.d
template: src="{{ item }}" dest="/etc/nginx/{{ item[:-3] }}"
with_lines: "cd ../templates && find includes.d -type f 2>/dev/null || :" Notes:
Potential problems:
|
@chriszarate if you're interested, I'd love your feedback on this revised approach. By the way, thanks for mentioning Features
# roles/wordpress-setup/tasks/nginx.yml
⋮
- name: Template files out to includes.d
template: src="includes.d/{{ item }}" dest="/etc/nginx/includes.d/{{ item[:-3] }}"
with_lines: "cd ../templates/includes.d && find {{ template_paths }} -type f 2>/dev/null || :"
register: includes
notify: reload nginx
- name: Retrieve list of existing files in includes.d
shell: "find {{ include_paths }} -type f 2>/dev/null || :"
args:
chdir: /etc/nginx/includes.d
register: includes_existing
changed_when: false
- name: Remove undesired files from includes.d
file: path="/etc/nginx/includes.d/{{ item }}" state=absent
with_items: includes_existing.stdout_lines | difference(includes_to_keep)
notify: reload nginx
⋮ Several supporting variables: # roles/wordpress-setup/defaults/main.yml
⋮
template_paths: "{% for site in wordpress_sites.keys() %}{{ site }}/*.conf.j2 {% endfor %}"
include_paths: "{{ template_paths | regex_replace('(.j2)', '') }}"
includes_to_keep: '{% if includes.results is defined -%}
[{% for item in includes.results %}
"{{ item.item[:-3] }}",
{% endfor %}]
{%- else -%}
[]
{%- endif %}' The last two tasks could have been combined in one. I chose against it because it might look more complicated and not print to the output exactly which files it removed. Roughly, the task would have been - name: Remove undesired files from includes.d
shell: for file in */*; do case "$file" in {{ includes_to_keep }}) ;; *) rm "$file" && echo "$file was removed";; esac; done
args:
chdir: /etc/nginx/includes.d
register: includes_removed
changed_when: includes_removed.stdout != '' It would need a variable like this in the defaults file: includes_to_keep: "{% for item in includes.results %}'{{ item.item[:-3] }}' | {% endfor %}''" |
I'll take a look at this today! |
This is great! Thanks right back at you for mentioning Works great. I was inspired to simplify the supporting variables, and I was eventually able to eliminate them altogether. I also removed the regexes and restricted the templates to a folder depth of 1 (e.g., I'll push that here in a minute, but feel free to open your own pull request if you like your approach better. I was just tinkering. |
e9a7dac
to
819e675
Compare
@chriszarate Smart! Lots of superior solutions in here, especially eliminating the supporting variables. I finally understand the "Remove unmanaged files"
|
Put files in /etc/nginx/includes.d, mirroring directory structure recursively.
819e675
to
309a5d8
Compare
Sorry for the long delay in circling back to this. I wasn't able to do testing until recently. Thanks for following up and catching those problems. I removed the I added Thanks again! I'd welcome more feedback but it feels ready to merge from my perspective. |
Nice! I agree: Looks ready to merge. I just ran it again against the tests I could think of.
@chriszarate Thanks for this!
|
…work Copy files/includes and populate templates/includes and add to includes.d.
Yes! Thanks! Took a stab at a Wiki page at Nginx-includes. |
@chriszarate That wiki reads so well, with great info. Thank you! |
@chriszarate 🍉 🍉 🍉 I second @fullyint on that wiki being well written. Great job. |
Picking up from #241, here's something that implements @louim's option 1, with both
files
andtemplates
as @swalkinshaw suggested.Be warned: This isn't pretty, and it relies on the
regex_replace
filter. Pretty sure this is safe for Ansible >= 1.8, though.Is there a better way to do this? I couldn't figure out how to nest a
with_fileglob
inside awith_dict
(or awith_items
/wordpress_sites.keys()
), so someone could break the task if one of their folder names doesn't match one of their site keys.