我们在日常的工作中经常会遇到需要修改配置文件中某个项的值的情况，什么apache啊，mysql啊，docker 啊， 等等 ，当然我们可以使用template等其它方式来实现，但是今天我们要说一下这个lineinfile的正则替换，聚个例子来说，docker的配置文件
# Modify these options if you want to change the way the docker daemon runs
# 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
# 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
# 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.
# 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.
# Controls the /etc/cron.daily/docker-logrotate cron job status.
# To disable, uncomment the line below.
其中有一行 OPTIONS=’–selinux-enabled‘ ， 可能我需要增加一些其它的参数，例如–selinux-enabled –log-driver json-file –log-opt max-size=50m
- name: Set various Docker options
line: "OPTIONS='--selinux-enabled --log-driver json-file --log-opt max-size=50m'"
这样就可以了，而且， lineinfile 还可以在文章末尾追加一行你想加入的
line: "the line should be added last"
这样，不加入正则，那么就会在文章的末尾加入一个行the line should be added last
Create a backup file including the timestamp information so you can get the original file back if you somehow clobbered it incorrectly.
The file to modify.
aliases: name, destfile
Name of the group that should own the file/directory, as would be fed to chown.
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,
All arguments accepted by the file module also work here.
Name of the user that should own the file/directory, as would be fed to chown.
(added in 1.7)
The regular expression to look for in every line of the file. For
Level part of the SELinux file context. This is the MLS/MCS attribute, sometimes known as the
Role part of SELinux file context,
Type part of SELinux file context,
User part of SELinux file context. Will default to system policy, if applicable. If set to
Whether the line should be there or not.
(added in 2.2)
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.
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.