ansible中常用模块详解

ansible中常用的模块详解:

file模块

ansible内置的可以查看模块用法的命令如下:

[root@docker5 ~]# ansible-doc -s file
- name: Sets attributes of files
  file:
      attributes:            # Attributes the file or directory should have. To get supported flags look at the man page for `chattr' on the target system. This
                               string should contain the attributes in the same order as the one displayed by `lsattr'.
      follow:                # This flag indicates that filesystem links, if they exist, should be followed. Previous to Ansible 2.5, this was `no' by default.
      force:                 # force the creation of the symlinks in two cases: the source file does not exist (but will appear later); the destination exists and
                               is a file (so, we need to unlink the "path" file and create symlink to the "src" file in place of
                               it).
      group:                 # Name of the group that should own the file/directory, as would be fed to `chown'.
      mode:                  # Mode the file or directory should be. For those used to `/usr/bin/chmod' remember that modes are actually octal numbers (like `0644'
                               or `01777'). 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').
      owner:                 # Name of the user that should own the file/directory, as would be fed to `chown'.
      path:                  # (required) path to the file being managed.  Aliases: `dest', `name'
      recurse:               # recursively set the specified file attributes (applies only to state=directory)
      selevel:               # 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:                # Role part of SELinux file context, `_default' feature works as for `seuser'.
      setype:                # Type part of SELinux file context, `_default' feature works as for `seuser'.
      seuser:                # 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.
      src:                   # path of the file to link to (applies only to `state=link' and `state=hard'). Will accept absolute, relative and nonexisting paths.
                               Relative paths are not expanded.
      state:                 # If `directory', all immediate subdirectories will be created if they do not exist, since 1.7 they will be created with the supplied
                               permissions. If `file', the file will NOT be created if it does not exist, see the [copy] or
                               [template] module if you want that behavior.  If `link', the symbolic link will be created or
                               changed. Use `hard' for hardlinks. If `absent', directories will be recursively deleted, and files or
                               symlinks will be unlinked. Note that `absent' will not cause `file' to fail if the `path' does not
                               exist as the state did not change. If `touch' (new in 1.4), an empty file will be created if the
                               `path' does not exist, while an existing file or directory will receive updated file access and
                               modification times (similar to the way `touch` works from the command line).
      unsafe_writes:         # 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.
[root@docker5 ~]# 
ansible-doc -s file

file模块用来设置文件属性,并且创建或者删除目录,创建连接。

相关选项如下:
force:需要在两种情况下强制创建软链接,一种是源文件不存在,但之后会建立的情况下;另一种是目标软链接已存在,需要先取消之前的软链,然后创建新的软链,有两个选项:yes|no
group:定义文件/目录的属组
mode:定义文件/目录的权限
owner:定义文件/目录的属主
path:必选项,定义文件/目录的路径
recurse:递归设置文件的属性,只对目录有效
src:被链接的源文件路径,只应用于state=link的情况
dest:被链接到的路径,只应用于state=link的情况
state:
       directory:如果目录不存在,就创建目录
       file:即使文件不存在,也不会被创建
       link:创建软链接
       hard:创建硬链接
       touch:如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间
       absent:删除目录、文件或者取消链接文件

利用file模块创建一个文件:

[root@docker5 ~]# ansible -i hosts all -m file -a "path=/tmp/test state=touch owner=root group=root"
10.0.102.162 | SUCCESS => {
    "changed": true, 
    "dest": "/tmp/test", 
    "gid": 0, 
    "group": "root", 
    "mode": "0644", 
    "owner": "root", 
    "secontext": "unconfined_u:object_r:user_tmp_t:s0", 
    "size": 0, 
    "state": "file", 
    "uid": 0
}
10.0.102.200 | SUCCESS => {
    "changed": true, 
    "dest": "/tmp/test", 
    "gid": 0, 
    "group": "root", 
    "mode": "0644", 
    "owner": "root", 
    "secontext": "unconfined_u:object_r:user_tmp_t:s0", 
    "size": 0, 
    "state": "file", 
    "uid": 0
}
10.0.102.212 | SUCCESS => {
    "changed": true, 
    "dest": "/tmp/test", 
    "gid": 0, 
    "group": "root", 
    "mode": "0644", 
    "owner": "root", 
    "secontext": "unconfined_u:object_r:user_tmp_t:s0", 
    "size": 0, 
    "state": "file", 
    "uid": 0
}

