I need to create or overwrite files on remote hosts. The modules lineinfile
or blockinfile
are useful when updating files, but not to create ones from scratch or completely overwrite existing ones.
The obvious solution is to use copy
but I would like to have as much as possible a standalone playbook, without files on the side. Is it possible to include in a playbook the content of the file to create?
Maybe something along the lines of having a variable with the content of the file which can be used as the src=
parameter for copy
(I tried this but it does not work as src
expects a local file)
You can use lookups in Ansible in order to get the contents of a file, e.g. Caveat: This lookup will work with local files, not remote files. Note that lookup runs locally, while the cat command in @TesterJeff's example is running on the remote machine.
A playbook can have 'n' number of plays in a list. In the following example, the first play has set of tasks for ubuntu servers and the second for centos servers.
Creating an empty file in AnsibleYou can Create an empty file using the file module. You just need to set two parmaters. Path – This is the location where you want the file to be created. It can be either a relative path or an absolute path.
Roles provide a framework for fully independent, or interdependent collections of variables, tasks, files, templates, and modules. In Ansible, the role is the primary mechanism for breaking a playbook into multiple files. This simplifies writing complex playbooks, and it makes them easier to reuse.
You might make a handlers.yml that looks like: And in your main playbook file, just include it like so, at the bottom of a play: You can mix in includes along with your regular non-included tasks and handlers. Includes can also be used to import one playbook file into another.
While it is possible to write a playbook in one very large file (and you might start out learning playbooks this way), eventually you’ll want to reuse files and start to organize things. At a basic level, including task files allows you to break up bits of configuration policy into smaller files. Task includes pull in tasks from other files.
Playbooks can also include plays from other playbook files. When that is done, the plays will be inserted into the playbook to form a longer list of plays. When you start to think about it – tasks, handlers, variables, and so on – begin to form larger concepts.
You can use include files to do this. Use of included task lists is a great way to define a role that system is going to fulfill. Remember, the goal of a play in a playbook is to map a group of systems into multiple roles. Let’s see what this looks like… A task include file simply contains a flat list of tasks, like so:
Copy with content:
tasks:
- copy:
content: |
This is some
not too complex
cotent for a file
dest: content.txt
But as per Ansible doc:
This is for simple values, for anything complex or with formatting please switch to the template module.
The template - module is a good way to achive your goal.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With