[PYTHON] [Ansible] Example of playbook that adds a character string to the first line of the file

I'm sure it's not limited to ansible, but I think there are times when you want to add a character string to the first line of a file such as a configuration file.

(It changes depending on the behavior in the order of setting in the configuration file)

Until now, I didn't really care about CentOS, but apparently it didn't work on OSX, so a memo at that time

And I noticed that @uraura's method was the simplest ...

environment

Playbook to add a string to the first line of the file

~/.If the string you want to insert does not exist in zshrc.An example of adding a string to the first line of zshrc.[lineinfile](http://docs.ansible.com/ansible/lineinfile_module.html)Use modules


 (And thanks to [@uraura](http://qiita.com/uraura) for teaching me)



#### **`/path/to/ansible-role/tasks/main.yml`**
```yaml

- name: Add .zshrc prezto.init.sh
  lineinfile:
    dest={{ ansible_env.HOME }}/.zshrc
    line="source ~/.zsh/prezto.init.sh"
    insertbefore=BOF state=present regexp="prezto\.init\.sh"
  notify:
    - execute source .zshrc

Finally

I was addicted to it for about an hour ...

Recommended Posts

[Ansible] Example of playbook that adds a character string to the first line of the file
# Function that returns the character code of a string
[Python] Programming to find the number of a in a character string that repeats a specified number of times.
Outputs a line containing the specified character string from a text file
How to extract the desired character string from a line 4 commands
How to put a line number at the beginning of a CSV file
<Python> A quiz to batch convert file names separated by a specific character string as part of the file name
Get the variable name of the variable as a character string.
How to connect the contents of a list into a string
How to quickly count the frequency of appearance of characters from a character string in Python?
When a character string of a certain series is in the Key of the dictionary, the character string is converted to the Value of the dictionary.
pandas Fetch the name of a column that contains a specific character
[Introduction to Python] How to split a character string with the split function
A story that struggled to handle the Python package of PocketSphinx
Conditional branch due to the existence of a shell script file
How to output the output result of the Linux man command to a file
A script that returns 0, 1 attached to the first Python prime number
I made a library that adds docstring to a Python stub file.
[python] A note that started to understand the behavior of matplotlib.pyplot
I want to specify a file that is not a character string for logrotate, but is it impossible?
[Python] A program that rotates the contents of the list to the left
"A book to train programming skills to fight in the world" Python code answer example --1.1 Duplicate character string
Heroku deployment of the first Django app that beginners are addicted to
[Python] A program that calculates the number of socks to be paired
How to create a wrapper that preserves the signature of the function to wrap
[Introduction to Python] How to write a character string with the format function
[Introduction to Python] Thorough explanation of the character string type used in Python!
I made a program to check the size of a file in Python
[Python] How to invert a character string
[Pandas] Expand the character string to DataFrame
[Python] Get the character code of the file
[PowerShell] Get the reading of the character string
The story of IPv6 address that I want to keep at a minimum
Python version (PHP to Python) that deletes the subsequent character string from the specified character string (extension)
Save an array of numpy to a wav file using the wave module
I want to add silence to the beginning of a wav file for 1 second
The story of making a Line Bot that tells us the schedule of competitive programming
Ansible self-made module creation-Part 3: Life that wants to judge the necessity of change-
How to make a Raspberry Pi that speaks the tweets of the specified user
How to calculate the volatility of a brand
A simple example of how to use ArgumentParser
[Ansible installation procedure] From installation to execution of playbook
Save the object to a file with pickle
I tried to generate a random character string
Convert the character code of the file with Python3
[VBA] Function to judge whether the character string of UTF-8 file system is 255 or less and function to count UTF-8 character string in bytes
How to display the modification date of a file in C language up to nanoseconds
The story of Linux that I want to teach myself half a year ago
A memo that implements the job of loading a GCS file into BigQuery in Python
Software that saves the set character string to the clipboard when you click the [Python] button
[Python] Leave only the elements that start with a specific character string in the array
Convert a path string that uses a symbolic link in the middle to an absolute path
A script that can perform stress tests according to the number of CPU cores
[Ruby] How to replace only a part of the string matched by the regular expression?
When reading a csv file with read_csv of pandas, the first column becomes index
The world's most easy-to-understand explanation of how to make a LINE BOT (1) [Account preparation]
How to find the first element that matches your criteria in a Python list
Checks if there is a specific character string for all files under the directory that is Python and outputs the target line
A story that reduces the effort of operation / maintenance
Parse a JSON string written to a file in Python
[Python] A program that counts the number of valleys