copy模块

主要的作用是复制文件到远程主机:

相关选项如下:
backup:在覆盖之前,将源文件备份,备份文件包含时间信息。有两个选项:yes|no
content:用于替代“src”,可以直接设定指定文件的值
dest:必选项。要将源文件复制到的远程主机的绝对路径,如果源文件是一个目录,那么该路径也必须是个目录
directory_mode:递归设定目录的权限,默认为系统默认权限
force:如果目标主机包含该文件,但内容不同,如果设置为yes,则强制覆盖,如果为no,则只有当目标主机的目标位置不存在该文件时,才复制。默认为yes
others:所有的file模块里的选项都可以在这里使用
src:被复制到远程主机的本地文件,可以是绝对路径,也可以是相对路径。如果路径是一个目录,它将递归复制。在这种情况下,如果路径使用“/”来结尾,则只复制目录里的内容,如果没有使用“/”来结尾,则包含目录在内的整个内容全部复制,类似于rsync

示例如下:

## 将本地文件“/etc/yum.repos.d/CentOS-Media.repo”复制到远程服务器
[root@zxwd05 ~]# ansible cluster_hosts -m file -a "path=/etc/yum.repos.d state=absent"
[root@zxwd05 ~]# ansible cluster_hosts -m file -a "path=/etc/yum.repos.d state=directory"
[root@zxwd05 ~]# ansible cluster_hosts -m copy -a "src=/etc/yum.repos.d/CentOS-Media.repo dest=/etc/yum.repos.d/ "

cron模块

我们知道crontab的主要作用就是制定定时计划任务,cron模块的主要作用和crontab命令是一样的。

我们知道在linux中添加一个计划任务需要指定计划任务执行的时间,执行的命令。而cron模块也是这样。

[root@zxwd05 ~]# ansible cluster_hosts -m cron -a 'name="custom job" minute=*/3 hour=* day=* month=* weekday=* job="/usr/sbin/ntpdate 192.168.30.139"'

name: 指定计划任务的名字,
分,时,日,月,周分别指定计划任务执行的时间点。
job:指定计划任务指定的命令。
state:指定此计划任务的状态。present表示当前添加,absent表示删除。

[root@zxwd05 ~]# ansible cluster_hosts -m cron -a 'name="custom job" state="absent"'          #删除计划任务。

service模块

service模块主要用来对系统中的服务进行管理。

相关参数如下:
name: 指定服务的名称。
state:指定对服务进行的操作,started, stopped, restarted, reloaded。【是对应动词的过去分词形式】
enabled: yes|no, 是否加入开机自启动。
runlevel: 启动的级别。

启动对应服务器上的mysql服务,如下:

[root@docker5 ~]# ansible -i /root/hosts all -m service -a "name=mysqld state=started enabled=no"

command模块

command 模块可以帮助我们在远程主机上执行命令。
注意:使用 command 模块在远程主机中执行命令时,不会经过远程主机的 shell 处理,在使用 command 模块时,如果需要执行的命令中含有重定向、管道符等操作时,这些符号也会失效,比如”<”, “>”, “|”, “;” 和 “&” 这些符号,如果你需要这些功能,可以参考后面介绍的 shell 模块。还有一点需要注意,如果远程节点是 windows 操作系统,则需要使用 win_command 模块。执行 ansible 时,不加 -m 默认使用 command ,可以在 /etc/ansible/ansible.cfg 中修改。
command模块的几个选项如下:

相关选项如下:
creates:一个文件名,当该文件存在,则该命令不执行
free_form:要执行的linux指令,这里的free_form不需要写成赋值的形式,直接写要执行的命令即可。
chdir:在执行指令之前,先切换到该目录
removes:一个文件名,当该文件不存在,则该选项不执行
executable:切换shell来执行指令,该执行路径必须是一个绝对路径

实例如下:

[root@docker5 ~]# ansible -i /root/hosts all -a "w"
10.0.102.212 | SUCCESS | rc=0 >>
 10:25:27 up 8 days, 13:04,  2 users,  load average: 0.09, 0.03, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1                      12Feb19  8days  0.01s  0.01s -bash
