[toc]

AD-hoc概述

什么是ad-hoc?

ad-hoc简而言之就是“临时命令”,执行完即结束,并不会保存

官方模块参考:官方模块

ansible执行任务的方式

1
2
3
4
5
6
# 执行任务的方式
AD-hoc # 执行linux基础命令
playbook # 执行linux的脚本

# ansible语法
ansible 主机 -m 模块 -a '动作'

AD-hoc结果返回颜色

1
2
3
4
绿色:命令执行成功无变化的颜色
黄色:命令执行成功有变化的颜色
红色:命令执行失败,报错msg
粉色:warning 警告一般无需处理

ansible查看模块帮助

1
2
3
4
5
# 查看指定模块的帮助内容
ansible-doc 模块

# 搜索关键字
/EXAMPLES

ad-hoc常用模块

1
2
3
4
5
6
7
8
9
10
11
12
13
command             # 执行shell命令(不支持管道等特殊字符)
shell # 执行shell命令(支持管道等特殊字符)
scripts # 执行shell脚本
yum_repository # 配置yum仓库
yum # 安装软件
copy # 变更配置文件
file # 建立目录或文件
service # 启动与停止服务
mount # 挂载设备
cron # 定时任务
get_url #下载软件
firewalld #防火墙
selinux #selinux

Ansible命令模块

command模块

1
2
3
# 默认模块, 执行命令
ansible web_group -m command -a 'ps -ef'
## command模块不识别特殊符号,例如:"|"

shell模块

1
2
3
# 如果需要一些管道操作,则使用shell
ansible web_group -m shell -a 'ps -ef'
## shell模块可以识别特殊符号,例如:"|"

script模块

1
2
3
# 编写脚本使用script模块
ansible web_group -m script -a '/root/touch.sh'
## script模块可以识别脚本

Ansible文件管理模块

file模块

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
# 模块动作
src # 指定软链接的源文件
dest # 指定软链接的目标文件
path # 指定文件路径
mode # 设置文件或目录权限
owner # 设置文件或目录属主信息
group # 设置文件或目录属组信息
recurse
-yes -true # 打开递归
-no -false # 不打开递归
state
-directory # 在远端创建目录
-touch # 在远端创建文件
-link # 软链接
-hard # 硬链接
-absent # 表示删除文件或目录

# 示例:
## 在web_group组创建/opt/test目录
ansible web_group -m file -a "path=/opt/test state=directory"

## /opt/test目录 属主www 属组www 权限777
ansible web_group -m file -a "path=/opt/test owner=www group=www mode=777"

## 创建/opt/abc的文件 属主www 属组www 权限777
ansible web_group -m file -a "path=/opt/abc owner=www group=www mode=777 state=touch"

## 创建/tmp/aaa/111/adc 目录 属主www 属组www 权限666
ansible web_group -m file -a "path=/tmp/aaa/111/adc recurse=true owner=www group=www mode=666 state=directory "

copy模块

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
# 统一配置文件
src # 推送数据的源文件路径
dest # 推送数据的目标路径
owner # 指定文件属主信息
group # 指定文件属组信息
mode # 指定文件权限信息
content # 指定内容写入文件(会覆盖原有数据)

backup
-yes -true # 备份
-no -false # 不备份
remote_src
-yes # 操作远端文件
-no # 不操作远端文件

# 示例:
## 将配置文件下发
ansible web_group -m copy -a "src=/root/www.xxx.conf dest=/opt"

## 配置文件更改后下发,备份(保留原文件,并自动标记时间信息备份)
ansible web_group -m copy -a "src=/root/www.xxx.conf dest=/opt backup=true"

## 操作被控端的文件
ansible web_group -m copy -a "src=/tmp/abc dest=/root remote_src=true"

## 直接向远端文件内写入数据信息,并且会覆盖远端文件内原有数据信息
ansible web_group -m copy -a "content='zls' dest=/tmp/zls.txt"

get_url模块

1
2
3
4
5
6
7
8
9
10
# 指定url进行下载
url # 指定下载地址
dest # 指定下载的目录
mode # 指定权限
checksum # 校验加密算法
md5
sha256

