The blockinfile module inserts "a piece of text" into the specified file, which is marked, that is, marks the text, so that the text can be found through "mark" in later operations, and then modify or delete it.
1, Common parameters
path:required, specifies the file to operate on.
Block: specifies a piece of text to be operated on. This parameter has a name of "content". Using content or block is the same.
Marker: insert a piece of text in the specified file, and ansible will automatically add two marks for the text, one is the start mark and the other is the end mark. By default, the start mark is "BEGIN ANSIBLE MANAGED BLOCK" and the end mark is "END ANSIBLE MANAGED BLOCK", and the marker parameter is used to customize "mark". For example, marker = {mark} test. After this setting, the start tag becomes a BEGIN test and the end tag becomes an END test.
State: the state parameter has two optional values, present and absent, which insert and delete the corresponding marked paragraphs.
insertafter: when inserting a piece of text, the text will be inserted at the end of the file by default. If you want to insert the text after a line, you can use this parameter to specify the corresponding line, or you can use a regular expression (python regular) to insert the text after the line that matches the regular expression. If more than one line of text can match the corresponding regular expression, the last one that satisfies the regular behavior shall prevail. The value of this parameter can also be set to EOF, indicating that the text is inserted at the end of the document.
Insert before: when inserting a piece of text, the text will be inserted at the end of the file by default. If you want to insert the text in front of a line, you can use this parameter to specify the corresponding line, or you can use a regular expression (python regular) to insert the text in front of the line that matches the regular expression. If more than one line of text can match the corresponding regular expression, the last regular behavior will prevail. The value of this parameter can also be set to BOF, indicating that the text is inserted at the beginning of the document.
backup: whether to back up the file before modifying it.
Create: whether to create the corresponding file when the file to be operated does not exist.
Two, example
Example 1
[root@ansible-manager ~]# ansible host1 -m blockinfile -a 'path=/testdir/rc.local block="systemctl start mariadb\nsystemctl start httpd"' [root@ansible-manager ~]#cat/testdir/rc.local # BEGIN ANSIBLE MANAGED BLOCK systemctl start mariadb systemctl start httpd # END ANSIBLE MANAGED BLOCK
Example 2
[root@ansible-manager ~]# ansible host1 -m blockinfile -a 'path=/testdir/rc.local block="systemctl start mariadb\nsystemctl start httpd" marker="#{mark} serivce to start"' [root@ansible-manager ~]#cat/testdir/rc.local #BEGIN serivce to start systemctl start mariadb systemctl start httpd #END serivce to start
Example 3
On the basis of the above command, execute the following command:
[root@ansible-manager ~]# ansible host1 -m blockinfile -a 'path=/testdir/rc.local block="systemctl start mariadb" marker="#{mark} serivce to start"'
Because when executing this command, the text block corresponding to the "{mark} serve to start" tag already exists in the file, and at the same time, the content corresponding to the block parameter is different from the content of the previous text block, so in this case, the content in the corresponding text block will be updated without inserting a new text block again, which is equivalent to updating the content in the original text block Content.
[root@ansible-manager ~]#cat/testdir/rc.local #BEGIN serivce to start systemctl start mariadb #END serivce to start
Example 4
On the basis of the above command, execute the following command:
[root@ansible-manager ~]# ansible host1 -m blockinfile -a 'path=/testdir/rc.local block="" marker="#{mark} serivce to start"'
When executing this command, the text block corresponding to the "{mark} service to start" tag already exists in the file. At the same time, the content corresponding to the block parameter is empty. At this time, the blockinfile module will delete the text block corresponding to the tag, so the return information is "Block removed".
Example 5
[root@ansible-manager ~]# ansible host1 -m blockinfile -a 'path=/testdir/rc.local marker="#{mark} serivce to start" state=absent'
Use to set the value of state to absent to delete the text block of the corresponding tag.
By default, the text block is inserted at the end of the file. We can also insert the text block at the specified position, for example, at the beginning of the file, or match the corresponding row according to the regular expression, and then insert the text block at the front or back of the matched row.
Example 6
If you want to insert a text block at the beginning of a document, you can use the insertbefore parameter to set its value to BOF, which means Begin Of File:
[root@ansible-manager ~]# ansible host1 -m blockinfile -a 'path=/testdir/rc.local block="####blockinfile test####" marker="#{mark} test" insertbefore=BOF'
Example 7
If you use the following command to insert a text block to the end of the document, as in the default operation, set the insertafter parameter to EOF to End Of File:
[root@ansible-manager ~]# ansible host1 -m blockinfile -a 'path=/testdir/rc.local block="####blockinfile test####" marker="#{mark} test eof" insertafter=EOF'
Example 8
Use the following command to match lines with regular expressions, and insert the text block after "lines starting with" ×! / bin/bash ":
[root@ansible-manager ~]# ansible host1 -m blockinfile -a 'path=/testdir/rc.local block="####blockinfile test####" marker="#{mark} test reg" insertafter="^#!/bin/bash"'
Example 9
Using the backup parameter, you can back up the file before modifying the file. The backed up file will be time stamped based on the original file name:
[root@ansible-manager ~]# ansible host1 -m blockinfile -a 'path=/testdir/rc.local marker="#{mark} test" state=absent backup=yes'
Example 10
Using the create parameter, if the specified file does not exist, create it:
[root@ansible-manager ~]# ansible host1 -m blockinfile -a 'path=/testdir/testfile3 block="test" marker="#{mark} test" create=yes'
--Blueicex 2020/2/2 17:55 blueice1980@126.com