Ansible常用模块

Ansible中常用模块

一、Ansible命令运行方式及常用参数

1、命令的格式:

ansible 清单 -m 模块 -a 模块参数

2、常用的参数:

名称含义
- -version显示版本
-m module指定模块,默认为command模块
-v详细过程 -vv -vvv更详细过程
- -list显示主机列表,也可以用–list-hosts
-k提示输入ssh连接密码,默认key认证
-C预执行检测
-T执行命令的超时时间,默认10s
-b执行sudo切换身份操作
-become-user=USERNAME指定sudo的用户
-K提示输入sudo密码

二、Ansible中常用模块

a、ping模块

语法:
ansible  hosts主机清单  -m  模块名

[root@192 ansible]# ansible 192.168.129.135 -m ping
[root@192 ansible]# ansible all -m ping		//all代表所有主机清单
[root@192 ansible]# ansible web -m ping
192.168.129.135 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

b、command模块

command模块用于在远程主机上执行命令,ansible默认就是使用command模块。不能使用特殊得符号 :| > >>

官方文档:https://docs.ansible.com/ansible/latest/modules/command_module.html#command-module

常用参数

名称含义
chdir执行命令前先进入到指定目录
cmd运行命令指定
creates当指定文件存在时,后一条命令不执行 / 指定文件不存在,后一条命令执行
removes当指定文件存在时,后一条命令执行 / 指定文件不存在,后一条命令不执行

语法:

ansible 主机清单 -m 模块名 -a ‘执行命令’

# /etc文件存在,则touch命令不执行
[root@192 ansible]# ansible web -m command -a 'chdir=/tmp creates=/etc touch file1'
192.168.129.137 | SUCCESS | rc=0 >>
skipped, since /etc exists

# /etc文件存在,则touch命令执行
[root@192 ansible]# ansible web -m command -a 'chdir=/tmp removes=/etc touch file1'
[WARNING]: Consider using the file module with state=touch rather than running 'touch'.  If you need to use command because file is
insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
192.168.129.137 | CHANGED | rc=0 >>

linux中的通配符 | 重定向在command模块中不适用

[root@192 ansible]# ansible web -m command -a 'chdir=/tmp rm -rf *'
[WARNING]: Consider using the file module with state=absent rather than running 'rm'.  If you need to use command because file is
insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
192.168.129.137 | CHANGED | rc=0 >>

[root@shazi tmp]# ls
file1

[root@192 ansible]# ansible web -m shell -a 'chdir=/tmp rm -rf *'
[WARNING]: Consider using the file module with state=absent rather than running 'rm'.  If you need to use command because file is
insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
192.168.129.137 | CHANGED | rc=0 >>

[root@shazi tmp]# ls

c、raw模块

raw模块支持管道符与重定向

//支持重定向
[root@192 ansible]# ansible web -m raw -a 'echo 'hello' > /tmp/abc' 
192.168.129.137 | CHANGED | rc=0 >>
Shared connection to 192.168.129.137 closed.

[root@192 ansible]# ansible web -m command -a 'cat /tmp/abc'
192.168.129.137 | CHANGED | rc=0 >>
hello

//支持管道符
[root@192 ansible]# ansible web -m raw -a'cat /tmp/abc | grep -Eo hello'
Enter passphrase for key '/root/.ssh/id_rsa': 
192.168.129.137 | CHANGED | rc=0 >>
hello
Shared connection to 192.168.129.137 closed.

d、shell模块

shell模块用于在受控机上执行受控机上的脚本,亦可直接在受控机上执行命令。
shell模块亦支持管道与重定向。

官方文档:https://docs.ansible.com/ansible/latest/modules/shell_module.html#shell-module

  • 常用参数
名称含义
chdir执行命令前先进入到指定目录
creates如果文件存在则不执行
removes如果文件存在将执行
[root@192 ansible]# ansible web -m shell -a 'ls /tmp | grep file'
192.168.129.137 | CHANGED | rc=0 >>
file

[root@192 ansible]# ansible web -m shell -a 'mkdir /root/test1'
[WARNING]: Consider using the file module with state=directory rather than running 'mkdir'.  If you need to use command because file is
insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
192.168.129.137 | CHANGED | rc=0 >>

