0
点赞
收藏
分享

微信扫一扫

Ansible常用模块

Ansible执行任务

  • Ad-hoc
  • playbook

Ansible常用模块_centos

# ad-hoc语法:

ansible 主机 -m 模块 -a 动作

Ad-hoc结果返回颜色

  • 绿色:命令执行成功且无变化的颜色
  • 黄色:命令执行成功,但是有变化(有更改)
  • 红色:命令执行失败,报错(看msg:)
  • 粉丝|紫色:warning,警告一般无需处理

Ansible查看帮助

## ansible-doc 模块名
## 找到帮助信息中的:EXAMPLES

Ad-hoc常用模块

command模块、shell模块

[root@m01 <sub>]# ansible web -m shell -a 'df -h'
[root@m01 </sub>]# ansible web -m command -a 'df -h'

注意:command模块不支持特殊符号

script模块

# 执行脚本
[root@m01 <sub>]# ansible-doc script

## 执行本机/root目录下的test.sh脚本
[root@m01 </sub>]# ansible web -m script -a '/root/test.sh'

优势:无需将脚本放在其他的机器上

Ansible文件管理模块

file模块

## file模块动作
src:指定软链接的源文件
dest:指定软链接的目标文件
path:指定文件路径
owner:指定文件属主
group:指定文件属组
mode:指定文件权限
recurse:递归
state:
- touch 创建文件
- absent 删除
- directory 创建目录
- link 软链接
- hard 硬链接

## 在/opt目录下创建test目录
[root@m01 <sub>]# ansible web -m file -a 'path=/opt/test state=directory'

## 在/opt目录下创建efg目录属主www 属组www 权限200
[root@m01 </sub>]# ansible web -m file -a 'path=/opt/efg owner=www group=www mode=200
state=directory'

## 在/opt目录下创建文件abc 属主www 属组www 权限200
[root@m01 <sub>]# ansible web -m file -a 'path=/opt/abc owner=www group=www mode=200
state=touch'

## 创建软链接文件
[root@m01 </sub>]# ansible web -m file -a 'path=/usr/opt_test_link state=absent'

copy模块 { template (推变量文件)}

# 作用:统一配置管理
# template 和 copy 动作一样
# 动作
src: 指定源文件的路径
dest: 指定目标路径
owner: 指定属主
group: 指定属组
mode: 指定权限
backup:备份
- yes/true
- no/false 默认
remote_src:远端的源文件
- yes/true
- no/false
content:指定内容写入文件

## 将管理端的配置文件下发到被管理端
[root@m01 <sub>]# ansible web -m copy -a 'src=/root/wenjian dest=/opt'

## 使用backup做备份
[root@m01 </sub>]# ansible web -m copy -a 'src=/root/wenjian dest=/opt backup=yes'

## 指定属主和属组
[root@m01 <sub>]# ansible web -m copy -a 'src=/root/wenjian dest=/opt owner=www group=www'

## remote_src指定源文件在远端
[root@m01 </sub>]# ansible web -m copy -a 'src=/opt/wenjian dest=/root remote_src=yes'

## content往指定文件中写入内容
[root@m01 ~]# ansible web -m copy -a 'dest=/opt/rsync.txt content="jin:123456"'

get_url

## wget 下载文件
## 查看帮助
[root@m01 <sub>]# ansible-doc get_url

## 动作
url:下载网址
dest:下载路径
mode:指定权限

## 下载wordpress
[root@m01 </sub>]# ansible web -m get_url -a 'url="http://test.driverzeng.com/Nginx_Code/QQ2.8.zip" dest=/opt'

wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

## 下载阿里云base源
[root@m01 ~]# ansible web -m get_url -a 'url="https://mirrors.aliyun.com/repo/Centos-7.repo" dest=/opt'

Ansible软件管理模块

yum模块

## 作用:安装rpm包
## 查看帮助文档
[root@m01 <sub>]# ansible-doc yum