root     pts/1    10.0.102.209     10:25    0.00s  0.12s  0.02s w

10.0.102.200 | SUCCESS | rc=0 >>
 10:25:30 up 9 days, 17:52,  3 users,  load average: 0.05, 0.04, 0.06
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1                      11Feb19  9days  0.01s  0.01s -bash
root     pts/1    172.16.100.19    08:02    2:19m  0.02s  0.01s /usr/local/mysql/bin/mysql -uroot -px xxxx
root     pts/2    10.0.102.209     10:25    0.00s  0.16s  0.03s w

10.0.102.162 | SUCCESS | rc=0 >>
 10:25:30 up 9 days, 17:52,  3 users,  load average: 0.16, 0.05, 0.06
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1                      11Feb19  9days  0.07s  0.07s -bash
root     pts/1    172.16.100.19    08:02    2:20m  0.03s  0.01s /usr/local/mysql/bin/mysql -uroot -px xxxx
root     pts/2    10.0.102.209     10:25    1.00s  0.21s  0.03s w

[root@docker5 ~]# ansible -i /root/hosts all -a "w creates=/tmp/test" 
10.0.102.212 | SUCCESS | rc=0 >>
skipped, since /tmp/test exists

10.0.102.200 | SUCCESS | rc=0 >>
skipped, since /tmp/test exists

10.0.102.162 | SUCCESS | rc=0 >>
skipped, since /tmp/test exists

[root@docker5 ~]# 

上面已经提到过command模块不能执行管道,而shell模块可以,因此在平时使用时习惯使用shell模块。

[root@docker5 ~]# ansible -i /root/hosts all -a "ps aux |grep tidb" 
10.0.102.200 | FAILED | rc=1 >>
error: garbage option

Usage:
 ps [options]

 Try 'ps --help <simple|list|output|threads|misc|all>'
  or 'ps --help <s|l|o|t|m|a>'
 for additional help text.

For more details see ps(1).non-zero return code

10.0.102.212 | FAILED | rc=1 >>
error: garbage option

Usage:
 ps [options]

 Try 'ps --help <simple|list|output|threads|misc|all>'
  or 'ps --help <s|l|o|t|m|a>'
 for additional help text.

For more details see ps(1).non-zero return code

10.0.102.162 | FAILED | rc=1 >>
error: garbage option

Usage:
 ps [options]

 Try 'ps --help <simple|list|output|threads|misc|all>'
  or 'ps --help <s|l|o|t|m|a>'
 for additional help text.

For more details see ps(1).non-zero return code
[root@docker5 ~]# ansible -i /root/hosts all -m shell -a "ps aux |grep tidb" 
10.0.102.212 | SUCCESS | rc=0 >>
tidb      4392  0.1  0.3 119144 14028 ?        Ssl  Feb14  13:53 bin/node_exporter --web.listen-address=:9100 --log.level=info
tidb      4393  0.0  0.0 113120   628 ?        S    Feb14   0:00 /bin/bash /home/tidb/deploy/scripts/run_node_exporter.sh
tidb      4397  0.0  0.0 107896   656 ?        S    Feb14   0:00 tee -i -a /home/tidb/deploy/log/node_exporter.log
tidb      4532  0.2  0.2  19596 11580 ?        Ssl  Feb14  19:19 bin/blackbox_exporter --web.listen-address=:9115 --log.level=info --config.file=conf/blackbox.yml
tidb      4533  0.0  0.0 113120   620 ?        S    Feb14   0:00 /bin/bash /home/tidb/deploy/scripts/run_blackbox_exporter.sh
tidb      4538  0.0  0.0 107896   656 ?        S    Feb14   0:00 tee -i -a /home/tidb/deploy/log/blackbox_exporter.log
tidb      4670  2.1  1.7 10600204 66912 ?      Ssl  Feb14 197:53 bin/pd-server --name=pd3 --client-urls=http://10.0.102.212:2379 --advertise-client-urls=http://10.0.102.212:2379 --peer-urls=http://10.0.102.212:2380 --advertise-peer-urls=http://10.0.102.212:2380 --data-dir=/home/tidb/deploy/data.pd --initial-cluster=pd1=http://10.0.102.200:2380,pd2=http://10.0.102.209:2380,pd3=http://10.0.102.212:2380 --config=conf/pd.toml --log-file=/home/tidb/deploy/log/pd.log
tidb      4809  0.5  5.8 968436 227172 ?       Ssl  Feb14  50:55 bin/tikv-server --addr 0.0.0.0:20160 --advertise-addr 10.0.102.212:20160 --pd 10.0.102.200:2379,10.0.102.209:2379,10.0.102.212:2379 --data-dir /home/tidb/deploy/data --config conf/tikv.toml --log-file /home/tidb/deploy/log/tikv.log
root     24478  0.0  0.0 113120  1172 pts/1    S+   10:27   0:00 /bin/sh -c ps aux |grep tidb
root     24480  0.0  0.0 112652   904 pts/1    S+   10:27   0:00 grep tidb