[root@shazi ~]# ls
abc  anaconda-ks.cfg  test1
  • ansible常用模块raw、command、shell的区别:
名称含义
shell模块调用/bin/sh指令执行
command模块不是调用的shell的指令,所以没有bash的环境变量
raw模块很多地方和shell类似,更多的地方建议使用shell和command模块

e、script模块

script模块用于在受控机上执行主控机上的脚本

[root@192 ansible]# mkdir /scripts
[root@192 ~]# cd /scripts/
[root@192 scripts]# cat a.sh 
#! /bin/bash
echo hello world
echo zhi shazi
        echo xioii
[root@192 scripts]# chmod +x /scripts/a.sh
[root@192 scripts]# ll /scripts/a.sh 
-rwxr-xr-x. 1 root root 30 Jul 18 12:31 /scripts/a.sh

[root@192 scripts]# ansible web -m script -a '/scripts/a.sh'
192.168.129.137 | CHANGED => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to 192.168.129.137 closed.\r\n",
    "stderr_lines": [
        "Shared connection to 192.168.129.137 closed."
    ],
    "stdout": "hello world\r\nzhi shazi\r\nxioii\r\n",
    "stdout_lines": [
        "hello world",
        "zhi shazi",
        "xioii"
    ]
}

f、template模块

template模块用于生成一个模板,并可将其传输至远程主机上。

1、下载一个Centos-8的yum源文件
[root@192 yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2595  100  2595    0     0  10136      0 --:--:-- --:--:-- --:--:-- 10136

2、将Centos-8源传到受控主机
[root@192 yum.repos.d]# ansible web -m template -a 'src=/etc/yum.repos.d/CentOS-Base.repo dest=/etc/yum.repos.d/Centos8.repo'
192.168.129.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "4966466ad015ef3d2a3cc0b8252d43efbdcf2c94",
    "dest": "/etc/yum.repos.d/Centos8.repo",
    "gid": 0,
    "group": "root",
    "md5sum": "d06fb7d5709727828bcaba7457ea673e",
    "mode": "0644",
    "owner": "root",
    "secontext": "system_u:object_r:system_conf_t:s0",
    "size": 2595,
    "src": "/root/.ansible/tmp/ansible-tmp-1626586130.9004688-479453-51846552834534/source",
    "state": "file",
    "uid": 0
}
3、查看受控机上是否有Centos-8[root@shazi yum.repos.d]# ls
Centos8.repo  CentOS-Base.repo  redhat.repo

g、yum模块

官方文档:https://docs.ansible.com/ansible/latest/modules/yum_repository_module.html#yum-repository-module

参数选项/默认值释义
name(required)指定软件名称信息
stateabsent/removed将软件进行卸载(慎用)
=present/installed将软件进行安装
latest安装最新的软件 yum update
[root@192 etc]# ansible web -m yum -a "name=wget* state=installed"  
Enter passphrase for key '/root/.ssh/id_rsa': 
192.168.129.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Installed: wget-1.19.5-10.el8.x86_64",
        "Removed: wget-1.19.5-8.el8_1.1.x86_64"
    ]
}
[root@shazi ~]# rpm -qa | grep wget
wget-1.19.5-10.el8.x86_64

h、copy模块

copy模块用于复制文件至远程受控机。

官方文档:https://docs.ansible.com/ansible/latest/modules/copy_module.html#copy-module

  • update_cache: 更新缓存

  • name: 要创建的文件名字

  • state: 状态(present,absent,latest),表示是安装还卸载
       present:默认的,表示为安装
       lastest: 安装为最新的版本
       absent:表示删除

  • mode: 目标文件的权限模式,模式可以被指定为符号模式(例如,u + rwx 或 u = rw,g = r,o = r)

  • backup: 如果原目标文件存在,则先备份目标文件

  • force: 是否强制覆盖,默认为yes

  • owner: 目标文件属主

  • group: 目标文件属组

参数选项/默认值释义
src指定将本地管理主机的什么数据信息进行远程复制
backupno* yes默认数据复制到远程主机,会覆盖原有文件(yes 将源文件进行备份)
content在文件中添加信息
dest(required)将数据复制到远程节点的路径信息
group文件数据复制到远程主机,设置文件属组用户信息
mode文件数据复制到远程主机,设置数据的权限 eg 0644 0755
owner文件数据复制到远程主机,设置文件属主用户信息
remote_srcno* yes如果设置为yes,表示将远程主机上的数据进行移动操作如果设置为no, 表示将管理主机上的数据进行分发操作

