Ansible:远程自动化运维

Ansible:远程自动化运维

Ansible

是基于python开发的配置管理和应用部署工具。也是自动化运维的重要工具。可以批量配置,部署,管理上千台主机。只需要固定在一台主机配置ansible就可以完成其他主机的操作

操纵模式

1.1 模块化操作,命令行执行

1.2 playbook,剧本,也是把命令行脚本化,脚本的格式yaml格式

幂等性

ansible特性:幂等性

幂等性:多次操作或者是多次执行,对系统的影响不会发生变化,无论执行对多少次结果都是一样的

ansible什么都不会做

四大组件

1.1 Inventory(主机清单):主机组,必须要声明管理主机的地址或者其他配置,不声明ansible无法对目标主机进行操作

1.2 modules(模块):学习核心。ansible的功能是靠模块来实现的

1.3 插件

1.4 playbooks(剧本):脚本(复用)

安装及模块

语法:命令行

192.168.100.12 ansible

192.168.100.13 被管理端

192.168.100.14 被管理端

同时
systemctl stop firewalld
setenforce 0
df -h

test2
yum -y install epel-release
yum -y install ansible
cd /etc/ansible
vim hosts
20 取消注释[web]
23 192.168.100.13
33 [xy102]
37 192.168.100.14
wq!

ssh-keygen -t rsa
# 一路回车

sshpass -p '123' ssh-copy-id root@192.168.100.13

sshpass -p '123' ssh-copy-id root@192.168.100.14

ansible-doc -l  # 列出ansible所有已安装的模块(支持的模块)
q   # 退出

模块和语法

第一个模块:command

基础模块,也是ansible的默认模块,不支持管道符和重定向操作,执行一般的linux命令

ansible <组名/ip地址> -m 指定模块,不加-m,默认使用command,-a <参数或者命令>

ansible 192.168.100.13 -m command -a "date"
yes  # 报错再执行sshpass -p '123' ssh-copy-id root@192.168.100.13

ansible 192.168.100.14 -m command -a "date"
yes  # 报错再执行sshpass -p '123' ssh-copy-id root@192.168.100.14

# 集群执行
ansible [web] -m command -a "date"

# host里面所有都执行
ansible all -m command -a "date"

需要交互的、动态查看、解压都不可以、静态可以--幂等性

常用参数:

chdir:在目标主机提前进入目录,然后执行指令

ansible 192.168.100.13 -a "chdir=/home ls ./"
# 先进入home再执行./(./可不加),chdir相当于cd切换目录

creates:判断文件是否存在,如果存在就不执行后面的指令

test3
touch 123

test2
ansible 192.168.100.13 -a "creates=/opt/123 ls /opt"
# 会告诉你存在,不执行后面操作

removes:判断文件是否存在,如果存在,执行指令

ansible 192.168.100.13 -a "removes=/opt/123 ls /opt"

解压

ansible 192.168.100.13 -a "tar -xf /opt/nginx-1.22.tar.gz -C /opt"

ansible 192.168.100.13 -a "chdir=/opt tar -xf nginx-1.22.tar.gz "

第二个模块:shell模块

支持管道符和重定向,也可以用逻辑表达式 &&且 ;逻辑或

ansible 192.168.100.14 -a “useradd test" 

ansible 192.168.100.14 -m shell -a "echo 123456 | passwd --stdin test"

ansbile 192.168.100.14 -m shell -a "echo 123 > /opt/123"

ansible 192.168.100.14 -m shell -a "cat /opt/123"

多个命令连在一块

ansible 192.168.100.14 -m shell -a "touch /opt/123.txt && echo 123 > /opt/123.txt && cat /opt/123.txt"
# 会有警告的提示

目标主机创建一个脚本,在脚本中写#!/bin/bash ifconfig 然后运行脚本,在一条命令完成

ansible 192.168.100.14 -m shell -a 'echo -e "#!/bin/bash\nifconfig" > /opt/test.sh && sh /opt/test.sh'

第三个模块:cron模块

定时任务模块 minute/hour/day/mouth/weekday 分/时/日/月/周

ansible 192.168.100.13 -m cron -a 'minute=30 hour=8 day=* job="ls /opt"'
# job=表示定时任务执行的命令

ansible 192.168.100.13 -a 'crontab -l'  # 查看创建的定时任务

ansible 192.168.100.13 -m cron -a 'minute=30 hour=8 day=3 month=9 job="ls /opt" name="test"'

删除定时任务

ansible 192.168.100.13 -m cron -a 'name="test" state=absent'


ansible 192.168.100.13 -m cron -a 'name="None" state=absent'
# 会将None全部删除,所以在创建定时任务的时候一定要指定名字

第四个模块:user模块

