Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Edit files using lineinfile and blockinfile or just copy the entire file using template?

I am starting to use Ansible in order to write a playbook that will deploy a staging environment for our application. I'm trying to understand if the best practice for applying changes to files are to change them locally on the control machine and then propagate them into the remote server, or whether the files should be manipulated inside the playbook.

Manipulating the files through the playbook seems better in terms of readability and documentation, as well as keeping the entire configuration process to one tool for the entire configuration process. On the other hand, changing the files on the local server is easier and faster.

What is the best way to approach these problems?

Thanks, Yaron.

like image 455
Yaron Idan Avatar asked Feb 07 '23 20:02

Yaron Idan


2 Answers

Welcome to Ansible!

I think you should have a look at Templates. Never change Files locally and deploy them. Your deployment should not change any file locally!

If you have a config file, write a template for this file and render some variables inside to change the config for the deployed application. The template module will render the file directly to your server.

like image 195
flxPeters Avatar answered May 16 '23 08:05

flxPeters


Use lineinfile and blockinfile (ansible > 2.0). It is cleaner, portable and can be run from any control machine. But there are exception when the block is huge.

like image 23
helloV Avatar answered May 16 '23 06:05

helloV