Debian Codegraph @

ansible_collections/community/windows/plugins/modules/win_lineinfile.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 DOCUMENTATION = r'''
7 ---
8 module: win_lineinfile
9 short_description: Ensure a particular line is in a file, or replace an existing line using a back-referenced regular expression
10 description:
11 - This module will search a file for a line, and ensure that it is present or absent.
12 - This is primarily useful when you want to change a single line in a file only.
13 options:
14 path:
15 description:
16 - The path of the file to modify.
17 - Note that the Windows path delimiter C(\) must be escaped as C(\\) when the line is double quoted.
18 type: path
19 required: yes
20 aliases: [ dest, destfile, name ]
21 backup:
22 description:
23 - Determine whether a backup should be created.
24 - When set to C(yes), create a backup file including the timestamp information
25 so you can get the original file back if you somehow clobbered it incorrectly.
26 type: bool
27 default: no
28 regex:
29 description:
30 - The regular expression to look for in every line of the file. For C(state=present), the pattern to replace if found; only the last line found
31 will be replaced. For C(state=absent), the pattern of the line to remove. Uses .NET compatible regular expressions;
32 see U(https://msdn.microsoft.com/en-us/library/hs600312%28v=vs.110%29.aspx).
33 aliases: [ "regexp" ]
34 state:
35 description:
36 - Whether the line should be there or not.
37 type: str
38 choices: [ absent, present ]
39 default: present
40 line:
41 description:
42 - Required for C(state=present). The line to insert/replace into the file. If C(backrefs) is set, may contain backreferences that will get
43 expanded with the C(regexp) capture groups if the regexp matches.
44 - Be aware that the line is processed first on the controller and thus is dependent on yaml quoting rules. Any double quoted line
45 will have control characters, such as '\r\n', expanded. To print such characters literally, use single or no quotes.
46 type: str
47 backrefs:
48 description:
49 - Used with C(state=present). If set, line can contain backreferences (both positional and named) that will get populated if the C(regexp)
50 matches. This flag changes the operation of the module slightly; C(insertbefore) and C(insertafter) will be ignored, and if the C(regexp)
51 doesn't match anywhere in the file, the file will be left unchanged.
52 - If the C(regexp) does match, the last matching line will be replaced by the expanded line parameter.
53 type: bool
54 default: no
55 insertafter:
56 description:
57 - Used with C(state=present). If specified, the line will be inserted after the last match of specified regular expression. A special value is
58 available; C(EOF) for inserting the line at the end of the file.
59 - If specified regular expression has no matches, EOF will be used instead. May not be used with C(backrefs).
60 type: str
61 choices: [ EOF, '*regex*' ]
62 default: EOF
63 insertbefore:
64 description:
65 - Used with C(state=present). If specified, the line will be inserted before the last match of specified regular expression. A value is available;
66 C(BOF) for inserting the line at the beginning of the file.
67 - If specified regular expression has no matches, the line will be inserted at the end of the file. May not be used with C(backrefs).
68 type: str
69 choices: [ BOF, '*regex*' ]
70 create:
71 description:
72 - Used with C(state=present). If specified, the file will be created if it does not already exist. By default it will fail if the file is missing.
73 type: bool
74 default: no
75 validate:
76 description:
77 - Validation to run before copying into place. Use %s in the command to indicate the current file to validate.
78 - The command is passed securely so shell features like expansion and pipes won't work.
79 type: str
80 encoding:
81 description:
82 - Specifies the encoding of the source text file to operate on (and thus what the output encoding will be). The default of C(auto) will cause
83 the module to auto-detect the encoding of the source file and ensure that the modified file is written with the same encoding.
84 - An explicit encoding can be passed as a string that is a valid value to pass to the .NET framework System.Text.Encoding.GetEncoding() method -
85 see U(https://msdn.microsoft.com/en-us/library/system.text.encoding%28v=vs.110%29.aspx).
86 - This is mostly useful with C(create=yes) if you want to create a new file with a specific encoding. If C(create=yes) is specified without a
87 specific encoding, the default encoding (UTF-8, no BOM) will be used.
88 type: str
89 default: auto
90 newline:
91 description:
92 - Specifies the line separator style to use for the modified file. This defaults to the windows line separator (C(\r\n)). Note that the indicated
93 line separator will be used for file output regardless of the original line separator that appears in the input file.
94 type: str
95 choices: [ unix, windows ]
96 default: windows
97 seealso:
98 - module: ansible.builtin.assemble
99 - module: ansible.builtin.lineinfile
100 author:
101 - Brian Lloyd (@brianlloyd)
102 '''
103
104 EXAMPLES = r'''
105 - name: Insert path without converting \r\n
106 community.windows.win_lineinfile:
107 path: c:\file.txt
108 line: c:\return\new
109
110 - community.windows.win_lineinfile:
111 path: C:\Temp\example.conf
112 regex: '^name='
113 line: 'name=JohnDoe'
114
115 - community.windows.win_lineinfile:
116 path: C:\Temp\example.conf
117 regex: '^name='
118 state: absent
119
120 - community.windows.win_lineinfile:
121 path: C:\Temp\example.conf
122 regex: '^127\.0\.0\.1'
123 line: '127.0.0.1 localhost'
124
125 - community.windows.win_lineinfile:
126 path: C:\Temp\httpd.conf
127 regex: '^Listen '
128 insertafter: '^#Listen '
129 line: Listen 8080
130
131 - community.windows.win_lineinfile:
132 path: C:\Temp\services
133 regex: '^# port for http'
134 insertbefore: '^www.*80/tcp'
135 line: '# port for http by default'
136
137 - name: Create file if it doesn't exist with a specific encoding
138 community.windows.win_lineinfile:
139 path: C:\Temp\utf16.txt
140 create: true
141 encoding: utf-16
142 line: This is a utf-16 encoded file
143
144 - name: Add a line to a file and ensure the resulting file uses unix line separators
145 community.windows.win_lineinfile:
146 path: C:\Temp\testfile.txt
147 line: Line added to file
148 newline: unix
149
150 - name: Update a line using backrefs
151 community.windows.win_lineinfile:
152 path: C:\Temp\example.conf
153 backrefs: true
154 regex: '(^name=)'
155 line: '$1JohnDoe'
156 '''
157
158 RETURN = r'''
159 backup_file:
160 description: Name of the backup file that was created.
161 returned: if backup=yes
162 type: str
163 sample: C:\Path\To\File.txt.11540.20150212-220915.bak
164 '''