1、目的文件的权限

[root@192 ansible]# ansible web -m copy -a 'src=/root/mode.txt dest=/tmp  mode=744'
192.168.129.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
    "dest": "/tmp/mode.txt",
    "gid": 0,
    "group": "root",
    "mode": "0744",
    "owner": "root",
    "path": "/tmp/mode.txt",
    "secontext": "unconfined_u:object_r:admin_home_t:s0",
    "size": 0,
    "state": "file",
    "uid": 0
}

[root@192 ansible]# ansible web -m shell -a 'ls -l /tmp/mode.txt'
192.168.129.137 | CHANGED | rc=0 >>
-rwxr--r--. 1 root root 0 718 12:54 /tmp/mode.txt

2、如果目的地文件已存在,备份目的地的原文件

[root@192 ansible]# ansible web -m copy -a 'src=/tmp/abc dest=/tmp/abc owner=ddd group=ddd mode=755 backup=yes'
192.168.129.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "backup_file": "/tmp/abc.493505.2021-07-18@13:08:38~",
    "changed": true,
    "checksum": "71415f2fcb9291dd06cfc730ae2838e69e45c234",
    "dest": "/tmp/abc",
    "gid": 1001,
    "group": "ddd",
    "md5sum": "3c4ee07f99a204bb8a0a0f940a8e1fcd",
    "mode": "0755",
    "owner": "ddd",
    "secontext": "unconfined_u:object_r:user_tmp_t:s0",
    "size": 18,
    "src": "/root/.ansible/tmp/ansible-tmp-1626584917.9604204-442099-277721032319/source",
    "state": "file",
    "uid": 1001
}
[root@shazi tmp]# ll
总用量 8
-rwxr-xr-x. 1 ddd  ddd  18 718 13:08 abc
-rw-r--r--. 1 root root  6 717 17:56 abc.493505.2021-07-18@13:08:38~
-rwxr--r--. 1 root root  0 718 12:54 mode.txt
[root@shazi tmp]# cat abc
linux
linux
hello
[root@shazi tmp]# cat abc.493505.2021-07-18@13\:08\:38~ 
hello

i、group模块

group模块用于在受控机上添加或删除组

官方文档:https://docs.ansible.com/ansible/latest/modules/group_module.html#group-module

参数选项/默认值释义
gid指创建的组ID信息
name指创建组名称信息
stateabsent删除指定的用户组
=present创建指定的用户组

1、在受控机上添加一个系统组,其gid为306,组名为zzz

[root@192 ~]# ansible web -m group -a 'name=zzz gid=306 state=present'
192.168.129.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "gid": 306,
    "name": "zzz",
    "state": "present",
    "system": false
}
[root@192 ~]# ansible web -m shell -a 'grep zzz /etc/group'
192.168.129.137 | CHANGED | rc=0 >>
zzz:x:306:

2、删除受控机上的zzz组

[root@192 ~]# ansible web -m group -a 'name=zzz state=absent'
192.168.129.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "name": "zzz",
    "state": "absent"
}

[root@192 ~]# ansible web -m shell -a 'grep zzz /etc/group'
192.168.129.137 | FAILED | rc=1 >>
non-zero return code

j、user模块

user模块用于管理受控机的用户帐号

官方文档:https://docs.ansible.com/ansible/latest/modules/user_module.html#user-module

参数选项/默认值释义
password请输入密码信息
name指定用户名信息
uid指定用户uid信息
group指定用户主要属于哪个组
groups指定用户属于哪个附加组信息
shell/bin/bash或/sbin/nologin指定是否能够登录
create_homeyes/no是否创建家目录信息
home指定家目录创建在什么路径 默认/home

1、在受控机上添加一个系统用户,用户名为zzz,uid为366,设置其shell为/sbin/nologin,无家目录