用户管理模块

第一个参数:name(必选参数)
state=present|absent present 创建  absent 删除
system=yes|no  创建用户时,是否为系统账号,no是普通用户,yes是程序用户
uid:指定用户的uid
group:指定用户组
shell:默认系统用户不加(/bin/bash)
create_home=yes|no  不是默认的家目录/home。/opt/test1家目录,create_home=yes创键,no就是不创建
password:用户添加密码
remove=yes|no  state=absent删除用户,删除用户时是否删除家目录
ansible 192.168.100.13 -m user -a 'name=xy102 system=no'

ansible 192.168.100.13 -m user -a 'name=xy77 uid=900 shell=/sbin/nologin system=yes'


ansible 192.168.100.13 -m user -a 'name=xy77 home=/opt/xy77 create_home=yes password=123456'

删除
ansible 192.168.100.13 -m user -a 'name=xy77 state=absent'

ansible 192.168.100.13 -m user -a 'name=xy77 remove=yes state=absent'

第五个模块:copy模块

copy复制模块,把指定主机的文件复制到目标主机

cd /opt
touch xy102.txt
echo 123 > xy102.txt
ansible 192.168.100.14 -m copy -a 'src=/opt/xy102.txt dest=/opt/'

src表示源文件

dest目标主机的保存路径

mode复制文件时 表示权限

owner文件的所有者 属主

group文件所在组 属组

content指定复制的内容,就不能用src

ansible 192.168.100.14 -m copy -a 'src=/opt/xy102.txt dest=/opt/ mode=640'

ansible 192.168.100.14 -m copy -a 'src=/opt/xy102.txt dest=/opt/ mode=640 owner=dn group=dn'
# 这个用户在目标主机必须提前存在
ansible 192.168.100.14 -m copy -a 'content="黑神话:悟空,真好玩" dest=/opt/houzi.txt mode=777 owner=dn group=dn'

# 远程把houzi.txt改成 孙悟空.txt
(1)使用command模块
ansible 192.168.100.14 -a 'mv /opt/houzi.txt /opt/孙悟空.txt'

(2)使用shell模块
ansible 192.168.100.14 -m shell -a 'mv /opt/孙悟空.txt /opt/猪八戒.txt'

第六个模块:file模块

设置文件属性

mode owner group state=touch|absent touche创建 absent删除

ansible 192.168.100.13 -m file -a 'path=/opt/abc.txt state=touch mode=777 owner=dn group=dn'
ansible 192.168.100.13 -m file -a 'path=/opt/abc.txt.link src=/opt/abc.txt state=link'

删除
ansible 192.168.100.13 -m file -a 'path=/opt/abc.txt.link state=absent'

第七个模块:hostname模块

设置远程主机的主机名

ansible 192.168.100.13 -m hostname -a "name=nginx1"
su 刷新

第八个模块:ping模块

看success表示成功

ansible all -m ping

第九个模块:yum模块

yum模块只能安装和卸载软件

ansible 192.168.100.13 -m yum -a 'name=httpd'  # 安装

ansible 192.168.100.13 -m yum -a 'name=httpd state=absent' # 卸载

第十个模块:server模块

管理模块,用来管理目标主机软件的运行状态

name 服务名称

state=started|stopped|restarted

enabled=true 开机自启

runlevel=40 运行级别 如果设置了开机自启,就需要声明运行级别

ansible 192.168.100.13 -m yum -a 'name=nginx'  # 安装软件

ansible 192.168.100.13 -m service -a 'name=nginx enabled=true state=started runlevel=60'  # 管理

1、安装nginx

2、开启nginx、开机自启

3、访问nginx,访问内容是this is nginx1

ansible 192.168.100.13 -m shell -a 'echo "this is nginx1" > /usr/share/nginx/html/index.html && curl 192.168.100.13'   # 访问nginx,访问内容是this is nginx1

第十一个模块:防火墙网络模块

test4 # iptables -vnL 

ansible 192.168.100.14 -m iptables -a 'chain=INPUT protocol=ICMP source=192.168.100.12 jump=REJECT' -b
# -b 表示后台运行
含义:14不能ping12

yum -y install nginx

ansible 192.168.100.13 -m iptables -a "chain=INPUT protocol=tcp destination_port=80 jump=REJECT" -b

ansible 192.168.100.13 -m iptables -a "chain=INPUT protocol=tcp destination_port=80 jump=ACCEPT" -b

iptables -vnL   # test4

ansible 192.168.100.13 -m iptables -a "chain=INPUT protocol=tcp destination_port=80 jump=REJECT state=absent" -b

test3
systemctl start firewalld


test2
ansible 192.168.100.13 -m firewalld -a "service=nginx zone=public permanent=true state=enabled immediate=true" -b

