安装ansible并配置
yum install ansible -y
配置文件/etc/ansible
ansible.cfg hosts roles yml
[root@gzz~66 ansible]# egrep -v "(^#|^$)" ansible.cfg | head -6
[defaults]
inventory = /etc/ansible/hosts
remote_port = 22
host_key_checking = False
log_path = /var/log/ansible.log
module_name = command
[root@gzz~66 ansible]# egrep -v "(^#|^$)" hosts
[test]
192.168.129.166 nginx_port=10087 name=gz~166
192.168.129.199 nginx_port=10088 name=gz~199
192.168.129.99 nginx_port=10089 name=gz~99
设置公钥
[root@gzz~66 ansible]# ssh-keygen
[root@gzz~66 ansible]# ssh-copy-id root@192.168.129.166
[root@gzz~66 ansible]# ssh-copy-id root@192.168.129.199
[root@gzz~66 ansible]# ssh-copy-id root@192.168.129.99
测试
[root@gzz~66 ansible]# ansible test -m ping
192.168.129.166 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.129.99 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.129.199 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
ansible命令语法
命令格式 | ansible | test | -m | command | -a | "ls" |
---|
格式说明 | 命令 | 集群或者主机名称 | 指定模块 | 模块名称 | 模块动作 | 具体命令 |
--version | 显示版本 |
---|
-m | 指定模块 |
-v | 显示过程,最多四个v,v越多越详细 |
--list | 显示主机列表 |
-k | 输入ssh连接密码(建议配一个公钥) |
-C --check | 检查语法,不执行命令 |
-u | 指定远程操作执行的用户 |
-b | 代替旧版的sudo切换,默认为root |
-K | 提示输入sudo时的口令 |
常用 | |
---|
ansible all --list | 列出所有主机 |
ansible-doc -s 模块名 | 查看模块用法 |
主机名匹配支持正则及逻辑运算符 | |
all | 所有主机都匹配 |
* | 通配符 |
: | 或关系 |
:& | 与关系 |
:! | 非关系 |
使用非和综合逻辑时,'主机(集群)要用单引号括起来' | "其他情况使用双引号括起来" |
ping模块
通常用来检测网络是否连通
ansible test -m ping
command模块
可以使用大多数命令,但是不支持特殊符号,管道符什么的
ansible test -m command -a "touch 2022.txt"
ansible test -m command -a "sed -i '42s/80/90/g' /tmp/httpd.conf"
shell模块
和command模块相似,但是shell模块支持特殊符号
ansible test -m shell -a "netstat -tanulp| grep 22"
ansible test -m shell -a "echo 'hello' > /tmp/httpd.conf"
ansible test -m shell -a "echo 'world' >> /tmp/httpd.conf"
ansible test -m shell -a "echo 'nameserver 114.114.114.114' >> /etc/resolv.conf"
script模块
在被控机上执行本地的脚本
ansible test -m script -a "/tmp/a.sh"
执行本地的脚本
在99的/tmp下创建了一个a.sh
[root@gzz~66 tmp]# ansible test -m script -a "creates=/tmp/a.sh /tmp/a.sh"
192.168.129.99 | SKIPPED
判断被控机本地文件或者目录是否存在,如果存在就跳过,如果不存在,就执行
copy模块
从主控端复制文件到被控端
默认直接覆盖
ansible test -m copy -a "src=/tmp/66.txt dest=/tmp owner=nebula group=nebula mode=777"
可不加owner,group,mode
指定内容直接生成目标文件
ansible test -m copy -a "content='happy' dest=/tmp/8.txt"
fetch模块
与copy模块相反,将被控机的文件拉到主控机上
ansible test -m fetch -a "src=/tmp/8.txt dest=/tmp"
存放到dest:/tmp下的以ip命名的文件夹
file模块
在被控机上创建文件和设置文件属性
创造(空文件)或者删除文件
ansible test -m file -a "path=/tmp/18.txt state=touch/absent"
创建文件是指定文件属性
ansible test -m file -a "path=/tmp/2023.txt owner=nebula group=nebula mode=755 state=touch/absent"
"
创建目录
ansible test -m file -a "path=/tmp/gaozhu state=directory owner=nebula group=nebula mode=777"
删除目录
ansible test -m file -a "path=/tmp/gaozhu state=absent"
递归修改
ansible test -m file -a "path=/tmp/gaozhu/ state=directory recurse=true owner=nebula group=nebula mode=777"
state=directory recurse=true
创建软链接
ansible test -m file -a "src=/tmp/kkk dest=/tmp/kkk-link state=link"
src和dest均是被控机上的文件路径
删除软链接
ansible test -m file -a "src=/tmp/kkk dest=/tmp/kkk-link state=absent"
链接文件被删除了,原文件还在
yum模块
删除安装软件
安装
ansible test -m yum -a "name=httpd state=present"
删除
ansible test -m yum -a "name=httpd state=absent"
安装最新版
ansible test -m yum -a "name=httpd state=latest"
cron模块
主要用来管理系统中的定时任务 | |
---|
name | 任务名称 |
cron_file | 替换客户端改用户的任务计划的文件 |
minute | 分 |
hour hourly | 时 |
day daily | 日 |
month monthly | 月 |
weekday weekly | 周 |
job | 计划执行的命令 |
backup | 是否备份之间的任务计划 |
user | 新建定时任务计划的用户 |
state | 指定任务计划present,absent |
reboot | 重启时 |
annuall | 每年 |
添加定时任务是名字不可以重复
ansible test -m cron -a "minute=15 name=touchfik job='touch /tmp/`date`.txt'"
删除定时任务
ansible test -m cron -a "name=touchfik state=absent"
删除名字为空的定时任务
ansible test -m cron -a "name=None state=absent"
注释定时任务
ansible test -m cron -a "minute=15 name=jkkk job='touch /tmp/`date`.txt'"
ansible test -m cron -a "minute=15 name=jkkk job='touch /tmp/`date`.txt' disabled=yes"
disabled=yes
取消注释
ansible test -m cron -a "minute=15 name=jkkk job='touch /tmp/`date`.txt' disabled=no"
disabled=no
通过ansible在自己任意一台主机上创建计划任务,任务名称为”crontab day test”,
任务每3天执行一次,于执行当天的1点1分开始执行,任务内容为输出 test 字符。
ansible test -m cron -a "minute=1 hour=1 day=*/3 name='crontab day test' job='echo test'"
service模块
enabled 开机启动 | yes|no |
---|
name | 服务名称 |
runlevel | 运行级别 |
state | |
started | 启动 |
stopped | 关闭 |
restarted | 重启 |
reloaded | 重新加载 |
实现远程管理服务
启动
ansible test -m service -a "name=httpd state=started"
关闭
ansible test -m service -a "name=httpd state=stopped"
实现开机自启动
ansible test -m service -a "name=httpd state=started enabled=yes"
ansible test -m shell -a "systemctl list-unit-files| grep httpd"
systemctl list-unit-files | grep httpd 通过此命令查看看机自启服务
systemd模块
也是远程管理服务模块用法和service模块相似
ansible test -m systemd -a "name=httpd state=started"
ansible test -m systemd -a "name=httpd state=stopped"
ansible test -m systemd -a "name=httpd state=restarted"
ansible test -m systemd -a "name=httpd state=reloaded"
user模块
关键参数 | |
---|
group | 属组 |
groups | 附加组 |
home | 设置家目录 |
remove | 删除用户并删除家目录 |
shell | 设置用户登录shell |
system | 系统用户 |
uid | 用户的id |
state | 创造或者删除 |
ansible test -m user -a "name=user1 shell=/sbin/nologin home=/opt/user1 uid=3000 group=root groups=root"
创造系统用户
ansible test -m user -a "name=user2 system=yes"
删除用户
ansible test -m user -a "name=user1 state=absent"
删除用户并删除其家目录
ansible test -m user -a "name=user3 state=absent remove=yes"
group模块
gid | 组id |
---|
name | 组名 |
system | 系统组 |
state | 创造或者删除 |
ansible test -m group -a "name=group1 gid=10000"
ansible test -m group -a "name=group2 system=yes"
ansible test -m group -a "name=group1 state=absent"
mount模块
path | 挂载到本地的目录 |
---|
src | 对端目录 |
fstype | 文件系统类型 nfs ext4 ext3 |
state | |
present | 只写入/etc/fstab不挂载 |
mounted | 即写入文件,也挂载 |
| |
absent | 卸载设备,并清理文件 |
unmounted | 只卸载不清理文件 |
建议挂载的时候使用mounted | 卸载的时候使用absent |
使用mount模块搭建nfs服务
首先在主机上面创建共享目录并修改权限
[root@gzz~66 data]# mkdir -pv /data/share
[root@gzz~66 data]# chmod -R 755 /data/share/
然后再主机上安装nfs和rpcbind服务
[root@gzz~66 data]# yum install nfs-utils rpcbind -y
修改配置文件增加从机信息
[root@gzz~66 data]# vi /etc/exports
1 /data/share/ 192.168.129.199(rw,no_root_squash,no_all_squash,sync)
2 /data/share/ 192.168.129.166(rw,no_root_squash,no_all_squash,sync)
然后按顺序启动服务
[root@gzz~66 data]# systemctl start rpcbind
[root@gzz~66 data]# systemctl start nfs
查看端口是否生效
[root@gzz~66 data]# showmount -e localhost
Export list for localhost:
/data/share 192.168.129.166,192.168.129.199
在从机上建立共享文件夹
[root@gzz~66 data]# ansible test -m file -a "path=/tmp/share state=directory"
从机上安装服务
[root@gzz~66 data]# ansible test -m yum -a "name=rpcbind state=present"
[root@gzz~66 data]# ansible test -m yum -a "name=nfs-utils state=present"
从机上启动服务 只需要启动rpcbind
[root@gzz~66 data]# ansible test -m service -a "name=rpcbind state=started"
查看端口
[root@gzz~66 data]# ansible test -m shell -a "showmount -e 192.168.129.66"
挂载
[root@gzz~66 data]# ansible test -m mount -a "src=192.168.129.66:/data/share path=/tmp/share fstype=nfs state=mounted"
setup模块
ansible_all_ipv4_addresses | 仅显示ipv4的信息 |
---|
ansible_devices | 仅显示磁盘设备信息 |
ansible_distribution | 显示是什么系统 |
ansible_distribution_major_version | 显示是系统主版本 |
ansible_distribution_version | 仅显示系统版本 |
ansible_machine | 显示系统类型例:32位,还是64位。 |
ansible_eth0 | 仅显示eth0的信息 |
ansible_hostname | 仅显示主机名 |
ansible_kernel | 仅显示内核版本 |
ansible_lvm | 显示lvm相关信息 |
ansible_memtotal_mb | 显示系统总内存 |
ansible_memory_mb | 详细显示内存情况 |
ansible_swaptotal_mb | 显示总的swap内存 |
ansible_mounts | 显示系统磁盘挂载情况 |
ansible_processor | 显示cpu个数(具体显示每个cpu的型号) |
ansible_processor_vcpus | 显示cpu个数(只显示总的个数) |
用户获取远程主机的一些信息
[root@gzz~66 share]# ansible test -m setup
直接指定setup可以执行,但是返回信息太多,所以我们可以使用filter进行过滤,得到我们想要的信息
获取远程主机的ip信息
ansible test -m setup -a "filter=ansible_all_ipv4_addresses"
获取远程主机的内存信息
ansible test -m setup -a "filter=ansible_memory_mb"
获取远程主机的主机名
ansible test -m setup -a "filter=ansible_fqdn"