10.0.102.200 | SUCCESS | rc=0 >>
tidb      9928  0.1  0.8 127340 15376 ?        Ssl  Feb14  14:51 bin/node_exporter --web.listen-address=:9100 --log.level=info
tidb      9929  0.0  0.0 113120   228 ?        S    Feb14   0:00 /bin/bash /home/tidb/deploy/scripts/run_node_exporter.sh
tidb      9933  0.0  0.0 107896   256 ?        S    Feb14   0:00 tee -i -a /home/tidb/deploy/log/node_exporter.log
tidb     10091  0.2  0.5  19596  9464 ?        Ssl  Feb14  20:12 bin/blackbox_exporter --web.listen-address=:9115 --log.level=info --config.file=conf/blackbox.yml
tidb     10092  0.0  0.0 113120   224 ?        S    Feb14   0:00 /bin/bash /home/tidb/deploy/scripts/run_blackbox_exporter.sh
tidb     10096  0.0  0.0 107896   260 ?        S    Feb14   0:00 tee -i -a /home/tidb/deploy/log/blackbox_exporter.log
tidb     10254  2.3  4.9 10607996 92648 ?      Ssl  Feb14 216:46 bin/pd-server --name=pd1 --client-urls=http://10.0.102.200:2379 --advertise-client-urls=http://10.0.102.200:2379 --peer-urls=http://10.0.102.200:2380 --advertise-peer-urls=http://10.0.102.200:2380 --data-dir=/home/tidb/deploy/data.pd --initial-cluster=pd1=http://10.0.102.200:2380,pd2=http://10.0.102.209:2380,pd3=http://10.0.102.212:2380 --config=conf/pd.toml --log-file=/home/tidb/deploy/log/pd.log
tidb     10421  0.7  2.0  60528 39284 ?        Ssl  Feb14  69:56 bin/tidb-server -P 4000 --status=10080 --path=10.0.102.200:2379,10.0.102.209:2379,10.0.102.212:2379 --config=conf/tidb.toml --log-file=/home/tidb/deploy/log/tidb.log
root     29979  0.0  0.0 113120  1172 pts/2    S+   10:27   0:00 /bin/sh -c ps aux |grep tidb
root     29981  0.0  0.0 112652   904 pts/2    S+   10:27   0:00 grep tidb

10.0.102.162 | SUCCESS | rc=0 >>
tidb      4260  0.1  0.7 201072 14184 ?        Ssl  Feb14  15:58 bin/node_exporter --web.listen-address=:9100 --log.level=info
tidb      4261  0.0  0.0 113120   228 ?        S    Feb14   0:00 /bin/bash /home/tidb/deploy/scripts/run_node_exporter.sh
tidb      4262  0.0  0.0 107896   272 ?        S    Feb14   0:00 tee -i -a /home/tidb/deploy/log/node_exporter.log
tidb      4400  0.2  0.5  19596 11300 ?        Ssl  Feb14  22:11 bin/blackbox_exporter --web.listen-address=:9115 --log.level=info --config.file=conf/blackbox.yml
tidb      4401  0.0  0.0 113120   228 ?        S    Feb14   0:00 /bin/bash /home/tidb/deploy/scripts/run_blackbox_exporter.sh
tidb      4402  0.0  0.0 107896   280 ?        S    Feb14   0:00 tee -i -a /home/tidb/deploy/log/blackbox_exporter.log
tidb      4539  0.4 11.6 931560 220016 ?       Ssl  Feb14  42:11 bin/tikv-server --addr 0.0.0.0:20160 --advertise-addr 10.0.102.162:20160 --pd 10.0.102.200:2379,10.0.102.209:2379,10.0.102.212:2379 --data-dir /home/tidb/deploy/data --config conf/tikv.toml --log-file /home/tidb/deploy/log/tikv.log
root      6779  0.0  0.0 113120  1172 pts/2    S+   10:27   0:00 /bin/sh -c ps aux |grep tidb
root      6781  0.0  0.0 112652   904 pts/2    S+   10:27   0:00 grep tidb

