ansible中lineinfile模块、replace模块、yum_repository 模块、mount模块相关分享

#主机名为localhost的主机就是ansible2

一、lineinfile模块

1、lineinfile模块介绍

  • 我们可以借助 lineinfile 模块,确保”某一行文本”存在于指定的文件中,或者确保从文件中删除指定的”文本”(即确保指定的文本不存在于文件中),还可以根据正则表达式,替换”某一行文本”。

2、常用参数

  • 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参数 :当要操作的文件并不存在时,是否创建对应的文件。

3、命令示例

准备一个文件/testdir/test,作为实验对象。

[root@ansible1 ~]# cat /testdir/test
Hello ansible,Hiiii
lineinfile -
Ensure a particular line is in a file,
lineinfile -
or replace an existing line using a back-referenced regular expression.
  • file指定文本内容

    确保指定的“ 内容 ”存在于文件中,如果指定的内容本来就存在于文件中,则不做任何操作,如果不存在,默认在文件的末尾插入这行文本,如下命令表示确保 “test lineinfile” 这行文本存在于 /testdir/test 文件中。

    [root@ansible1 testdir]# ansible ansible1 -m lineinfile -a 'path=/testdir/test line="test lineinfile"'
    ansible1 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "backup": "", 
        "changed": true, 
        "msg": "line added"
    }
    

    因为 “test lineinfile” 不存在,所以在文件末尾插入这行文本。

[root@ansible1 testdir]# cat test 
Hello ansible,Hiiii
lineinfile -
Ensure a particular line is in a file,
lineinfile -
or replace an existing line using a back-referenced regular expression.
test lineinfile #新插入的行
  • regexp参数 :使用正则表达式匹配对应的行,当替换文本时,如果有多行文本都能被匹配,则只有最后面被匹配到的那行文本才会被替换,当删除文本时,如果有多行文本都能被匹配,这么这些行都会被删除。

    如下命令表示根据正则表达式替换”某一行”,如果不止一行能够匹配正则,那么只有最后一个匹配正则的行才会被替换,被匹配行会被替换成 line 参数指定的内容,但是如果指定的表达式没有匹配到任何一行,那么 line 中的内容会被添加到文件的最后一行。

[root@ansible1 testdir]# ansible ansible1 -m lineinfile -a 'path=/testdir/test regexp="^line" line="test lineinfile"'
ansible1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "backup": "", 
    "changed": true, 
    "msg": "line replaced"
}
[root@ansible1 testdir]# cat test 
Hello ansible,Hiiii
lineinfile -  #被匹配到的内容,但是没被替换
Ensure a particular line is in a file,
test lineinfile  #被匹配到的内容,被替换成line中指定的内容
or replace an existing line using a back-referenced regular expression.
test lineinfile

上面正则表达式表示匹配以”line”开头的行,最终匹配到了2行,但只替换了最后一个匹配的行。

[root@ansible1 testdir]# ansible ansible1 -m lineinfile -a 'path=/testdir/test regexp="^line123" line="test lineinfile2"'
ansible1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "backup": "", 
    "changed": true, 
    "msg": "line added"
}

上面正则表达式表示以”line123”开头的行,没有匹配到任何一行,则内容加到文件的最后一行。

[root@ansible1 testdir]# cat test 
Hello ansible,Hiiii
lineinfile -
Ensure a particular line is in a file,
test lineinfile
or replace an existing line using a back-referenced regular expression.
test lineinfile
test lineinfile2  #没有匹配到以line123开头的,在最后一行插入line中的内容
  • backrefs参数:详情介绍请浏览常用参数解释

如下命令表示根据正则表达式替换”某一行”,如果不止一行能够匹配正则,那么只有最后一个匹配正则的行才会被替换,被匹配行会被替换成 line 参数指定的内容,但是如果指定的表达式没有匹配到任何一行,那么则不对文件进行任何操作。

[root@ansible1 testdir]# ansible ansible1 -m lineinfile -a 'path=/testdir/test regexp="^line" line="test lineinfile3" backrefs=yes'
ansible1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "backup": "", 
    "changed": true, 
    "msg": "line replaced"
}

上面正则表达式表示以”line”开头的行,最终匹配到了1行,则替换了所匹配的行。

[root@ansible1 testdir]# cat test 
Hello ansible,Hiiii
test lineinfile3  #匹配到已经替换
Ensure a particular line is in a file,
test lineinfile
or replace an existing line using a back-referenced regular expression.
test lineinfile
test lineinfile2

