首页 » 翻译 » Ansible 入门 » 正文

ansible 常用module lineinfile 文件单行正则替换

我们在日常的工作中经常会遇到需要修改配置文件中某个项的值的情况,什么apache啊,mysql啊,docker 啊, 等等 ,当然我们可以使用template等其它方式来实现,但是今天我们要说一下这个lineinfile的正则替换,聚个例子来说,docker的配置文件

其中有一行 OPTIONS=’–selinux-enabled‘ , 可能我需要增加一些其它的参数,例如–selinux-enabled –log-driver json-file –log-opt max-size=50m

这样就可以了,而且, lineinfile 还可以在文章末尾追加一行你想加入的

这样,不加入正则,那么就会在文章的末尾加入一个行the line should be added last

附录完整使用参数

parameter required default choices comments
backrefs

no no
  • yes
  • no
Used with state=present. If set, line can contain backreferences (both positional and named) that will get populated if the regexp matches. This flag changes the operation of the module slightly; insertbefore and insertafter will be ignored, and if the regexp doesn’t match anywhere in the file, the file will be left unchanged. If the regexp does match, the last matching line will be replaced by the expanded line parameter.
backup

no no
  • yes
  • no
Create a backup file including the timestamp information so you can get the original file back if you somehow clobbered it incorrectly.
create

no no
  • yes
  • no
Used with 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.
dest

yes
The file to modify.
aliases: name, destfile

group

no
Name of the group that should own the file/directory, as would be fed to chown.
insertafter

no EOF
  • EOF
  • *regex*
Used with state=present. If specified, the line will be inserted after the last match of specified regular expression. A special value is available; EOF for inserting the line at the end of the file. If specified regular expression has no matches, EOF will be used instead. May not be used with backrefs.
insertbefore

no
  • BOF
  • *regex*
Used with state=present. If specified, the line will be inserted before the last match of specified regular expression. A value is available; BOF for inserting the line at the beginning of the file. If specified regular expression has no matches, the line will be inserted at the end of the file. May not be used with backrefs.
line

no
Required for state=present. The line to insert/replace into the file. If backrefs is set, may contain backreferences that will get expanded with the regexp capture groups if the regexp matches.
mode

no
Mode the file or directory should be. For those used to /usr/bin/chmod remember that modes are actually octal numbers (like 0644). Leaving off the leading zero will likely have unexpected results. As of version 1.8, the mode may be specified as a symbolic mode (for example, u+rwx or u=rw,g=r,o=r).
others

no
All arguments accepted by the file module also work here.
owner

no
Name of the user that should own the file/directory, as would be fed to chown.
regexp

(added in 1.7)
no
The regular expression to look for in every line of the file. For state=present, the pattern to replace if found; only the last line found will be replaced. For state=absent, the pattern of the line to remove. Uses Python regular expressions; seehttp://docs.python.org/2/library/re.html.
selevel

no s0
Level part of the SELinux file context. This is the MLS/MCS attribute, sometimes known as the range. _default feature works as for seuser.
serole

no
Role part of SELinux file context, _default feature works as for seuser.
setype

no
Type part of SELinux file context, _default feature works as for seuser.
seuser

no
User part of SELinux file context. Will default to system policy, if applicable. If set to_default, it will use the user portion of the policy if available.
state

no present
  • present
  • absent
Whether the line should be there or not.
unsafe_writes

(added in 2.2)
no
Normally this module uses atomic operations to prevent data corruption or inconsistent reads from the target files, sometimes systems are configured or just broken in ways that prevent this. One example are docker mounted files, they cannot be updated atomically and can only be done in an unsafe manner.
This boolean option allows ansible to fall back to unsafe methods of updating files for those cases in which you do not have any other choice. Be aware that this is subject to race conditions and can lead to data corruption.
validate

no None
The validation command to run before copying into place. The path to the file to validate is passed in via ‘%s’ which must be present as in the example below. The command is passed securely so shell features like expansion and pipes won’t work.

Zhiming Zhang

Senior devops at Appannie
一个奔跑在运维路上的胖子
Zhiming Zhang

Latest posts by Zhiming Zhang (see all)