[root@192 ~]# ansible web -m user -a 'name=zzz uid=366 system=yes create_home=no shell=/sbin/nologin state=present'
192.168.129.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "comment": "",
    "create_home": false,
    "group": 366,
    "home": "/home/zzz",
    "name": "zzz",
    "shell": "/sbin/nologin",
    "state": "present",
    "system": true,
    "uid": 366
}
[root@192 ~]# ansible web -m shell -a 'grep zzz /etc/group'
192.168.129.137 | CHANGED | rc=0 >>
zzz:x:366:
[root@192 ~]# ansible web -m shell -a 'ls /home'
192.168.129.137 | CHANGED | rc=0 >>
ddd
hhh
hhr

2、修改zzz用户的uid为306

[root@192 ~]# ansible web -m user -a 'name=zzz uid=306'
192.168.129.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "append": false,
    "changed": true,
    "comment": "",
    "group": 366,
    "home": "/home/zzz",
    "move_home": false,
    "name": "zzz",
    "shell": "/sbin/nologin",
    "state": "present",
    "uid": 306
}
[root@192 ~]# ansible web -m shell -a 'grep zzz /etc/group'
192.168.129.137 | CHANGED | rc=0 >>
zzz:x:366:
[root@192 ~]# ansible web -m shell -a 'grep zzz /etc/passwd'
192.168.129.137 | CHANGED | rc=0 >>
zzz:x:306:366::/home/zzz:/sbin/nologin

3、删除用户时不删除家目录

[root@192 ~]# ansible web -m user -a 'name=zzz state=absent'
192.168.129.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "force": false,
    "name": "zzz",
    "remove": false,
    "state": "absent"
}

4、删除用户时删除用户的家目录

[root@192 ~]# ansible web -m user -a 'name=hhh state=absent remove=yes'
Enter passphrase for key '/root/.ssh/id_rsa': 
192.168.129.137 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "name": "hhh",
    "state": "absent"
}

k、service模块

service模块用于管理受控机上的服务

官方文档:https://docs.ansible.com/ansible/latest/modules/service_module.html#service-module

参数选项/默认值释义
enabledno /yes设置服务是否开机自启动 如果参数不指定,原有服务开机自启动状态进行保留
name (required)设置要启动/停止服务名称
state=reloaded平滑重启
=restarted重启
=started启动
=stopped停止
  • 查看受控机上的vsftpd服务是否启动
[root@192 ~]# ansible web -m shell -a 'systemctl is-active vsftpd'
192.168.129.137 | FAILED | rc=3 >>
inactivenon-zero return code
  • 启动vsftpd服务
1、安装vsftpd服务
[root@192 ~]# ansible web -m yum -a 'name=vsftpd* state=installed'
192.168.129.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Installed: vsftpd-3.0.3-33.el8.x86_64"
    ]
}
2、启动
[root@192 ~]# ansible web -m service -a 'name=vsftpd state=started'
192.168.129.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
		....此处省略N行
  • 查看受控机上的vsftpd服务是否启动
[root@192 ~]# ansible web -m shell -a 'systemctl is-active vsftpd'
192.168.129.137 | CHANGED | rc=0 >>
active
  • 查看受控机上的vsftpd服务是否开机自启
[root@192 ~]# ansible web -m shell -a 'systemctl is-enabled vsftpd'
192.168.129.137 | FAILED | rc=1 >>
disablednon-zero return code
  • 设置vsftpd服务开机自启
[root@192 ~]# ansible web -m service -a 'name=vsftpd enabled=yes'
Enter passphrase for key '/root/.ssh/id_rsa': 
192.168.129.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
		......此处省略N行
  • 查看受控机上的vsftpd服务是否开机自启
[root@192 ~]# ansible web -m shell -a 'systemctl is-enabled vsftpd'
192.168.129.137 | CHANGED | rc=0 >>
enabled
  • 重启vsftpd服务
[root@192 ~]# ansible web -m service -a 'name=vsftpd state=restarted'
Enter passphrase for key '/root/.ssh/id_rsa': 
192.168.129.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
		......此处省略N行
  • 停止受控机上的vsftpd服务
[root@192 ~]# ansible web -m service -a 'name=vsftpd state=stopped'
192.168.129.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
		......此处省略N行
