学习b记 · 第二阶段
十四、Ansible_ ad-hoc
1、什么是ad-hoc?
ad-hoc简而言之就是“临时命令”,执行完即结束,并不会保存
2、ad-hoc模式的使用场景
比如在多台机器上查看某个进程是否启动,或拷贝指定文件到本地,等等
3、ad-hoc结果返回颜色
绿色: 代表被管理端主机没有被修改
黄色: 代表被管理端主机发现变更
红色: 代表出现了故障,注意查看提示
4、ad-hoc常用模块
command # 执行shell命令(不支持管道等特殊字符)
shell # 执行shell命令
scripts # 执行shell脚本
yum_repository # 配置yum仓库
yum # 安装软件
copy # 变更配置文件
file # 建立目录或文件
service # 启动与停止服务
mount # 挂载设备
cron # 定时任务
get_url #下载软件
firewalld #防火墙
selinux #selinux
5、Ansible-doc帮助手册
[root@m01 ~]# ansible-doc -l # 查看所有模块说明
[root@m01 ~]# ansible-doc copy # 查看指定模块方法
[root@m01 ~]# ansible-doc -s copy # 查看指定模块参数
#环境就是上一篇的东西
#清单↓先做好免密
[webs]
172.16.1.202
172.16.1.204
[nfs]
172.16.1.203
1、command # 执行shell命令(不支持管道等特殊字符)
[root@m01 ansible]# ansible nfs -m command -a 'df -h'
[root@m01 /]# ansible webs -m command -a 'free -h'
2、shell # 执行shell命令 支持管道
[root@m01 /]# ansible webs -m shell -a 'ps -aux | grep nginx'
[root@m01 /]# ansible webs -m shell -a 'cat /etc/passwd | grep root'
3、yum_repository # 配置yum仓库
......
4、yum # 安装软件
name 指定软件的名称
例如: name=httpd 安装httpd
state:
present # 安装动作
absent # 表示卸载lrzsz
[root@m01 ansible]# ansible webs -m yum -a 'name=httpd state=present'
[root@m01 ansible]# ansible webs -m shell -a 'rpm -qa httpd'
172.16.1.202 | CHANGED | rc=0 >>
httpd-2.4.6-99.el7.centos.1.x86_64
172.16.1.204 | CHANGED | rc=0 >>
httpd-2.4.6-99.el7.centos.1.x86_64
[root@m01 ansible]# ansible webs -m yum -a 'name=httpd state=absent'
[root@m01 ansible]# ansible webs -m shell -a 'rpm -qa httpd'
172.16.1.202 | CHANGED | rc=0 >>
172.16.1.204 | CHANGED | rc=0 >>
5、copy # 变更配置文件
src: 源文件
dest: 目标目录/文件
owner: 属主
group: 属组
mode: 权限
backup: yes #对目标文件进行备份默认以时间命名 复制过去的内容和已经存在的内容不一样就会把之前的备份
[root@m01 ansible]# ansible webs -m copy -a 'src=test.txt dest=/root/ mode=777'
6、file # 建立目录或文件
path: 目标文件/目录
owner: 属主
group: 属组
mode: 权限
state:
touch # 创建普通文件
directory # 创建目录
[root@m01 ansible]# ansible webs -m file -a 'path=/test mode=777 state=directory'
[root@m01 ansible]# ansible webs -m file -a 'path=/tmp/abc.txt mode=777 state=touch'
7、service/systemd # 启动与停止服务
name: # 服务的名称
state: # 动作
started # 启动
stopped # 停止
reloaded # 重新加载
restarted# 重新启动
enabled: yes # 开机自动运行
[root@m01 ansible]# ansible webs -m systemd -a 'name=firewalld state=started enabled=yes'
[root@m01 ansible]# ansible webs -m systemd -a 'name=firewalld state=stopped enabled=no'
8、mount # 挂载设备
src: 挂载的网络文件系统路径(172.16.1.203:/data)
path: # 挂载的路径 挂载点
fstype: # 文件类型
state:
present: # 只写入fstab开机自动挂载
unmounted: # 只删除fstab中自动挂载项
使用下面的参数:
mounted: # 挂载并写入开机自动挂载fstab
absent: # 卸载并删除开机自动运行
#这里用nfs做一个例子
1)创建用户、组
[root@m01 ansible]# ansible nfs -m group -a 'name=www gid=666'
[root@m01 ansible]# ansible nfs -m user -a 'name=www uid=666 group=www shell=/sbin/nologin create_home=false'
2)创建目录指定用户
[root@m01 ansible]# ansible nfs -m file -a 'path=/data state=directory owner=www group=www'
3)编写配置文件
[root@m01 ansible]# ansible nfs -m copy -a 'src=exports dest=/etc/'
4)启动
[root@m01 ansible]# ansible nfs -m systemd -a 'name=nfs state=started'
5)挂载
[root@m01 ansible]# ansible webs -m mount -a 'src=172.16.1.203:/data path=/mnt fstype=nfs state=mounted'
9、cron # 定时任务
name: "check dirs" # 注释描述
minute: "0" # 分钟
hour: "5,2" # 小时
job: "ls /" # 具体执行的命令
[root@m01 ansible]# ansible webs -m cron -a 'name=test! minute=*/5 job="echo aoligei"'
[root@m01 ansible]# ansible webs -m cron -a 'name=test! minute=*/5 job="echo aoligei" state=absent'
10、get_url #下载软件
url:#下载的地址
dest:#下载存储位置
[root@m01 ansible]# ansible webs -m get_url -a 'url=https://cn.wordpress.org/latest-zh_CN.tar.gz dest=/root/'
11、firewalld #防火墙
......
12、selinux #selinux
[root@m01 ansible]# ansible webs -m selinux -a 'policy=targeted state=enforcing'
[root@m01 ansible]# ansible webs -m selinux -a 'policy=targeted state=disabled'