@ -19,13 +19,11 @@
DOCUMENTATION = """
DOCUMENTATION = """
- - -
- - -
module : win_lineinfile
module : win_lineinfile
author : Brian Lloyd ( brian . d . lloyd @gmail.com )
author : " Brian Lloyd <brian.d.lloyd@gmail.com> "
short_description : Ensure a particular line is in a file , or replace an
short_description : Ensure a particular line is in a file , or replace an existing line using a back - referenced regular expression .
existing line using a back - referenced regular expression .
description :
description :
- This module will search a file for a line , and ensure that it is present or absent .
- This module will search a file for a line , and ensure that it is present or absent .
- This is primarily useful when you want to change a single line in
- This is primarily useful when you want to change a single line in a file only .
a file only .
version_added : " 2.0 "
version_added : " 2.0 "
options :
options :
dest :
dest :
@ -36,11 +34,7 @@ options:
regexp :
regexp :
required : false
required : false
description :
description :
- The regular expression to look for in every line of the file . For
- " 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 will be replaced. For C(state=absent), the pattern of the line to remove. Uses .NET compatible regular expressions; see U(https://msdn.microsoft.com/en-us/library/hs600312 % 28v=vs.110 % 29.aspx). "
C ( state = present ) , the pattern to replace if found ; only the last line
found will be replaced . For C ( state = absent ) , the pattern of the line
to remove . Uses . NET compatible regular expressions ; see
U ( https : / / msdn . microsoft . com / en - us / library / hs600312 % 28 v = vs .110 % 29. aspx ) .
state :
state :
required : false
required : false
choices : [ present , absent ]
choices : [ present , absent ]
@ -50,92 +44,62 @@ options:
line :
line :
required : false
required : false
description :
description :
- Required for C ( state = present ) . The line to insert / replace into the
- Required for C ( state = present ) . The line to insert / replace into the file . If C ( backrefs ) is set , may contain backreferences that will get expanded with the C ( regexp ) capture groups if the regexp matches .
file . If C ( backrefs ) is set , may contain backreferences that will get
expanded with the C ( regexp ) capture groups if the regexp matches .
backrefs :
backrefs :
required : false
required : false
default : " no "
default : " no "
choices : [ " yes " , " no " ]
choices : [ " yes " , " no " ]
description :
description :
- Used with C ( state = present ) . If set , line can contain backreferences
- Used with C ( state = present ) . If set , line can contain backreferences ( both positional and named ) that will get populated if the C ( regexp ) matches . This flag changes the operation of the module slightly ; C ( insertbefore ) and C ( insertafter ) will be ignored , and if the C ( regexp ) doesn ' t match anywhere in the file, the file will be left unchanged.
( both positional and named ) that will get populated if the C ( regexp )
- If the C ( regexp ) does match , the last matching line will be replaced by the expanded line parameter .
matches . This flag changes the operation of the module slightly ;
C ( insertbefore ) and C ( insertafter ) will be ignored , and if the C ( regexp )
doesn ' t match anywhere in the file, the file will be left unchanged.
If the C ( regexp ) does match , the last matching line will be replaced by
the expanded line parameter .
insertafter :
insertafter :
required : false
required : false
default : EOF
default : EOF
description :
description :
- Used with C ( state = present ) . If specified , the line will be inserted
- Used with C ( state = present ) . If specified , the line will be inserted after the last match of specified regular expression . A special value is available ; C ( EOF ) for inserting the line at the end of the file .
after the last match of specified regular expression . A special value is
- If specified regular expresion has no matches , EOF will be used instead . May not be used with C ( backrefs ) .
available ; C ( EOF ) for inserting the line at the end of the file .
If specified regular expresion has no matches , EOF will be used instead .
May not be used with C ( backrefs ) .
choices : [ ' EOF ' , ' *regex* ' ]
choices : [ ' EOF ' , ' *regex* ' ]
insertbefore :
insertbefore :
required : false
required : false
version_added : " 1.1 "
description :
description :
- Used with C ( state = present ) . If specified , the line will be inserted
- Used with C ( state = present ) . If specified , the line will be inserted before the last match of specified regular expression . A value is available ; C ( BOF ) for inserting the line at the beginning of the file .
before the last match of specified regular expression . A value is
- If specified regular expresion has no matches , the line will be inserted at the end of the file . May not be used with C ( backrefs ) .
available ; C ( BOF ) for inserting the line at the beginning of the file .
If specified regular expresion has no matches , the line will be
inserted at the end of the file . May not be used with C ( backrefs ) .
choices : [ ' BOF ' , ' *regex* ' ]
choices : [ ' BOF ' , ' *regex* ' ]
create :
create :
required : false
required : false
choices : [ " yes " , " no " ]
choices : [ " yes " , " no " ]
default : " no "
default : " no "
description :
description :
- Used with C ( state = present ) . If specified , the file will be created
- 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 .
if it does not already exist . By default it will fail if the file
is missing .
backup :
backup :
required : false
required : false
default : " no "
default : " no "
choices : [ " yes " , " no " ]
choices : [ " yes " , " no " ]
description :
description :
- Create a backup file including the timestamp information so you can
- Create a backup file including the timestamp information so you can get the original file back if you somehow clobbered it incorrectly .
get the original file back if you somehow clobbered it incorrectly .
validate :
validate :
required : false
required : false
description :
description :
- validation to run before copying into place .
- Validation to run before copying into place . Use % s in the command to indicate the current file to validate .
Use % s in the command to indicate the current file to validate .
- The command is passed securely so shell features like expansion and pipes won ' t work.
The command is passed securely so shell features like
expansion and pipes won ' t work.
required : false
required : false
default : None
default : None
encoding :
encoding :
required : false
required : false
default : " auto "
default : " auto "
description :
description :
- Specifies the encoding of the source text file to operate on ( and thus what the
- 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 the module to auto - detect the encoding of the source file and ensure that the modified file is written with the same encoding .
output encoding will be ) . The default of C ( auto ) will cause the module to auto - detect
- " 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 - see U(https://msdn.microsoft.com/en-us/library/system.text.encoding % 28v=vs.110 % 29.aspx). "
the encoding of the source file and ensure that the modified file is written with the
- 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 specific encoding , the default encoding ( UTF - 8 , no BOM ) will be used .
same encoding .
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 - see
U ( https : / / msdn . microsoft . com / en - us / library / system . text . encoding % 28 v = vs .110 % 29. aspx ) .
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 specific encoding , the default encoding
( UTF - 8 , no BOM ) will be used .
newline :
newline :
required : false
required : false
description :
description :
- Specifies the line separator style to use for the modified file . This defaults
- Specifies the line separator style to use for the modified file . This defaults to the windows line separator ( \r \n ) . Note that the indicated line separator will be used for file output regardless of the original line seperator that appears in the input file .
to the windows line separator ( \r \n ) . Note that the indicated line separator
will be used for file output regardless of the original line seperator that
appears in the input file .
choices : [ " windows " , " unix " ]
choices : [ " windows " , " unix " ]
default : " windows "
default : " windows "
"""
"""
EXAMPLES = r """
EXAMPLES = """
- win_lineinfile : dest = C : \\temp \\example . conf regexp = ^ name = line = " name=JohnDoe "
- win_lineinfile : dest = C : \\temp \\example . conf regexp = ^ name = line = " name=JohnDoe "
- win_lineinfile : dest = C : \\temp \\example . conf state = absent regexp = " ^name= "
- win_lineinfile : dest = C : \\temp \\example . conf state = absent regexp = " ^name= "