ansible是基于python开发的配置管理和应用部署工具。
也是自动化运维的重要工具。
可以批量配置,部署,管理上千台主机。
只需要在一台主机配置ansible就可以完成其他主机的操作。
操作模式:
1、模块化操作:命令行执行
2、playbook:剧本,也就是把命令行脚本化,脚本的格式是yaml格式
ansible的特性:幂等性
幂等性:多次操作或者多次执行,对系统的影响不会发生变化,无论执行多少次结果都是一样的,ansible什么都不会做
ansible的四大组件:
1、lnventory : 主机清单 主机组
必须要声明管理主机的地址或者其他撇脂,不声明ansible无法对主机进行操作
2、modules :学习的核心
ansible的功能都是靠模块来实现
3、插件
4、playbooks :剧本 ---------脚本(复用)
模块和语法的学习
192.168.124.10 ansible
192.168.124.20 目标主机1
192.169.124.30 目标主机2
准备工作,安装epel源,安装ansible
yum -y install epel-release
yum -y install ansible
1 [root@localhost opt]# cd /etc/ansible
2 [root@localhost ansible]# ls
3 ansible.cfg hosts roles
4 [root@localhost ansible]# vim hosts
命令行:
ansible-doc -l 列出ansible所有的模块
1、command模块:基础模块 也是ansible的默认模块,不支持管道符和重定向操作,执行一般的linux命令
ansible <组名/ip地址> -m (指定模块,不加-m,默认使用command) -a <参数或者命令>
动态有变化的是无法跟踪的,如tail -f 加了-f就是动态的,不会显示,不加-f静态的,就会显示
command可以对目标主机的压缩包解压,但是必须要有指定的解压路径
ansible常用的参数
chdir :在目标主机提前进入目录,然后执行指令
[root@localhost ansible]# ansible 192.168.124.20 -a 'chdir=/home ls'
192.168.124.20 | CHANGED | rc=0 >>
test1
creates :判断文件是否存在,如果存在就不执行后面的指令
[root@localhost ansible]# ansible 192.168.124.20 -a 'creates=/opt/123 ls /opt'
192.168.124.20 | SUCCESS | rc=0 >>
skipped, since /opt/123 exists
removes :判断文件是否存在,如果存在就执行后面的指令
[root@localhost ansible]# ansible 192.168.124.20 -a 'removes=/opt/123 ls /opt'
192.168.124.20 | CHANGED | rc=0 >>
123
nginx-1.22.0
nginx-1.22.0.tar.gz
rh
2、shell模块 :支持管道符和重定向,也可以用逻辑表达式&&(且) ;(逻辑或)
1 [root@localhost ansible]# ansible 192.168.124.20 -m shell -a 'useradd 2test'
2 192.168.124.20 | CHANGED | rc=0 >>3 [root@localhost ansible]# ansible 192.168.124.20 -m shell -a 'echo 123456 | passwd --stdin test'
4 192.168.124.20 | CHANGED | rc=0 >>
5 更改用户 test 的密码 。
6 passwd:所有的身份验证令牌已经成功更新。
7 [root@localhost ansible]# ansible 192.168.124.20 -m shell -a 'echo 123 > /opt/123' 192.168.124.20 | CHANGED | rc=0 >>8 [root@localhost ansible]# ansible 192.168.124.20 -m shell -a 'cat /opt/123'
9 192.168.124.20 | CHANGED | rc=0 >>
10 123
练习:
在主机创建一个脚本,在脚本中写#!/bin/bash ifconfig 然后运行脚本,在一条命令中完成
[root@localhost ansible]# ansible 192.168.124.30 -m shell -a 'echo -e "#!bin/bash\nifconfig" > /opt/test.sh && sh /opt/test.sh'
192.168.124.30 | CHANGED | rc=0 >>
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 192.168.124.30 netmask 255.255.255.0 broadcast 192.168.124.255inet6 fe80::b995:4bbd:202c:45e5 prefixlen 64 scopeid 0x20<link>inet6 fe80::1bdf:23b7:bdb0:d771 prefixlen 64 scopeid 0x20<link>inet6 fe80::e95f:aa56:10e4:6888 prefixlen 64 scopeid 0x20<link>ether 00:0c:29:f2:6f:c7 txqueuelen 1000 (Ethernet)RX packets 23839 bytes 2467642 (2.3 MiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 21702 bytes 1777782 (1.6 MiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536inet 127.0.0.1 netmask 255.0.0.0inet6 ::1 prefixlen 128 scopeid 0x10<host>loop txqueuelen 1000 (Local Loopback)RX packets 37017 bytes 3226288 (3.0 MiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 37017 bytes 3226288 (3.0 MiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255ether 52:54:00:3c:3a:39 txqueuelen 1000 (Ethernet)RX packets 0 bytes 0 (0.0 B)RX errors 0 dropped 0 overruns 0 frame 0TX packets 0 bytes 0 (0.0 B)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
3、cron模块 :定时任务模块 minute / hour / day /month /weekday
对应 :分/时/日/月/周
创建定时任务:9月的第3天的8:30 执行ls /opt这个命令 名字为test1
[root@localhost ansible]# ansible 192.168.124.20 -m cron -a 'minute=30 hour=8 day=3 month=9 job="ls /opt" name="test1" '
job表示定时任务执行的命令
删除名字为None的定时任务
ansible 192.168.124.20 -m cron -a 'name="None" state=absent'
4、user模块 :用户管理模块
参数
name :用户名 必选参数
state :present 创建 可以不加默认是创建
absent 删除
system=yes/no 创建用户时no是普通用户,yes是程序用户
uid :指定用户的uid
group:指定用户组
shell:默认是系统用户可以不加/bin/bash
creates_home=yes/no 不是默认的家目录/home
/opt/test1家目录 create_home=yes 创建 ,no就是不创建
password:用户添加密码
remove=yes/no state=absent删除用户,删除用户时是否删除家目录
创建普通用户
参数
name :用户名 必选参数
state :present 创建 可以不加默认是创建
absent 删除
system=yes/no 创建用户时no是普通用户,yes是程序用户
uid :指定用户的uid
group:指定用户组
shell:默认是系统用户可以不加/bin/bash
creates_home=yes/no 不是默认的家目录/home
/opt/test1家目录 create_home=yes 创建 ,no就是不创建
password:用户添加密码
remove=yes/no state=absent删除用户,删除用户时是否删除家目录
创建普通用户
ansible 192.168.124.20 -m user -a 'name=xy102 system=no'
创建程序用户
ansible 192.168.124.20 -m user -a 'name=xy102 shell=/sbin/nologin system=yes'
创建用户指定家目录同时添加密码
ansible 192.168.124.20 -m user -a 'name=xy88 home=/opt/xy788 create_home=yes password=123456'
删除用户同时删除家目录
ansible 192.168.124.20 -m user -a 'name=xy88 remove=yes state=absent'
5、copy复制模块:当前主机的文件复制到目标主机
ansible 192.168.124.20 -m copy -a 'src=/opt/xy102.txt dest=/opt/'
src表示源文件 dest 目标主机的保存路径
mode 复制文件,表示权限
owner 文件的所有者 属主
group 文件的所在组 属组
ansible 192.168.124.20 -m copy -a 'src=/opt/xy102.txt dest=/opt/ mode=640 owner=bmm group=bmm'
content 指定复制的内容,就不能用src
ansible 192.168.124.20 -m copy -a 'content="黑神话:悟空,性价比高" dest=/opt/houzi.txt mode=777 owner=bmm group=bmm'
6、file模块:设置文件属性
mode owner group state=touch | abstent touch创建 absent删除
ansible 192.168.124.20 -m file -a 'path=/opt/abc.txt state=touch mode=777 owner=bmm group=bmm'
创建一个链接文件
ansible 192.168.124.20 -m file -a 'path=/opt/abc.txt .link src=/opt/abc.txt state=link'
删除文件
ansible 192.168.124.20 -m file -a 'path=/opt/abc.txt .link state=absent'
7、hostname模块:给目标主机设置主机名
ansible 192.168.124.30 -吗hostname -a "name=nginx"
8、ping模块 : 测试主机和目标主机是否ping通
ansible all -m ping
9、yum模块 :在目标主机安装软件,只能安装和卸载
安装软件
ansible 192.168.124.20 -m yum -a 'name=httpd'
卸载软件
ansible 192.168.124.20 -m yum -a 'name=httpd state=absent'
10、service模块 :管理目标主机上软件的运行状态
参数
name 服务名称 必加参数
state=started|stopped|restarted
enabled=true
runlevel=60运行级别,设置了开机自启就需要声明运行级别
ansible 192.168.124.20 -m service -a 'name=nginx enabled=true state=started runlevel=60'
1安装nginx 2开机自启nginx 开机自启 3访问的内容是this is nginx1 !
ansible 192.168.124.20 -m shell -a 'echo "this is nginx1" > /usr/share/nginx/html/index.html'
ansible 192.168.124.20 -a 'curl 192.168.124.20'
访问
11、防火墙和网络模块
拒绝30ping20
ansible 192.168.124.20 -m iptables -a 'chain=INPUT protocol=ICMP source=192.168.124.30 jump=REJECT' -b
防火墙策略拒绝80端口
ansible 192,168.233.20 -m iptables -a "chain=INPUT protocol=tcp destination port=8
0 jump=REJECT" -b
防火墙策略允许80端口
ansible 192,168.233.20 -m iptables -a "chain=INPUT protocol=tcp destination port=8
0 jump=ACCEPT" -b
删除防火墙策略
ansible 192,168.233.20 -m iptables -a "chain=INPUT protocol=tcp destination port=8
0 jump=REJECT state=absent" -b
给80端口放空
ansible 192.168.124.20 -m firewalld -a 'service=nginx zone=public permanent=true state=enabled immediate=true' -b
firewall的配置文件没有nginx,所以用下端口+协议来进行放空
ansible 192.168.124.20 -m firewalld -a 'port=80/tcp zone=public permanent=true state=enabled immediate=true' -b
删除放空的端口
ansible 192.168.124.20 -m firewalld -a 'port=80/tcp zone=public permanent=true state=disenabled immediate=true' -b
配置网卡
ansible 192.168.124.30 -m ansible.builtin.lineinfile -a "path=/etc/sysconfig/network-scripts/ifcfg-ens33 regexp='^IPADDR' line='IPADDR=192.168.124.29' " -b
重启网卡
ansible 192.168.124.30 -a 'systemctl restart network'
12、script模块 运行本地的脚本,把脚本运行的结果输出到目标主机
ansible 192.168.124.20 -m script -a '/opt/test.sh'
13、setup模块 查看目标主机的信息
查看cpu
ansible 1892.168.124.20 -m setup -a 'filter=ansible_* processor *'
查看内核
ansible 1892.168.124.20 -m setup -a 'filter=ansible_proc_cmdline'
查看内存
ansible 1892.168.124.20 -m setup -a 'filter=ansible_mem*'
查看系统信息
ansible 1892.168.124.20 -m setup -a 'filter=ansible_system'
总结:
command和shell(常用)
copy、yum、user
service服务 对服务进行管理
file模块 文件属性进行修改
hostname 改主机名
ping
主机清单:
主机组:IP地址
批量匹配ip地址10-50
vim hosts
192.168.124.[1:5] [0:9]
组嵌套
[web]
192.168.124.20
[web1]
192.168.124.30
[webs:children]
web
web1