我们在日常的工作中经常会遇到需要修改配置文件中某个项的值的情况,什么apache啊,mysql啊,docker 啊, 等等 ,当然我们可以使用template等其它方式来实现,但是今天我们要说一下这个lineinfile的正则替换,聚个例子来说,docker的配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# /etc/sysconfig/docker # Modify these options if you want to change the way the docker daemon runs OPTIONS='--selinux-enabled' DOCKER_CERT_PATH=/etc/docker # If you want to add your own registry to be used for docker search and docker # pull use the ADD_REGISTRY option to list a set of registries, each prepended # with --add-registry flag. The first registry added will be the first registry # searched. ADD_REGISTRY='--add-registry registry.access.redhat.com' # If you want to block registries from being used, uncomment the BLOCK_REGISTRY # option and give it a set of registries, each prepended with --block-registry # flag. For example adding docker.io will stop users from downloading images # from docker.io # BLOCK_REGISTRY='--block-registry' # If you have a registry secured with https but do not have proper certs # distributed, you can tell docker to not look for full authorization by # adding the registry to the INSECURE_REGISTRY line and uncommenting it. # INSECURE_REGISTRY='--insecure-registry' # On an SELinux system, if you remove the --selinux-enabled option, you # also need to turn on the docker_transition_unconfined boolean. # setsebool -P docker_transition_unconfined 1 # Location used for temporary files, such as those created by # docker load and build operations. Default is /var/lib/docker/tmp # Can be overriden by setting the following environment variable. # DOCKER_TMPDIR=/var/tmp # Controls the /etc/cron.daily/docker-logrotate cron job status. # To disable, uncomment the line below. # LOGROTATE=false |
其中有一行 OPTIONS=’–selinux-enabled‘ , 可能我需要增加一些其它的参数,例如–selinux-enabled –log-driver json-file –log-opt max-size=50m
1 2 3 4 5 |
- name: Set various Docker options lineinfile: dest: /etc/sysconfig/docker regexp: '^OPTIONS=.*$' line: "OPTIONS='--selinux-enabled --log-driver json-file --log-opt max-size=50m'" |
这样就可以了,而且, lineinfile 还可以在文章末尾追加一行你想加入的
1 2 3 4 |
- name: lineinfile: dest: /tmp/test.txt line: "the line should be added last" |
这样,不加入正则,那么就会在文章的末尾加入一个行the line should be added last
附录完整使用参数
parameter | required | default | choices | comments |
---|---|---|---|---|
backrefs | no | 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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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.
|
Latest posts by Zhiming Zhang (see all)
- aws eks node 自动化扩展工具 Karpenter - 8月 10, 2022
- ReplicationController and ReplicaSet in Kubernetes - 12月 20, 2021
- public key fingerprint - 5月 27, 2021