## 动作
name:指定安装包的名字
- http:// 从指定的url下载 yum install -y http://网址
- file:// 从本地rpm包安装 yum localinstall
- 包名 从yum仓库中下载 yum install -y 包名
state:
- absent/removed:卸载 yum remove
- present/installed:安装 yum install 默认
- latest:安装最新版本
download_only:只下载不安装
## 安装nginx和mysql客户端
root@m01 </sub>]# ansible web -m yum -a 'name=nginx,mariadb'

yum_repository模块

## 管理yum源,yum仓库
[root@m01 <sub>]# ansible-doc yum_repository

## yum源配置文件
[base]
name=xxxx
baseurl=http://xxx
gpgcheck=0
gpgkey=file://xxx
enable=1

## 动作:
name:仓库的名字[base]
description:仓库的描述信息 name=xxxx
baseurl:仓库的url地址 baseurl=http://xxx
file:如果没有指定file则文件名和name指定的一致,如果指定了file,文件名为file指定的内容,仓库名为 name指定的内容
owner:指定属主
group:指定属组
mode:指定权限
gpgcheck:秘钥对检测
- yes/True gpgcheck=1
- no/False gpgcheck=0
enabled:是否开启仓库
- yes/True enable=1
- no/False enable=0
state:
- present:创建仓库
- absent:删除仓库

## 创建nginx官方的yum源
[root@m01 </sub>]# ansible web -m yum_repository -a 'name=nginx-stable description="nginx stable repo" baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=false enabled=true'

## 结果
[root@web01 opt]# vim /etc/yum.repos.d/nginx-stable.repo
[nginx-stable]
baseurl = http://nginx.org/packages/centos/$releasever/$basearch/
enabled = 1
gpgcheck = 0
name = nginx stable repo

## 使用file指定yum源的文件名
[root@m01 <sub>]# ansible web -m yum_repository -a 'name=nginx-stable description="nginx stable repo" baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=false enabled=true file=nginx'

## 删除仓库
[root@m01 </sub>]# ansible web -m yum_repository -a 'name=nginx-stable file=nginx state=absent'

## [root@m01 <sub>]# ansible web -m yum_repository -a 'name=nginx-stable file=nginx state=absent'
[root@m01 </sub>]# ansible web -m yum_repository -a 'name=nginx-mainline description="nginx stable repo" baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=false enabled=true file=nginx'

[root@web01 yum.repos.d]# cat nginx.repo
[nginx-stable]
baseurl = http://nginx.org/packages/centos/$releasever/$basearch/
enabled = 1
gpgcheck = 0
name = nginx stable repo

[nginx-mainline]
baseurl = http://nginx.org/packages/centos/$releasever/$basearch/
enabled = 1
gpgcheck = 0
name = nginx stable repo

## 删除指定的仓库名
[root@m01 ~]# ansible web -m yum_repository -a 'name=nginx-mainline file=nginx state=absent'
[root@web01 yum.repos.d]# cat nginx.repo
[nginx-stable]
baseurl = http://nginx.org/packages/centos/$releasever/$basearch/
enabled = 1
gpgcheck = 0
name = nginx stable repo

Ansible服务管理模块

service、systemd模块

## 管理服务启停
[root@m01 <sub>]# ansible-doc service

## 动作:
name:指定服务名字
state:
- started 开启服务
- reloaded 重新加载服务
- stopped 停止服务
- restarted 重启服务
enabled:开机自启
- yes/True 加入开机自启
- no/False 不加入开机自启 默认

## 启动nginx并加入开机自启
[root@m01 </sub>]# ansible web -m service -a 'name=nginx state=started enabled=yes'

Ansible用户管理模块

user模块

## 创建用户
[root@m01 <sub>]# ansible-doc user
useradd www -u 666 -g 666 -s /sbin/nologin -M
-c:描述信息

