ansible-lineinfile module

lineinfile: Modify the contents of the file, add a line in front of a line, add a line at the back of a row, delete a row, add a line at the end, replace or add a row

path parameters: parameter must specify the file to be operated.

line parameters: Use this parameter to specify the text content.

regexp parameters: use regular expressions to match the corresponding row, when replacing text, if there are multiple lines of text can be matched, only the last surface is matched to that line of text will be replaced when you delete text, if there is Multiple lines of text can be matched, so these lines will be deleted.

state parameters: When you want to delete the corresponding text, you need to set the value of state parameter is absent, absent for the absence of meaning, it means to delete, the default state is present.

backrefs parameters: default, when according to a regular replacement text, even though the positive regexp parameters there packet, nor the packet regularization is referenced in line parameters, except the value backrefs parameter to yes. backrefs = yes reference to a rear opening, so that, line parameter after a packet can be performed to regexp parameters quoted, say not readily apparent, it can be understood with reference to an example of the latter command. backrefs = yes In addition to the reference function, the additional effect can be opened, by default, when a regular expression substituted for the corresponding row, if there is no match to any regular row, then the line corresponding to the content will be inserted into the text the end, however, if you use backrefs = yes, the situation is different, when to use regular expressions to replace the corresponding row, and set up backrefs = yes, then when there is no positive match to any of the rows, will not any operation, equivalent to keep the original file unchanged.

insertafter parameters: After the aid insertafter parameters can insert text into a "designated line", the value of insertafter parameter can be set to EOF or regular expressions, EOF is End Of File meaning, it signifies the end inserted into the document, the default insertafter case value of the EOF, then if the value insertafter is a regular expression, shows a text into a matched regular rows, if n is no match for any line, the inserted end of the file, when using backrefs parameter, It will be ignored.

insertbefore parameters: With insertbefore parameter text can be inserted before "the specified line", the value of insertbefore parameter can be set to BOF or regular expressions, BOF to Begin Of File meaning, represents the beginning inserted into the document, if the insertbefore of value is set to a regular expression that will insert text before the match to the regular line to, if there is no match to any regular line, insert the end of the file, when using backrefs parameters, this parameter is ignored.

backup parameters: whether to back up files before you modify the file.

create parameters: When the file you want to operate does not exist, whether to create a corresponding file.

 

1, modify the contents of the file

ansible 192.168.30.21 -m lineinfile -a "dest=/data/hosts regexp="^192.168.30.21" line="123456"" -b

 

2, insert a row in front of the row designated

ansible 192.168.30.21 -m lineinfile -a "dest=/data/hosts insertbefore="^192.168.30.22" line="7890"" -b

 

3, increase muscle at the end of a line

ansible 192.168.30.21 -m lineinfile -a "dest=/data/hosts line='192.168.30.23   web03'" -b

 

4, delete a row

ansible 192.168.30.21 -m lineinfile -a "dest=/data/hosts regexp='192.168.30.23(.*)' state=absent" -b

 

5, replace a row, and if not, will be added to the end of the text

ansible 192.168.30.21 -m lineinfile -a "dest=/data/hosts regexp='192.168.30.24(.*)' line='192.168.30.24   web05' state=present" -b

 

6, playbook + with_items batch modify file content

script:

[dwchensenwen@vms20 script]$ cat lineinfile.yaml 
---
- hosts: test
  remote_user: dwchensenwen
  become: yes
  become_method: sudo
  tasks:
  - name: Configuer hosts
    lineinfile: dest=/data/hosts regexp={{ item.regexp }} line={{ item.line }}
    with_items:
      - { regexp: "^123456", line: "192.168.30.21   web01" }
      - { regexp: "^7890", line: "192.168.30.22   web02" }

运行结果:

 

Guess you like

Origin www.cnblogs.com/mustark/p/11102923.html