再次执行,已经没有可匹配的行,则不做任何操作。

  • state参数:详情介绍请浏览常用参数解释

    根据 line 参数的内容删除行,如果文件中有多行都与 line 参数的内容相同,那么这些相同的行都会被删除。

[root@ansible1 testdir]# ansible ansible1 -m lineinfile -a 'path=/testdir/test line="test lineinfile" state=absent'
ansible1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "backup": "", 
    "changed": true, 
    "found": 2, 
    "msg": "2 line(s) removed"
}
[root@ansible1 testdir]# cat test 
Hello ansible,Hiiii
test lineinfile3
Ensure a particular line is in a file,
or replace an existing line using a back-referenced regular expression.
test lineinfile2
#匹配到两行line中的内容已经被删除

根据正则表达式删除对应行,如果有多行都满足正则表达式,那么所有匹配的行都会被删除。

[root@ansible1 testdir]# ansible ansible1 -m lineinfile -a 'path=/testdir/test regexp="^test" state=absent'
ansible1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "backup": "", 
    "changed": true, 
    "found": 2, 
    "msg": "2 line(s) removed"
}
[root@ansible1 testdir]# cat test 
Hello ansible,Hiiii
Ensure a particular line is in a file,
or replace an existing line using a back-referenced regular expression.
#以test开头的行都被删除

默认情况下,lineinfile 模块不支持后向引用。如果将 backrefs 设置为 yes,表示开启支持后向引用。使用如下命令,可以将 test 示例文件中的 “Hello ansible,Hiiii” 替换成 “Hiiii”,如果不设置 backrefs=yes,则不支持后向引用,那么 “Hello ansible,Hiiii” 将被替换成 “\2”。

[root@ansible1 testdir]# ansible ansible1 -m lineinfile -a 'path=/testdir/test regexp="(H.{4}).*(H.{4})" line="\2" backrefs=yes'
ansible1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "backup": "", 
    "changed": true, 
    "msg": "line replaced"
}
[root@ansible1 testdir]# cat test 
Hiiii  #已经被替换
Ensure a particular line is in a file,
or replace an existing line using a back-referenced regular expression.

二、replace模块

1、replace模块解释

replace 模块可以根据我们指定的正则表达式替换文件中的字符串,文件中所有被匹配到的字符串都会被替换。

2、常用参数

path参数 :必须参数,指定要操作的文件,2.3版本之前,只能使用 dest, destfile, name指定要操作的文件,2.4版本中,仍然可以使用这些参数名,这些参数名作为 path 参数的别名使用。

regexp参数 : 必须参数,指定一个 python 正则表达式,文件中与正则匹配的字符串将会被替换。

replace参数 : 指定最终要替换成的字符串。

backup参数 :是否在修改文件之前对文件进行备份,最好设置为yes。

3、实例

还是用上边那个文件

[root@ansible1 testdir]# cat test 
Hiiii
Ensure a particular line is in a file,
or replace an existing line using a back-referenced regular expression.

把 主机中的 /testdir/test 文件中的所有 Hiiii 替换成 abc

[root@ansible1 testdir]# ansible ansible1 -m replace -a 'path=/testdir/test regexp="Hiiii" replace=abc'
ansible1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "msg": "1 replacements made"
}
[root@ansible1 testdir]# cat test 
abc #和lineinfile的区别在于,replace会把全文中的Hiiii都给替换,而lineinfile只会替换最后一行。
Ensure a particular line is in a file,
or replace an existing line using a back-referenced regular expression.

备份命令

[root@ansible1 testdir]# ansible ansible1 -m replace -a 'path=/testdir/test regexp="Hiiii" replace=abc backup=yes'

三、yum_repository 模块

1、yum_repository 模块解释

yum_repository 模块可以帮助我们管理远程主机上的 yum 仓库。

2、常用参数

name参数:必须参数,用于指定要操作的唯一的仓库ID,也就是”.repo”配置文件中每个仓库对应的”中括号”内的仓库ID。

baseurl参数:此参数用于设置 yum 仓库的 baseurl。

description参数:此参数用于设置仓库的注释信息,也就是”.repo”配置文件中每个仓库对应的”name字段”对应的内容。