# 示例:
ansible web_group -m get_url -a "url=http://test.driverzeng.com/Nginx_Code/QQ2.8.zip dest=/tmp mode=644"

Ansible软件管理模块

yum

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# yum软件安装模块
exclude=kernel*,foo* # 排除某些包
list=ansible # 类似于yum list查看是否可以安装
disablerepo="epel,ol7_latest" # 禁用指定的yum仓库
download_only=true # 只下载不安装 yum install d
name
-file:// # 指定本地安装路径(yum localinstall 本地rpm包)
-http:// # 指定yum源(从指定url安装)
-httpd # 指定要安装的软件包名称(yum仓库安装)
state
-installed -present # 安装软件包
-removed -absent # 移除软件包
-latest # 安装最新软件包

# 示例:
ansible web_group -m yum -a "name=httpd state=present"

yum repository

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
# 管理yum源模块
name # 指定仓库名,如果没有file则为仓库文件名
description # 仓库的描述信息
baseurl # 指定yum源的url地址
file # 如果没有指定filename文件名就是和name指定的一致
name # 指定的内容
owner # 指定属主
group # 指定属组
mode # 指定权限
gpgkey # 指定密钥
gpgcheck
-yes # 检查秘钥
-no # 不检查秘钥
enabled
-yes # 启用仓库
-no # 不启用仓库
state
-persent # 创建仓库
-absent # 删除仓库

# 示例:
## 添加yum仓库
ansible web_group -m yum_repository -a "name=zls_epel description=EPEL baseurl=https://download.fedoraproject.org/pub/epel/$releasever/$basearch/" -i ./hosts

## 仓库名和配置文件名不同
ansible web_group -m yum_repository -a 'name=zls_epel description=EPEL file=test_zls baseurl=https://download.fedoraproject.org/pub/base/$releasever/$basearch/ gpgcheck=no' -i ./hosts

## 删除yum仓库及文件
ansible web_group -m yum_repository -a 'name=zls_epel file=test_zls state=absent' -i ./hosts

#添加mirrorlist ??
ansible web_group -m yum_repository -a 'name=zls_epel description=EPEL file=test_zls baseurl=https://download.fedoraproject.org/pub/base/$releasever/$basearch/ gpgcheck=no mirrorlist=http://mirrorlist.repoforge.org/el7/mirrors-rpmforge enabled=no' -i ./hosts

service、systemd模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 管理服务启动
name # 指定要启动服务的名字
state
-started # 启动服务
-stopped # 停止服务
-restarted # 重启服务
-reloaded # 重载服务
enabled
-yes -true # 开启开机自启
-no -false # 关闭开机自启

# 示例:
## 开启nginx并加入开机自启
ansible web_group -m service -a 'name=nginx state=started enabled=true'

#停止crond并删除开机自启
ansible web_group -m service -a "name=crond state=stoped enabled=no"

Ansible用户管理模块

user模块

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 创建用户
name # 指定用户名
comment # 指定用户描述信息
uid # 指定用户的uid
group # 指定用户组gid
shell # 指定用户登录shell
append # 追加附加组(指定附加组后使用)
groups # 指定附加组名称
password # 给用户添加密码(单引号)
ssh_key_bits: 2048 # 创建用户的时候,创建秘钥 私钥的位数是2048位
ssh_key_file: .ssh/id_rsa # 指定私钥的位置
state
-absent # 删除用户
-present # 创建用户
remove
-yes # 删除用户和用户相关的文件
-no # 不删除
create_home
-yes # 创建家目录
-no # 不创建家目录

# 示例:
## 创建www用户静止登录,不创建家目录
ansible web_group -m user -a 'name=www shell=/sbin/nologin create_home=false'

## 创建用户指定uid和gid,不创建家目录也不允许登陆
ansible web_group -m user -a "name=zls uid=888 group=888 shell=/sbin/nologin create_home=false"