[root@docker5 ~]# 
command与shell比较的一个实例

script模块

script 模块可以帮助我们在远程主机上执行 ansible 管理主机上的脚本,也就是说,脚本一直存在于 ansible 管理主机本地,不需要手动拷贝到远程主机后再执行。

script模块的用法如下:

[root@docker5 tasks]# ansible-doc -s script
- name: Runs a local script on a remote node after transferring it
  script:
      chdir:                 # cd into this directory on the remote node before running the script
      creates:               # a filename, when it already exists, this step will *not* be run.
      decrypt:               # This option controls the autodecryption of source files using vault.
      free_form:             # (required) Path to the local script file followed by optional arguments. There is no parameter actually named 'free form'; see the
                               examples!
      removes:               # a filename, when it does not exist, this step will *not* be run.
[root@docker5 tasks]# 

把本地的脚本在远程主机上执行。这个命令的选项和command差不多,不同的是这里执行的是shell脚本而已。

unarchive模块

 这个模块的主要作用就是解压。模块有两种用法:

1:如果参数copy=yes,则把本地的压缩包拷贝到远程主机,然后执行压缩。

2:如果参数copy=no,则直接解压远程主机上给出的压缩包。

creates:指定一个文件名,当该文件存在时,则解压指令不执行

dest:远程主机上的一个路径,即文件解压的路径 

grop:解压后的目录或文件的属组

list_files:如果为yes,则会列出压缩包里的文件,默认为no,2.0版本新增的选项

mode:解决后文件的权限

src:如果copy为yes,则需要指定压缩文件的源路径 

owner:解压后文件或目录的属主

实例如下:

ansible -i /root/hosts all  -m unarchive -a 'src=/usr/loca/src/mysql.tar.gz dest=/usr/local/ copy=no'

与之相对的压缩命令的模块是archive。

archive压缩命令

查看其文档用法如下:

[root@docker5 tasks]# ansible-doc -s archive
- name: Creates a compressed archive of one or more files or trees
  archive:
      attributes:            # Attributes the file or directory should have. To get supported flags look at the man page for `chattr' on the target system. This string should contain the attributes in
                               the same order as the one displayed by `lsattr'.
      dest:                  # The file name of the destination archive. This is required when `path' refers to multiple files by either specifying a glob, a directory or multiple paths in a list.
      exclude_path:          # Remote absolute path, glob, or list of paths or globs for the file or files to exclude from the archive
      format:                # The type of compression to use. Support for xz was added in version 2.5.
      group:                 # Name of the group that should own the file/directory, as would be fed to `chown'.
      mode:                  # Mode the file or directory should be. For those used to `/usr/bin/chmod' remember that modes are actually octal numbers (like `0644' or `01777'). 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').
      owner:                 # Name of the user that should own the file/directory, as would be fed to `chown'.
      path:                  # (required) Remote absolute path, glob, or list of paths or globs for the file or files to compress or archive.
      remove:                # Remove any added source files and trees after adding to archive.
      selevel:               # 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:                # Role part of SELinux file context, `_default' feature works as for `seuser'.
      setype:                # Type part of SELinux file context, `_default' feature works as for `seuser'.
      seuser:                # 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.
      unsafe_writes:         # 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.

 示例如下:

[root@docker5 tasks]# ansible -i /root/hosts master -m archive -a "path=/test/ format=gz dest=/usr/local/src/test.tar.gz"
10.0.102.162 | SUCCESS => {
    "archived": [                            #这个目录里面命名有两个文件,但是只压缩了一个,
        "/test/mysqld"
    ], 
    "arcroot": "/test/", 
    "changed": true, 
    "dest": "/usr/local/src/test.tar.gz", 
    "expanded_exclude_paths": [], 
    "expanded_paths": [
        "/test/"
    ], 
    "gid": 0, 
    "group": "root", 
    "missing": [], 
    "mode": "0644", 
    "owner": "root", 
    "secontext": "unconfined_u:object_r:usr_t:s0", 
    "size": 189, 
    "state": "file", 
    "uid": 0
}
[root@docker5 tasks]# ansible -i /root/hosts master -m shell -a "chdir=/usr/local/src ls"
10.0.102.162 | SUCCESS | rc=0 >>
mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
test.tar.gz

因为源目录里面有两个文件,但是上面的压缩只压缩了一个文件。

[root@docker5 tasks]# ansible -i /root/hosts master -m archive -a "path=/test/* format=gz dest=/usr/local/src/test.tar.gz"
10.0.102.162 | SUCCESS => {
    "archived": [
        "/test/mysqld", 
        "/test/test111"
    ], 
    "arcroot": "/test/", 
    "changed": true, 
    "dest": "/usr/local/src/test.tar.gz", 
    "expanded_exclude_paths": [], 
    "expanded_paths": [
        "/test/mysqld", 
        "/test/test111"
    ], 
    "gid": 0, 
    "group": "root", 
    "missing": [], 
    "mode": "0644", 
    "owner": "root", 
    "secontext": "unconfined_u:object_r:usr_t:s0", 
    "size": 212, 
    "state": "file", 
    "uid": 0
}

#这里虽然压缩了两个文件,但是在解压之后,发现并没有目录,而是把两个文件单独列了出来。
[root@docker4 src]# ls
test.tar.gz
[root@docker4 src]# tar zxvf test.tar.gz
mysqld
test111
[root@docker4 src]# ls
mysqld  test111  test.tar.gz
[root@docker4 src]#

#自己执行tar命令压缩解压
[root@docker4 src]# tar zcvf  test.tar.gz /test
/test/
/test/mysqld
/test/test111
[root@docker4 src]# ls
test.tar.gz
[root@docker4 src]# tar zxvf test.tar.gz
test/
test/mysqld
test/test111
[root@docker4 src]# ls #会有一个test目录
test  test.tar.gz

replace模块

这个模块可以根据我们指定的正则表达式替换文件的匹配的内容。

先看一个例子:

 - name: change the start script
   #shell: sed -i "s/^datadir=/datadir=\/data\/mysql/" /etc/init.d/mysqld
   replace: path=/etc/init.d/mysqld replace="datadir={{ datadir_name }}" regexp="^datadir=" backup=yes

#安装MySQL的时候,需要修改MySQL的启动脚本,配置datadir参数,这里两行的作用是一样的。只是在执行playbook的时候,使用shell模块会报出警告说建议使用replcae模块。

#模块参数如下:
path: 指定远程主机要替换的文件的路径。
regexp: 指定在文件中匹配的正则表达式,上面匹配以“datadir=”开头的行
replace: 指定替换的文件,就是把上面正则匹配到的文件,替换成这里的内容。
backup:表示在对文件操作之前是否备份文件。

lineinfile模块

这个模块会遍历文本中每一行,然后对其中的行进行操作。

path参数 :必须参数,指定要操作的文件。

line参数 : 使用此参数指定文本内容。

regexp参数 :使用正则表达式匹配对应的行,当替换文本时,如果有多行文本都能被匹配,则只有最后面被匹配到的那行文本才会被替换,当删除文本时,如果有多行文本都能被匹配,
        这么这些行都会被删除。 state参数:当想要删除对应的文本时,需要将state参数的值设置为absent,absent为缺席之意,表示删除,state的默认值为present。 backrefs参数:默认情况下,当根据正则替换文本时,即使regexp参数中的正则存在分组,在line参数中也不能对正则中的分组进行引用,除非将backrefs参数的值设置为yes。
      backrefs