## 动作:
name:用户名
comment:-c指定用户描述信息
uid:-u指定用户的uid
group: -g指定用户的组 gid
shell: -s指定用户登录的shell -s /sbin/nologin
append:-a 指定附加组并追加附加组
groups:-G指定用户附加组
state:
- absent 删除用户 userdel
- present 创建用户 useradd 默认
remove:
- yes/True userdel -r 删除用户和用户相关的文件
- no/False 默认
ssh_key_bits:创建用户时,创建私钥,私钥的位数 2048
ssh_key_file:指定私钥的位置
create_home:
- yes/True 创建用户同时创建家目录 默认
- no/False 创建用户不创建家目录

## 创建www用户禁止登陆,不创建家目录
[root@m01 </sub>]# ansible web -m user -a 'name=www uid=666 group=666 shell=/sbin/nologin create_home=no'

group模块

## 管理用户组
[root@m01 <sub>]# ansible-doc group

## 动作:
name:指定组名字
gid:指定组id
state:
- present 创建组 groupadd 默认
- absent 删除组 groupdel

## 创建一个www组并且gid是666
[root@m01 </sub>]# ansible web -m group -a 'name=www gid=666'

Ansible定时任务模块

cron模块

## 管理定时任务
[root@m01 <sub>]# ansible-doc cron
00 04 * * * /usr/bin/ntpdate time1.aliyun.com &>/dev/null

## 动作:
name:定时任务注释信息
minute:分 00
hour:时 04
day:日
month:月
weekday:周
job:执行的任务 /bin/ls
state:
- present 创建定时任务 默认
- absent 删除定时任务

## 创建时间同步定时任务
[root@m01 </sub>]# ansible web -m cron -a 'name="time sync" minute=00 hour=04 job="/usr/bin/ntpdate time1.aliyun.com &>/dev/null" state=present'

## 删除定时任务
[root@m01 ~]# ansible web -m cron -a 'name="time sync" state=absent'

Ansible磁盘挂载模块

mount模块

## 管理磁盘挂载
[root@m01 <sub>]# ansible-doc mount

mount -t nfs 172.16.1.31:/data /code/wordpress/wp-content/uploads

## 动作:
path:挂载路径 /code/wordpress/wp-content/uploads
src:挂载源 172.16.1.31:/data
fstype:文件类型 -t nfs
state:
- present:只将挂载信息记录在/etc/fstab中(开机挂载)
- mounted:立刻挂载,并将配置写入/etc/fstab中
- unmounted:卸载设备,但是不会清除/etc/fstab中的内容
- absent:卸载设备,并清除/etc/fstab中的内容

挂载:mounted
卸载:absent

mount -o rw,remount /
opts: 指定挂载路径是否可读可写 rw,remount

## 挂载nfs
[root@m01 </sub>]# ansible web -m mount -a 'path=/code/wordpress/wp-content/uploads src=172.16.1.31:/data fstype=nfs state=mounted'

Ansible数据库模块

mysql_user

## 作用:管理mysql用户
[root@m01 ~]# ansible-doc mysql_user
mysql -uroot -p123
grant all on *.* to wp_user@'%' identified by '123';

## 动作
name:指定用户名 wp_user
host:指定允许连接的IP主机 %
password:指定密码 123
priv:指定权限 '*.*:ALL'
login_user:MySQL登录的用户 root
login_password:MySQL登录用户root的密码 123
state:
- present 创建
- absent 删除

## MySQL 跳过反向解析,不让IP地址解析成主机名
vim /etc/my.cnf
[mysqld]
skip_name_resolve
systemctl restart mariadb

mysql_db

# 管理mysql库
[root@m01 ~]# ansible-doc mysql_db
### 创建数据库
create database wordpress;
### 导出wordpress库数据
mysqldump -uroot -p123 -B wordpress > /tmp/wordpress.sql

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


举报

相关推荐

0 条评论