## 创建用户并生成秘钥对
ansible web_group -m user -a "name=zls uid=888 group=root shell=/bin/bash generate_ssh_key=yes ssh_key_bits=2048 ssh_key_file=.ssh/id_rsa" -i ./hosts
web01 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 0,
"home": "/home/zls",
"name": "zls",
"shell": "/bin/bash",
"ssh_fingerprint": "2048 SHA256:WEMHCpSjxxqFwlzrCk1FqrPqeq6N/SHxL1gFTSqHlGM ansible-generated on web01 (RSA)",
"ssh_key_file": "/home/zls/.ssh/id_rsa",
"ssh_public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDRx+bCYGh4FqpKoPzyXrR8ef9GwoY6l6QEFQ0+XPynR22fd9Lbs1eUxWDm5aH4ZO8sPaI8a5xmj88Sipwl0FxlQTjD2X/vreZNEDbwFWrbZ24VvPkfPSSWBh5SxLH6pJt8pGQpPVWuLRMx6yOOxRB1hh9bGFzQNg5z8xqzeogTOoI7cxSFZVuUb5affNj8H5mCw2nAvblV+HNhRzbMlwr+9/EWcCWHDnlVYcELHXjpNJcyGB3VFOu1MPkmLaSTcaB73O0eRvZQkYMBePKJC44tvjHihGhvCk9rzh8qvzHxvMgoMD/+0uKAlIwEvOyfAczb7fxllU0rDtbyPtjbuLsR ansible-generated on web01",
"state": "present",
"system": false,
"uid": 888
}
web02 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 0,
"home": "/home/zls",
"name": "zls",
"shell": "/bin/bash",
"ssh_fingerprint": "2048 SHA256:IepfOosi2Xm8kfr4nOPAhG3fec6o8kpMnJ0/RwN+0F8 ansible-generated on web02 (RSA)",
"ssh_key_file": "/home/zls/.ssh/id_rsa",
"ssh_public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDEcO9iDKg4X8ya/y9E0eDelAFMp/rxiDSzW31r+REawaQyF4oywcdIagpz0MTg2BeF2WdaYUmHmtmSTfSOMif26+R1FLcL9f9NYu3io/0388jukcTfyN02diXWgqoKtt4Gbm8Bq8sWE4tX/FSYl42fG6bX1AyDSMzzB7ERr2AD/Y9KuKt7cEXDinGjqTFEXw6+x1wBHpotkUisYiZCci+1Nx4YSznVRBveZTlpxMUYmKgwkUXQIt+RoOYzjgD++0md8O7lwJGgODZkahlrf2pOQnmpS4isLi9or4N+DVnqD+cXb/RjgJzPIJZYazgRY3vtAU9DDqm5i049x/VxEqFj ansible-generated on web02",
"state": "present",
"system": false,
"uid": 888
}

## 将明文密码进行hash加密,然后进行用户创建
ansible web_group -m debug -a "msg={{ 'zls' | password_hash('sha512', 'salt') }}" -i ./hosts
web01 | SUCCESS => {
"msg": "$6$salt$gaWhNcZweYlKQcLU1CqyY/UbYqIeUffVz6ESj87aMNfMX.xYBx0Z.67wzLN/hkkxmNut7SvkksPZ2Zlrse98m/"
}
web02 | SUCCESS => {
"msg": "$6$salt$gaWhNcZweYlKQcLU1CqyY/UbYqIeUffVz6ESj87aMNfMX.xYBx0Z.67wzLN/hkkxmNut7SvkksPZ2Zlrse98m/"
}

## 创建用户
ansible web_group -m user -a 'name=zls1 password=$6$salt$gaWhNcZweYlKQcLU1CqyY/UbYqIeUffVz6ESj87aMNfMX.xYBx0Z.67wzLN/hkkxmNut7SvkksPZ2Zlrse98m/ create_home=true shell=/bin/bash' -i ./hosts

group模块

1
2
3
4
5
6
7
8
9
10
11
12
13
# 创建组
name # 指定组名
gid # 指定组gid
state
-present # 创建组 groupadd
-absent # 删除组 groupdel

