Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to include file content in a playbook?

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)

like image 231
WoJ Avatar asked Jul 20 '16 11:07

WoJ


People also ask

How do I read Ansible file content?

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.

Can there be n number of plays inside a playbook?

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.

Can an empty file be created in Ansible if yes how?

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.

What is the difference between Ansible playbook and roles?

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.

How do I include includes in a playbook?

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.

Can I write a playbook in a task file?

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.

What is a playbook and how does it work?

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.

Why use include files in playlists?

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:


2 Answers

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.

like image 87
Konstantin Suvorov Avatar answered Sep 28 '22 01:09

Konstantin Suvorov


The template - module is a good way to achive your goal.

like image 26
Capri90 Avatar answered Sep 28 '22 03:09

Capri90