[root@192 ~]# ansible web -m shell -a 'systemctl is-active vsftpd'
Enter passphrase for key '/root/.ssh/id_rsa': 
192.168.129.137 | FAILED | rc=3 >>
inactivenon-zero return code

l、file模块

官方文档:https://docs.ansible.com/ansible/latest/modules/file_module.html#file-module

参数选项/默认值释义
dest/path/name(required)将数据复制到远程节点的路径信息
group文件数据复制到远程主机,设置文件属组用户信息
mod文件数据复制到远程主机,设置数据的权限 eg 0644 0755
owner文件数据复制到远程主机,设置文件属主用户信息
src指定将本地管理主机的什么数据信息进行远程复制
stateabsent将数据进行删除
=directory创建一个空目录信息
=file查看指定目录信息是否存在
=touch创建一个空文件信息
=hard/link创建链接文件
名称含义
state=absent删除文件或者目录
owner属主
group属组
set4777 属主特殊权限
2777 属组特殊权限
1777 执行特殊权限
7777 全部特殊权限

1、创建一个普通文件

[root@192 ~]# ansible web -m file -a 'path=/tmp/gool state=touch'

2、创建一个目录

[root@192 ~]# ansible web -m file -a 'path=/tmp/test state=directory'

3、查看

[root@192 ~]# ansible web -m shell -a 'ls /tmp'
192.168.129.137 | CHANGED | rc=0 >>
gool
test

4、删除文件

[root@192 ~]# ansible web -m file -a 'state=absent path=/tmp/gool'

5、删除目录

[root@192 ~]# ansible web -m file -a 'state=absent path=/tmp/test'

6、查看

[root@192 ~]# ansible web -m shell -a 'ls /tmp'
192.168.129.137 | CHANGED | rc=0 >>

7、创建一个软连接文件

[root@192 ~]# ansible web -m file -a 'state=link src=/tmp/goole path=/tmp/谷歌'

[root@shazi tmp]# ll
总用量 0
lrwxrwxrwx. 1 root root 10 718 16:34 谷歌 -> /tmp/goole
-rw-r--r--. 1 root root  0 718 16:33 goole

8、创建一个硬连接文件

[root@192 ~]# ansible web -m file -a 'state=hard src=/etc/passwd path=/tmp/passwd'

[root@shazi tmp]# ll
总用量 4
-rw-r--r--. 3 root root 2655 718 15:29 passwd
[root@shazi etc]# ll /passwd 
-rw-r--r--. 1 root root 2655 718 15:29 /passwd

9、创建一个文件,并设置权限,属主,属组

//创建zzz用户
[root@192 ~]# ansible web -m user -a 'name=zzz uid=366  system=yes create_home=no shell=/sbin/nologin state=present'

[root@192 ~]# ansible web -m file -a 'state=touch path=/tmp/fiel.txt mode=744 owner=zzz group=zzz'

10、修改一个文件得权限,属主,属组

[root@192 ~]# ansible web -m file -a 'path=/tmp/goole mode=777'
[root@192 ~]# ansible web -m file -a 'path=/tmp/fiel.txt owner=root group=root'

11、修改一个文件得set位特殊权限

[root@192 ~]# ansible web -m file -a 'path=/tmp/goole mode=2777'
[root@192 ~]# ansible web -m shell -a 'ls -l /tmp/goole'
192.168.129.137 | CHANGED | rc=0 >>
-rwxrwsrwx. 1 root root 0 718 16:33 /tmp/goole

12、若想同时改变目录及其中的子目录或字文件的属性,可以用recurse=yes参数实现

[root@192 ~]# ansible web -m file -a 'path=/tmp group=zzz recurse=yes'
[root@shazi tmp]# ll
总用量 4
lrwxrwxrwx. 1 root zzz   10 718 16:34 谷歌 -> /tmp/goole
-rwxrwxrwx. 1 root zzz    0 718 16:42 fiel.txt
-rwxrwxrwx. 1 root zzz    0 718 16:33 goole

m、lineinfile模块