# 示例:
## 创建一个www组 gid是666
ansible all -m group -a 'name=www gid=666'

## 创建一个www组
ansible all -m user -a 'name=www uid=666 group=666 shell=/sbin/nologin create_home=false'

ansible定时任务模块

cron模块

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
# 管理定时任务
* * * * * /usr/bin/ntpdata time1.aliyun.com&>/dev/null
00 05 * * * /usr/bin/ntpdata time1.aliyun.com&>/dev/null
name # 定时任务的描述信息
minute # 分
hour # 时
day # 日
month # 月
weekday # 周
job # 执行的任务
state
-present # 创建定时任务 默认
-absent # 删除定时任务

# 示例:
## 使用ansible添加一条定时任务
ansible web_group -m cron -a "minute=* hour=* day=* month=* weekday=* job='/bin/sh /server/scripts/test.sh'"
ansible web_group -m cron -a "job='/bin/sh /server/scripts/test.sh'"
ansible web01 -m cron -a 'name="time_rsyn" minute=00 hour=05 job="/usr/bin/ntpdata time1.aliyun.com&>/dev/null" state=absent'

## 设置定时任务注释信息,防止重复,name设定
ansible web_group -m cron -a "name='cron01' job='/bin/sh /server/scripts/test.sh'"

## 删除相应定时任务
ansible web_group -m cron -a "name='ansible cron02' minute=0 hour=0 job='/bin/sh /server/scripts/test.sh' state=absent"

## 注释相应定时任务,使定时任务失效
ansible web_group -m cron -a "name='ansible cron01' minute=0 hour=0 job='/bin/sh /server/scripts/test.sh' disabled=no"

Ansible磁盘挂载模块

mount模块

1
2
3
4
5
6
7
8
9
10
11
12
13
# 管理磁盘挂载
path # 需挂载的路径
src # 挂载到目标路径
fstype # 文件类型 -t nfs
stata
-present # 开机挂载,仅将挂载配置写入/etc/fstab
-mounted # 挂载设备,并将配置写入/etc/fstab
-umounted # 卸载设备,不会清除/etc/fstab写入的配置
-absent # 卸载设备,会清理/etc/fstab写入的配置

# 示例:
## 挂载远端磁盘
ansible web01 -m mount -a 'path=/code/wordpress/wp-content/uploads src=172.16.1.31:/data fstype=nfs state=mounted'

Ansible解压缩模块

unarchive模块

1
2
3
4
5
6
7
8
9
# 解压缩
src # 指定压缩包的路径
dest # 指定解压的路径
owner # 属主
group # 属组
mode # 权限
remote_src
-yes # 压缩包在远端
-no # 压缩包在本地

ansible数据库模块

mysql_user模块

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
# 管理mysql用户
name # 指定用户名
password # 指定密码 123
host # 指定允许连接的IP主机
priv # 指定用户的数据库权限
login_user # 指定登录的用户
login_password # 指定登录的密码
state
-present # 创建用户
-absent # 删除用户
MySQL-python # 使用playbook安装数据库需要装此此程序

# 示例:
## 安装Python连接数据库并创建用户
- hosts: mysql_group
tasks:
- name: 安装Python连接MySQL库
yum:
name: MySQL-python
state: present
- name: 创建mysql test用户
mysql_user:
login_user: root
login_password: '123'
name: test
password: '123'
host: '%'
priv: '*.*:ALL'
state: present

## 解决mysql的反向解析,不让IP解析成主机名
vim /etc/my.cnf
[mysqld]
skip_name_resolve # 添加此修改
systemctl restart mariadb # 重启mariadb服务

mysql_db模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 管理mysql库
create database wordpress;
mysqldump -uroot -p123 -Bwordpress > /tmp/wp.sql

## 动作
name # 指定库名
target # 导出数据库(sql文件)存放的路径
src # 导出的数据库
login_user # 指定登录的用户
login_password # 指定登录的密码
state
- present # 创建库
- absent # 删除库
- dump # 导出数据
- import # 导入数据