Active8 months ago
Is there an easy way to deploy a folder full of template .j2 folder to a linux box, using the same name as the template, but without the .j2 extension, rather than using the template module for each file?
Right now i have a long list of:
dawudMay 04, 2018 Automate Windows updates with Ansible ^ Now that we have gone over the basics of setting up an Ansible server for Windows communication and seen how to ensure successful WinRM communication, let's look at how we can use this connection to automate Windows updates with Ansible. Aug 26, 2014 An Ansible Tutorial. Ansible is one of the simplest server provisioning and configuration management tools. This is a guide to getting started with Ansible. Configuration Management. Knowing this, you may choose to set your template of the nginx.conf file like so.
13.7k33 gold badges3333 silver badges5656 bronze badges
TrolololololTrololololol
6 Answers
You could use
with_fileglob
to get the list of files from your template directory and use filters to strip the j2 extension like this.![Ansible template file windows Ansible template file windows](/uploads/1/2/4/9/124906733/853764315.png)
RussellRussell
Michael DeHaan(creator of Ansible) made a post on CoderWall that talks about very similar issue. You can adjust and expand it according to your needs(such as permissions and ownership).Relevant part of the post is here:
This can be simplified by using '
with_items
' and a single notify
statement. If any of the tasks change, the service will be notified in exactly the same way that it needs to restart at the end of the playbook run.Note that since we have tasks that take more than one unique argument, we don't just say '
item
' in the 'template:
' line, but use with_items
with a hash (dictionary) variable. You can also keep it a little shorter by using lists, if you like. This is a stylistic preference:![Ansible template if Ansible template if](/uploads/1/2/4/9/124906733/323210190.png)
Of course we could also define the list you were walking over in another file, like a '
MxxMxxgroupvars/webservers
' file to define all the variables needed for the webservers
group, or a YAML file loaded from the 'varsfiles
' directive inside the playbook. Look how this can clean up if we do.1,78211 gold badge1919 silver badges4040 bronze badges
The answer by Russel does work but it needs improvement
Firs of all the $ needs to go as it was wrong regex in the regex_replace . Secondly all the files should be in the files directory rather than templates directory
HJ_VORTEXHJ_VORTEX
I wrote a filetree lookup plugin that can help with actions on file trees.
- https://github.com/ansible/ansible/pull/14332 (Ansible v2.x)
- https://github.com/ansible/ansible/pull/14628 (Ansible v1.9.x)
You can recurse over files in a file tree and based on file properties perform actions (e.g. template or copy). Since the relative path is returned, you can recreate the filetree on the target system(s) with ease.
It makes for more readable playbooks.
Dag WieersDag Wieers
The below command worked for me to do a recursive lookup for j2 files in templates and move it to the destination. Hope it helps someone looking for recursive copy of templates to destination.
RobinRobin
Ansible Windows Template File Download
There is a possibility to grab the list of actual files from the directory automatically and iterate them afterwards.
Roman DolejsiRoman Dolejsi