名称含义
regexp=’^ $’正则匹配,匹配数字
path指定要操作的文件
line指定文本的内容
regexp使用正则表达式匹配对应的行
当替换文本时,如果有多行文本被匹配,只替换最后匹配的那一行
当删除文本时,如果有多行文本被匹配,那么这些行都会被删除
statestate的默认值为present
state=absent时,表示删除文本
backrefs当没有根据匹配规则找到对应的行时不做任何更改,默认值为no
当 backrefs=yes,没有根据匹配规则找到对应的行,指定的文本内容添加
当backrefs=yes时,也可以向后引用regexp变量信息
insertafter将文本插入到指定的行之后,参数的值可以设定为EOF或者正则表达式
insertbefore将文本插入到指定的行之前,参数的值可以设定为BOF或者正则表达式
backup是否在修改文件前备份
create当要操作的文件不存在时,是否创建对应的文件

1、当要操作的文件不存在时,是否创建对应的文件

[root@192 ~]# ansible web -m lineinfile -a 'path=/tmp/fiel line="hello woorld" '
192.168.129.137 | FAILED! => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "msg": "Destination /tmp/fiel does not exist !",
    "rc": 257
}

//create=yes当要操作的文件不存在时,创建对应的文件
[root@192 ~]# ansible web -m lineinfile -a 'path=/tmp/fiel line="hello woorld" create=yes'
192.168.129.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "backup": "",
    "changed": true,
    "msg": "line added"
}

//查看
root@shazi tmp]# ll
总用量 8
-rw-r--r--. 1 root root 13 718 17:26 fiel
-rw-r--r--. 1 root root 96 718 17:26 fiel.txt
-rw-r--r--. 1 root root  0 718 16:33 goole

2、将匹配到的含hello字符串的最后一行行替换为line中的字符串

[root@192 ~]# ansible web -m lineinfile -a 'path=/tmp/fiel line="Centos" regexp='hello' create=yes'

[root@shazi tmp]# cat fiel
linux
hello 
world hello
helll
Centos

3、当 backrefs=yes,同时根据匹配规则找不到相应的行时,不对文件进行任何改变

[root@192 ~]# ansible web -m lineinfile -a 'path=/tmp/fiel line="hehe" regexp="jjyy" backrefs=yes'

4、^正则表达式

[root@192 ~]# ansible web -m lineinfile -a 'path=/etc/selinux/config regexp="^SELINUX=" line="SELINUX=disabled" '

[root@shazi selinux]# cat config 
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.

5、在文件的开头插入line中的字符

[root@192 ~]# ansible web -m lineinfile -a 'path=/tmp/fiel line="shazi" insertbefore=BOF'

[root@shazi tmp]# cat fiel
shazi
linux
hello 

6、在文件的结尾插入line中的字符

[root@192 ~]# ansible web -m lineinfile -a 'path=/tmp/fiel line="xxx" insertafter=EOF'
[root@shazi tmp]# cat fiel
shazi
linux
Centos
xxx
7、在含有hello字符的行前插入line中的字符
[root@192 ~]# ansible web -m lineinfile -a 'path=/tmp/fiel line="****" insertbefore="hello" '

[root@shazi tmp]# cat fiel
shazi
linux
****
hello 
Centos
xxx
8、在含有hello字符的行后插入line中的字符
[root@192 ~]# ansible web -m insertafter -a 'path=/tmp/fiel line="%%%%" insertbefore="hello" '

[root@shazi tmp]# cat fiel
shazi
linux
****
hello 
%%%%
Centos
xxx

n、blockinfile 模块

给文件中添加一段内容,并注明标志

1、添加

block=''   			文档内容
marker='#{mark}'   标志信息
state			将指定文本"插入"到文件中	

[root@192 ~]# ansible web -m blockinfile -a 'block="nginx" path=/tmp/fiel marker="#{mark} nginx"'
Enter passphrase for key '/root/.ssh/id_rsa': 
192.168.129.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "Block inserted"
}
[root@shazi tmp]# cat fiel
#BEGIN nginx
nginx
#END nginx

2、在末尾添加

[root@192 ~]# ansible web -m blockinfile -a 'path=/tmp/fiel block="systemctl\start " '

[root@shazi tmp]# cat fiel
shazi
linux
# BEGIN ANSIBLE MANAGED BLOCK
systemctl\start 
# END ANSIBLE MANAGED BLOCK

3、删除:

[root@192 ~]# ansible web -m blockinfile -a 'path=/tmp/fiel block="systemctl\start " state=absent'