file参数:此参数用于设置仓库的配置文件名称,即设置”.repo”配置文件的文件名前缀,在不使用此参数的情况下,默认以 name 参数的仓库ID作为”.repo”配置文件的文件名前缀,同一个”.repo” 配置文件中可以存在多个 yum 源。

enabled参数:此参数用于设置是否激活对应的 yum 源,此参数默认值为 yes,表示启用对应的 yum 源,设置为 no 表示不启用对应的 yum 源。

gpgcheck参数:此参数用于设置是否开启 rpm 包验证功能,默认值为 no,表示不启用包验证,设置为 yes 表示开启包验证功能。

gpgcakey参数:当 gpgcheck 参数设置为 yes 时,需要使用此参数指定验证包所需的公钥。

state参数:默认值为 present,当值设置为 absent 时,表示删除对应的 yum 源。

3、实例

1.在 ansible-demo3 主机上设置ID为 aliEpel 的 yum 源,仓库配置文件路径为 /etc/yum.repos.d/aliEpel.repo

[root@ansible1 testdir]# ansible ansible2 -m yum_repository -a 'name=aliEpel description="alibaba EPEL" baseurl=https://mirrors.aliyun.com/epel/$releasever\Server/$basearch/'
ansible2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "repo": "aliEpel", 
    "state": "present"
}
[root@localhost yum.repos.d]# ls
aliEpel.repo  CentOS-Base.repo  epel.repo
#aliEpel.repo创建成功,前缀没有设置默认以name参数的内容为前缀。
[root@localhost yum.repos.d]# cat aliEpel.repo 
[aliEpel] #仓库ID
baseurl = https://mirrors.aliyun.com/epel/$releasever\Server/$basearch/  #baseurl
name = alibaba EPEL #name字段

2.在 ansible-demo3 主机上设置ID为 aliEpel 的 yum 源,仓库配置文件路径为 /etc/yum.repos.d/alibaba.repo

此条命令和上面的区别在于增加了file参数

[root@ansible1 testdir]# ansible ansible2 -m yum_repository -a 'name=aliEpel description="alibaba EPEL" baseurl=https://mirrors.aliyun.com/epel/$releasever\Server/$basearch/ file=alibaba'
ansible2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "repo": "aliEpel", 
    "state": "present"
}
[root@localhost yum.repos.d]# ls
alibaba.repo  aliEpel.repo  CentOS-Base.repo  epel.repo
#因为设置了file参数,所以前缀以file参数的值命名。
[root@localhost yum.repos.d]# cat alibaba.repo 
[aliEpel]
baseurl = https://mirrors.aliyun.com/epel/$releasever\Server/$basearch/
name = alibaba EPEL

3.在 ansible-demo3 主机上设置ID为 local 的 yum 源,但是不启用它。

[root@ansible1 testdir]# ansible ansible2 -m yum_repository -a 'name=aliEpel description="alibaba EPEL" baseurl=https://mirrors.aliyun.com/epel/$releasever\Server/$basearch/ file=alibaba enabled=no'

4.在 ansible-demo3 主机上设置ID为 local 的 yum 源,开启包验证功能,并指定验证包所需的公钥位置为 /media/RPM-GPG-KEY-CentOS-7

[root@ansible1 testdir]# ansible ansible2 -m yum_repository -a 'name=aliEpel description="alibaba EPEL" baseurl=https://mirrors.aliyun.com/epel/$releasever\Server/$basearch/ file=alibaba gpgcheck=yes gpgcakey=file:///media/RPM-GPG-KEY-CentOS-7''

5.在 ansible-demo3 主机上删除 /etc/yum.repos.d/alibaba.repo 配置文件中的 aliEpel 源。

[root@ansible1 testdir]# ansible ansible2 -m yum_repository -a 'file=alibaba name=aliEpel state=absent'
ansible2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "repo": "aliEpel", 
    "state": "absent"
}
[root@localhost yum.repos.d]# ls
aliEpel.repo  CentOS-Base.repo  epel.repo
#alibaba.repo源已经被删除

四、mount模块

1、mount模块介绍

mount模块用于挂载磁盘

2、实例

1、格式化磁盘

添加一个磁盘,挂载磁盘之前需要用filesystem模块格式化磁盘。

#fstype是文件系统,dev是指定格式化的磁盘
[root@ansible1 testdir]# ansible ansible2 -m filesystem -a 'fstype=ext4 dev=/dev/sdb'
ansible2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true
}

2、挂载磁盘