# 会报错

test3
firewall-cmd --get-services
# 支持firewall支持所有nginx的类型

ansible 192.168.100.13 -m firewalld -a "port=80/tcp zone=public permanent=true staste=enabled immediate=true" -b

删除
ansible 192.168.100.13 -m firewalld -a "port=80/tcp zone=public permanent=true staste=disabled immediate=true" -b

修改网卡
vim /etc/sysconfig/network-scripts/ifcfg-ens33

ansible 192.168.100.14 -m ansible.builtin.lineinfile -a "path=/etc/sysconfig/network-scripts/ifcfg-ens33 regexp='^IPADDR' line='IPADDR=192.168.100.24'" -b

ifconfg  # 未变,重启

ansible 192.168.100.14 -a 'systemctl restart network'

不动,去虚拟机查看,修改成功,ctrl+Z

第十二个模块:script模块

运行的本地脚本,把脚本运行的结果输出到目标主机

(1)test2
cd /opt
vim test.sh
#!/bin/bash
echo "hello world" > /opt/test1.txt
wq!
chmod 777 test.sh
ansible 192.168.100.13 -m script -a '/opt/test.sh'
test3 查看

(2)test2
cd /opt
vim test.sh
#!/bin/bash
ifconfig
wq!
ansible 192.168.100.13 -m script -a '/opt/test.sh'

(3)test2
cd /opt
vim test.sh
#!/bin/bash
ifconfig > /opt/test2.txt
wq!
ansible 192.168.100.13 -m script -a '/opt/test.sh'

test3
cat /opt/test2.txt   # 静态出现test3的ifconfig内容

(4)test2
vim test.sh
#!/bin/bash
free -h > /opt/test3.txt
df -h > /opt/test4.txt
wq!
ansible 192.168.100.13 -m script -a '/opt/test.sh'

test3
cat /opt/test3.txt
cat /opt/test4.txt   # 静态查看test3的free -h和df -h

第十三个模块:setup模块

查看目标主机的硬件信息。(ip地址、cpu、内核版本、系统信息)

ansible 192.168.100.13 -m setup    # 收集目标主机的信息

-a  # 收集目标主机的指定信息

ansible 192.168.100.13 -m setup -a 'filter=ansible_*processor*'   # 查看目标主机cpu情况

ansible 192.168.100.13 -m setup -a 'filter=ansible_proc_cmdline'   # 查看目标主机内核版本

ansible 192.168.100.13 -m setup -a 'filter=ansible_men*'   # 查看目标主机内存

ansible 192.168.100.13 -m setup -a 'filter=ansible_system'   # 查看目标主机系统信息

十三个模块总结:

(1)command和shell(常用)——shell可以通用command的命令,多了管道符、重定向和命令结合、连接符

(2)copy、yum、user(比较多)

(3)service服务模块——只能服务进行管理、只能开、关、重启,目前不能查看服务状态

​ file模块——对文件属性进行修改

​ hostname模块——改主机名

​ ping模块——ping

主机清单

含义:主机清单含主机组,主机组包含ip地址。主机组:ip地址

非默认端口

cd /etc/ansible
vim hosts
(1)方法一
192.168.100.13:10022
# 非默认端口的情况下,可在ip地址后指定端口,m默认则不需要指定

(2)方法二
192.168.100.10 ansible_port=22 ansible_user=root ansible_password=123
# 指定目标主机ip、端口、用户、密码
wq!

ansible 192.168.100.10 -m ping
# 报错 192.168.100.10 | FAILED! => {
    "msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this.  Please add this host's fingerprint to your known_hosts file to manage this host."
}
# 表示在连接目标主机时,尝试使用密码验证,但是由于主机密钥检查功能开启,导致连接失败
解决办法:
ssh-keyscan -H 192.168.100.15 >> ~/.ssh/known_hosts   # 手动添加主机指纹到'konwn_hosts'文件


(二)对当前组内的所有主机生效,前提:端口、用户、密码一样
cd /etc/ansible
vim hosts
[web]
192.168.100.13
192.168.100.10 
ansible_port=22 
ansible_user=root 
ansible_password=123
wq!

(三)批量匹配ip地址
[web]
192.168.100.1[1:13]   # 表示11到13
ansible_port=22 
ansible_user=root 
ansible_password=123

# 192.168.100.[1:5][0:9],表示从192.168.100.10~50

wq!

ansible web -m ping   # 开的可以ping通,没开的ping不通

(四)组嵌套
[web]
192.168.100.13

[web1]
192.168.100.14

[webs:children]    # 嵌套
web
web1
wq!

ansible webs -m ping    # ping的是web和web1里面的主机 

总结:批量和组嵌套重要

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值