[root@shazi tmp]# cat fiel
shazi
linux

o、cron模块

官方文档:https://docs.ansible.com/ansible/latest/modules/cron_module.html#cron-module

参数选项/默认值释义
minute/hour/day/month/weekday和设置时间信息相关参数
job和设置定时任务相关参数
name(required)设置定时任务注释信息
stateabsent删除指定定时任务
disabledyes将指定定时任务进行注释
=no取消注释

1、模块方法

 1. backup:对远程主机上的原任务计划内容修改之前做备份 
 2. cron_file:如果指定该选项,则用该文件替换远程主机上的cron.d目录下的用户的任务计划 
 3. day:日(1-31**/2,……) 
 4. hour:小时(0-23**/2,……)  
 5. minute:分钟(0-59**/2,……) 
 6. month:月(1-12**/2,……) 
 7. weekday:周(0-7*,……)
 8. job:要执行的任务,依赖于state=present 
 9. name:该任务的描述 
10. special_time:指定什么时候执行,参数:reboot,yearly,annually,monthly,weekly,daily,hourly 
11. state:确认该任务计划是创建还是删除 
12. user:以哪个用户的身份执行

2、示例:

  • 添加定时任务
[root@192 ~]# ansible web -m cron -a 'name="test crontab" minute=5 hour=1 job="echo test"'
192.168.129.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "envs": [],
    "jobs": [
        "test crontab"
    ]
}
[root@shazi tmp]# crontab -l
#Ansible: test crontab
5 1 * * * echo test
  • 移除定时任务
[root@192 ~]# ansible web -m cron -a 'name="test crontab" minute=5 hour=1 job="echo test" state="absent"'
192.168.129.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "envs": [],
    "jobs": []
}

p、unarchive 解压模块

- name: Unarchive a file that is already on the remote machine
  unarchive:
    src: /tmp/foo.zip            #要解压的包
    dest: /usr/local/bin        #解压到目标位置
    remote_src: 
        yes                        #要解压的包在受控端
        no                        #要解压的包在控制端

示例:

#1.解压控制端的包到受控端
[root@m01 /package]# ansible web -m unarchive -a 'src=/package/php.tar.gz dest=/tmp/'

#2.解压受控端的包到受控端
[root@m01 /package]# ansible web -m unarchive -a 'src=/package/php.tar.gz dest=/tmp/ remote_src=yes'

q、archive 压缩模块

EXAMPLES:
- name: Compress directory /path/to/foo/ into /path/to/foo.tgz
  archive:
    path: /path/to/foo            #要压缩的文件或目录
    dest: /path/to/foo.tgz        #压缩后的文件
    format:bz2, gz, tar, xz, zip    #指定打包的类型

示例:

#1.打包站点目录
[root@m01 /package]# ansible web01 -m archive -a 'path=/code dest=/tmp/code.tar.gz'

三、运维发布方式有哪些?

1、蓝绿部署

用于downtime应用上线时的一套部署策略 不停止老版,额外搞一套新版本,等测试发现新版本OK后,删除老版本。
斜体样式在部署新版本之前,需要将部署新版本的流量掐断,全部打到ok的老版本上。
缺点:需要有两倍的机器资源,得保持冗余的服务始终在线
优点:两个版本可能都可能组任何时间点获取流量

2、灰度发布(金丝线发布)

在不停止老版本的情况下,额外搞一套新版本,经常与A/B测试一起使用,用于测试选择多种方案
精确的流量分发控制
做监控

3、滚动发布

一般是取出一个或者多个服务器停止服务,执行更新,并重新将其投入使用。周而复始,直到集群中所有的实例都更新成新版本
优点:比蓝绿部署节约资源
缺点:服务器节点数量多,会很慢

4、红黑部署

测试不通过,找到问题原因后,直接干掉新生成的服务器以及Autoscaling Group就可以,测试通过,则将ELB指向新的服务器集群,然后销毁掉旧的服务器集群以及AutoScaling Group
优点:服务始终在线,同时采用不可变部署的方式,也不像蓝绿部署一样得保持冗余的服务始终在线
缺点:在蓝绿色部署中,两个版本可能暂时同时获取请求,而在红黑中,只有一个版本在任何时间点获得流量

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值