#name是指定挂载目录,src指定磁盘,fstype文件系统,其中state的可选值为:absent\mounted\umounted,opts是传递给mount的参数
[root@ansible1 testdir]# ansible ansible2 -m mount -a 'name=/mnt/disk3 src=/dev/sdb fstype=ext4 state=mounted opts=rw'
ansible2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "dump": "0", 
    "fstab": "/etc/fstab", 
    "fstype": "ext4", 
    "name": "/mnt/disk3", 
    "opts": "rw", 
    "passno": "0", 
    "src": "/dev/sdb"
}
[root@localhost ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0  100G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   99G  0 part 
  ├─centos-root 253:0    0   95G  0 lvm  /
  └─centos-swap 253:1    0    4G  0 lvm  [SWAP]
sdb               8:16   0   20G  0 disk /mnt/disk3  #挂载成功
sr0              11:0    1 1024M  0 rom  

五、unarchive模块

1、unarchive模块解释

压缩解压

2、参数解释

creates:一个文件名,当它已经存在时,这个步骤将不会被运行。
copy:默认为yes,拷贝的文件从ansible主机复制到远程主机,no在远程主机上寻找src源文件解压
src:tar源路径,可以是ansible主机上的路径,也可以是远程主机上的路径,如果是远程主机上的路径,则需设置copy=no
dest:远程主机上的目标绝对路径
mode:设置解压缩后的文件权限
exec:列出需要排除的目录和文件
remote_src:设置remote_src=yes为解包目标上已经存在的档案。对于Windows目标,改用win_unzip模块。
owner:解压后文件或目录的属主
group:解压后的目录或文件的属组

3、实例

1 、在远程主机上解压文件并设置权限

[root@ansible1 testdir]# ansible ansible2 -m unarchive -a 'src=/mnt/rinetd.tar.gz dest=/opt/ copy=no mode=0755'
ansible2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "dest": "/opt/", 
    "extract_results": {
        "cmd": [
            "/usr/bin/gtar", 
            "--extract", 
            "-C", 
            "/opt/", 
            "-z", 
            "-f", 
            "/mnt/rinetd.tar.gz"
        ], 
        "err": "", 
        "out": "", 
        "rc": 0
    }, 
    "gid": 0, 
    "group": "root", 
    "handler": "TgzArchive", 
    "mode": "0755", 
    "owner": "root", 
    "size": 41, 
    "src": "/mnt/rinetd.tar.gz", 
    "state": "directory", 
    "uid": 0
}
[root@localhost mnt]# ls
disk3  rinetd.tar.gz  ss
[root@localhost mnt]# ls /opt/
a  a.sh  rinetd
[root@localhost opt]# ll
total 4
drwxr-xr-x 2 root root   6 Jun 30 10:55 a
-rw-r--r-- 1 root root 440 Jun 30 10:30 a.sh
drwxr-xr-x 2 root root 275 Mar 27 13:27 rinetd #权限755

2、 解压ansible管理机上的压缩文件到远程主机并设置权限

[root@ansible1 testdir]# ls
redis-4.0.9.tar.gz  test
[root@ansible1 testdir]# ansible ansible2 -m unarchive -a "src=/testdir/redis-4.0.9.tar.gz dest=/opt/ mode=0755 copy=yes"
ansible2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "dest": "/opt/", 
    "extract_results": {
        "cmd": [
            "/usr/bin/gtar", 
            "--extract", 
            "-C", 
            "/opt/", 
            "-z", 
            "-f", 
            "/root/.ansible/tmp/ansible-tmp-1593523689.47-39546-43514213791936/source"
        ], 
        "err": "", 
        "out": "", 
        "rc": 0
    }, 
    "gid": 0, 
    "group": "root", 
    "handler": "TgzArchive", 
    "mode": "0755", 
    "owner": "root", 
    "size": 60, 
    "src": "/root/.ansible/tmp/ansible-tmp-1593523689.47-39546-43514213791936/source", 
    "state": "directory", 
    "uid": 0
}
[root@localhost opt]# ll
total 4
drwxr-xr-x 2 root root   6 Jun 30 10:55 a
-rw-r--r-- 1 root root 440 Jun 30 10:30 a.sh
drwxr-xr-x 6 root root 309 Mar 27  2018 redis-4.0.9 #权限755,解压成功
drwxr-xr-x 2 root root 275 Mar 27 13:27 rinetd

猜你喜欢

转载自blog.csdn.net/weixin_46299169/article/details/107989656