=yes表示开启后向引用,这样,line参数中就能对regexp参数中的分组进行后向引用了,这样说不太容易明白,可以参考后面的示例命令理解。backrefs=yes
      除了能够开启后向引用功能,还有另一个作用,默认情况下,当使用正则表达式替换对应行时,如果正则没有匹配到任何的行,那么line对应的内容会被插入到文本的末尾,
      不过,如果使用了backrefs=yes,情况就不一样了,当使用正则表达式替换对应行时,同时设置了backrefs=yes,那么当正则没有匹配到任何的行时,
      则不会对文件进行任何操作,相当于保持原文件不变。 insertafter参数:借助insertafter参数可以将文本插入到“指定的行”之后,insertafter参数的值可以设置为EOF或者正则表达式,EOF为End Of File之意,表示插入到文档的末尾,
     默认情况下insertafter的值为EOF,如果将insertafter的值设置为正则表达式,表示将文本插入到匹配到正则的行之后,如果正则没有匹配到任何行,则插入到文件末尾,
    当使用backrefs参数时,此参数会被忽略。 insertbefore参数:借助insertbefore参数可以将文本插入到“指定的行”之前,insertbefore参数的值可以设置为BOF或者正则表达式,BOF为Begin Of File之意,
      表示插入到文档的开头,如果将insertbefore的值设置为正则表达式,表示将文本插入到匹配到正则的行之前,如果正则没有匹配到任何行,则插入到文件末尾,
      当使用backrefs参数时,此参数会被忽略。 backup参数:是否在修改文件之前对文件进行备份。 create参数 :当要操作的文件并不存在时,是否创建对应的文件。

在远程主机上创建一个测试文件如下:

[root@docker4 test]# cat mysqld 
[mysqld]
skip-grant-tables
datadir=/data/mysql

datadir is test

[mysqld]
apped this row

然后再ansible主机上测试:

[root@docker5 ~]# cat test.yml 
---
 - hosts: master
   remote_user: root
   gather_facts: no

   tasks:
     - name: test the lineinfile module
       lineinfile:
          path=/test/mysqld
          regexp="^\[mysqld\]$"               #匹配以datadir开头的行
          line="test the row..."              #替换为指定的内容
[root@docker5 ~]# 

执行上面的剧本,

[root@docker5 ~]# ansible-playbook -i hosts test.yml 

PLAY [master] *******************************************************************************************************************************************************************************************

TASK [test the lineinfile module] ***********************************************************************************************************************************************************************
changed: [10.0.102.162]

PLAY RECAP **********************************************************************************************************************************************************************************************
10.0.102.162               : ok=1    changed=1    unreachable=0    failed=0   

[root@docker5 ~]#


#查看内容
[root@docker4 test]# cat mysqld              #这里可以看到只有最后一个匹配的才被替换。
[mysqld]
skip-grant-tables
datadir=/data/mysql

datadir is test

test the row...
apped this row

继续测试,在[mysqld]下面添加一行logbin=master.

[root@docker5 ~]# cat test.yml 
---
 - hosts: master
   remote_user: root
   gather_facts: no

   tasks:
     - name: test the lineinfile module
       lineinfile:
          path=/test/mysqld
          line="log-bin=master"
          insertafter="^\[mysqld\]$"          #设为正则表达式,表示文本插入到匹配行之后。

[root@docker5 ~]# ansible-playbook -i hosts test.yml 

PLAY [master] *******************************************************************************************************************************************************************************************

TASK [test the lineinfile module] ***********************************************************************************************************************************************************************
changed: [10.0.102.162]

PLAY RECAP **********************************************************************************************************************************************************************************************
10.0.102.162               : ok=1    changed=1    unreachable=0    failed=0   


#查看结果如下:
[root@docker4 test]# cat mysqld
[mysqld]
log-bin=master
skip-grant-tables
datadir=/data/mysql

datadir is test

test the row...
apped this row

匹配删除以datadir开头的行。

[root@docker5 ~]# cat test.yml 
---
 - hosts: master
   remote_user: root
   gather_facts: no

   tasks:
     - name: test the lineinfile module
       lineinfile:
          path=/test/mysqld
          regexp="^datadir"                 
          state=absent
[root@docker5 ~]# 
执行结果如下:
[root@docker4 test]# cat mysqld 
[mysqld]
log-bin=master
skip-grant-tables


test the row...
apped this row

 

posted @ 2019-02-21 15:28  夜间独行的浪子  阅读(4749)  评论(0编辑  收藏  举报