Debian Codegraph @

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