目录
fetch
注意:此功能是从其他远程主机下载到本地
参数
| 参数 | 说明 |
|---|---|
| dest | 远程机器中本地路径(仅能指定文件名),如果dest以/结尾,它将使用源文件的基础名称 |
| src | 远程机器的远程路径(禁止指定目录) |
| flat | 仅传输文件 |
示例
传输文件(递归创建目录)
ansible test -m fetch -a "dest=~/ src=/etc/hosts"
ansible test -m shell -a "tree ~/"
仅传输文件
#递归删除
ansible test -m file -a "dest=~/192.168.255.101/ state=absent"
ansible test -m file -a "dest=~/192.168.255.102/ state=absent"
ansible test -m file -a "dest=~/192.168.255.103/ state=absent"
ansible test -m file -a "dest=~/192.168.255.104/ state=absent"
ansible test -m file -a "dest=~/192.168.255.105/ state=absent"
#下载远程机器上的文件
ansible test -m fetch -a "dest=~/ src=/etc/hosts flat=yes"
ansible test -m shell -a "ls ~/"
flat设置时,会覆盖同名文件
cron
参数
| 参数 | 说明 |
|---|---|
| minute | 分钟 |
| hour | 小时 |
| day | 天 |
| mouth | 月 |
| week | 周 |
| job | 任务 |
| name | 描述 |
| disabled | 注释 |
| user | 用户 |
| reboot | 重启时运行(不推荐) |
| special_time | 指定时间 |
state状态参数
| 参数 | 说明 |
|---|---|
| absent | 删除定时任务 |
| present | 创建定时任务(默认) |
示例
提示:普通用户开启crontab的两种方式(二选一),需要root权限,
#白名单方式
ansible test -m shell -a "echo 'centos' >>/etc/cron.allow"
#
#黑名单方式
#删除白名单
ansible test -m file -a "dest=/etc/cron.allow state=absent"
#添加黑名单
ansible test -m shell -a "echo 'anonymous' >> /etc/cron.allow"
创建定时任务
ansible test -m cron -a 'minute=*/1 name="date_time" job="echo `/usr/bin/date` >> ~/date.txt" '
ansible test -m shell -a "crontab -l"
ansible test -m shell -a "cat ~/date.txt"
删除定时任务
ansible test -m cron -a 'name="date_time" state=absent'
ansible test -m shell -a "crontab -l"
yum
参数
| 参数 | 说明 |
|---|---|
| name | 软件名称 |
| update_cache | 更新缓存 |
| conf_file | 指定使用的配置文件 |
| disable_pgp_check | 软件校验 |
| disablerepo | 临时禁止repo |
| enablerepo | 临时启用repo |
state
|参数|说明|
|present|安装|
|absent|卸载|
|latest|安装最新版本|
示例
注意:安装软件需要root权限
安装nginx
ansible test -m yum -a "name=nginx state=latest"
ansible test -m shell -a "which nginx; nginx -v"
卸载nginx
ansible test -m yum -a "name=nginx state=absent"
ansible test -m shell -a "which nginx; nginx -v"
mount
参数
| 参数 | 说明 |
|---|---|
| fstype | 文件类型 |
| ops | 选项 |
| path | 挂载点 |
| src | 被挂载的 |
state
| 参数 | 说明 |
|---|---|
| unmounted | 加载/etc/fstab文件,卸载 |
| absent | 在fstab中删除挂载信息 |
| present | 在fstab中添加挂载信息 |
| mounted | 1.将挂载的信息添加到/etc/fstab中,2加载配置文件并挂载 |
示例
先添加磁盘,在虚拟机添加即可,此步骤略过
注意:需要root权限
#重启
reboot
#查看磁盘信息
fdisk -l
#格式化磁盘
fdisk /dev/sdb
#添加分区,键入n
n
#选择主分区,键入p
p
#选择分区号,键入1
1
#是为EFI启动预留的,而Fdisk也随着这波的兴起而跟着变,键入2048,起始大小
2048
#最终大小,键入回车就好
#键入w,保存之前的配置
w
#查看磁盘信息
fdisk -l
#格式化分区 -t 分区类型
mkfs -t ext4 /dev/sdb1
挂载分区
ansible 192.168.255.101 -m mount -a "fstype=ext4 opts=rw path=/mnt src=/dev/sdb1 state=mounted"
ansible 192.168.255.101 -m shell -a "df -h"
卸载分区
ansible 192.168.255.101 -m mount -a "fstype=ext4 path=/mnt state=unmounted"
ansible 192.168.255.101 -m shell -a "df -h"
service
参数
| 参数 | 说明 |
|---|---|
| arguments | 额外参数 |
| enabled | 开机启动 |
| name | 服务名称 |
| runlevel | 开机启动级别 |
| sleep | 等待时间 |
state参数
| 参数 | 说明 |
|---|---|
| started | 启动服务 |
| stopped | 停止服务 |
| restarted | 重启服务 |
| reload | 重新加载服务配置文件 |
示例
启动服务
ansible test -m yum -a "name=nginx stated=present"
ansible test -m service -a "name=nginx state=started enabled=true"
ansible test -m shell -a "ss -lnatup | grep nginx"
关闭服务
ansible test -m service -a "name=nginx state=stopped"
ansible test -m shell -a "ss -lnatup | grep nginx"
user
管理用户,需要root权限
参数
| 参数 | 解释 |
|---|---|
| comment | 用户描述信息 |
| createhome | 创建用户家目录 |
| force | 在使用state=absent时,行为与userdel -force一致 |
| group | 指定组 |
| groups | 附属组 |
| home | 指定家目录 |
| move_home | 如果设置为home=时,试图将用户主目录移动到指定目录 |
| name | 指定用户名 |
| non_unique | 允许选项修改非唯一的用户ID值 |
| password | 指定用户密码 |
| remove | 在使用state=absent时,行为与userdel -remove一致 |
| shell | 指定默认交互解释器 |
| state | 设置用户状态,absent为删除 |
| system | 设置用户为系统用户 |
| uid | 设置用户id |
示例
创建用户
ansible test -m user -a "name=test state=present id=10086"
ansible test -m shell -a "id 10086"
删除用户
ansible test -m user -a "name=test id=10086 state=absent"
ansible test -m shell -a "id 10086"
group
参数
| 参数 | 说明 |
|---|---|
| gid | 设置组的id号 |
| name | 设定组名称 |
| state | 状态值,present创建,absent删除 |
| system | yes为系统组 |
示例
创建用户组
ansible test -m group -a "name=test gid=10086 state=present"
ansible test -m shell -a "grep 10086 /etc/group"
删除用户组
ansible test -m group -a "name=test gid=10086 state=absent"
ansible test -m shell -a "grep 10086 /etc/group"
script
本地脚本远程执行
示例
添加脚本
cat >> /home/centos/sh.sh<<EOF
#!/bin/bash
echo ====`date +"%Y-%m-%d %T"`===== >>a.txt
cat /proc/meminfo | head >> a.txt
EOF
远程执行命令
ansible test -m script -a "~/sh.sh"
ansible test -m shell -a "cat ~/a.txt"
setup
通过facts组件收集信息,返回json信息
示例
获取内存信息
ansible test -m setup -a 'filter="*mem*"'
保存信息
ansible test -m setup -a 'filter="*mem*"' --tree ~/
#通过python内置json工具展示信息
find ./ -name "192.*" -exec python -m json.tool {} \;
浙公网安备 33010602011771号