Ansible自动化运维实战部署1
发布日期:2021-06-29 02:31:22
浏览次数:3
分类:技术文章
本文共 7183 字,大约阅读时间需要 23 分钟。
Ansible自动化运维
一、abstract-简介
1、LOGO
ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点, 实现了批量系统配置、批量程序部署、批量运行命令等功能。 无客户端。2、工作原理
二、Ansible安装部署
1、准备环境
环境:所有机器关闭防火墙和selinux主机:4台 1个控制节点 3个被控制节点解析:本地互相解析(所有机器)# vim /etc/hosts192.168.1.10 host1192.168.1.11 host2192.168.1.12 host3192.168.1.9 ansible-server (控制节点服务器端)配置ssh公钥认证:控制节点需要发送ssh公钥给所有被控制节点[root@ansible-server ~]# ssh-keygen[root@ansible-server ~]# ssh-copy-id -i 192.168.1.10 #所有被控节点机器
2、安装Ansible
阿里yum源安装:cat >/etc/yum.repos.d/epel-7.repo<
官方epel源# yum install -y epel-release# yum install -y ansible# ansible --version //查看版本# ansible --help //查看帮助# rpm -ql ansible //列出所有文件# rpm -qc ansible //查看配置文件# ansible-doc -l //查看所有模块# ansible-doc -s yum //看yum模块,了解其功能
三、Ansible基础
1、定义主机清单
# vim /etc/ansible/hosts //在配置文件最下方追加主机名host1host2host3
2、测试连通性
# ansible host1 -m ping //测试Ansible与被管理主机的连通性,-m是指模块# ansible host1 -m ping -0 //简洁输出,一行
3、know_hosts
# ansible host2 -m ping -u root -k -o //增加用户名选项,增加密码选项# vim /etc/ssh/ssh_config //修改ssh配置文件去掉(yes/no)的询问StrictHostKeyChecking no# sysyemctl restart sshd //重启ssh
4、ping和ssh的区别
ping:是ICMP网际消息管理协议,ssh是应用层安全登录程序,关闭host1主机的ssh进程,进行ping连通性测试成功。使用ansible对host1进行联通测试,却是失败的。
总结:ping的通,ssh不一定联的通,ansible的ping,是探测ssh程序是否连接。不是icmp协议
四、inventory-主机清单
1、含义:清查;存货清单;财产目录;主机清单
2、增加主机组
官方链接:http://docs.ansible.com/ansible/intro_inventory.html#
# vim /etc/ansible/hosts //直接在主机的上方添加中括号代表组名[webserver] //使用[]标签指定主机组 ----标签自定义host1host2host3[root@localhost ~]# ansible webserver -m ping -u root -k -oSSH password: host3 | SUCCESS => { "changed": false, "ping": "pong"}host1 | SUCCESS => { "changed": false, "ping": "pong"}host4 | SUCCESS => { "changed": false, "ping": "pong"}host2 | SUCCESS => { "changed": false, "ping": "pong"}
3、增加用户名,密码
# vim /etc/ansible/hosts[webserver] host1 ansible_ssh_user='root' ansible_ssh_pass='111111'host2 ansible_ssh_user='root' ansible_ssh_pass='111111'host3 ansible_ssh_user='root' ansible_ssh_pass='111111'也可以用正则方法:host[1:4] ansible_ssh_user='root' ansible_ssh_pass='111111'[root@localhost ~]# ansible webserver -m ping -o //测试面用户名和密码设置成功
4、增加端口
# ss -anpt | grep sshd //查看默认端口号LISTEN 0 128 *:22 *:* users:(("sshd",pid=976,fd=3))ESTAB 0 0 192.168.239.129:22 192.168.239.1:56164 users:(("sshd",pid=3739,fd=3))LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=976,fd=4))# vim /etc/ssh/sshd_config //修改默认端口号port 2222# systemctl restart sshd //重启ssh# ansible host1 -m ping -o #访问会报错# vim /etc/ansible/hosts //修改主机访问端口号host1 ansible_ssh_user='root' ansible_ssh_pass='111111' ansible_ssh_port='2222'# ansible host1 -m ping -o #访问成功
5、组定义:变量
# vim /etc/ansible/hosts[webserver] host1 ansible_ssh_port='2222' //特殊的单独定义[webserver:vars] //定义组变量ansible_ssh_user='root'ansible_ssh_pass='111111'
常用变量:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JkmsC7gV-1622185486182)(C:\Users\Y\AppData\Roaming\Typora\typora-user-images\image-20210527192336442.png)]
6、子分组
将不同的分组进行组合
# vim /etc/ansiable/hosts[apache]host[1:2][nginx]host[3:4][webserver:children] //定义组与组之间的组合apachenginx[webserver:vars]ansible_ssh_user='root'ansible_ssh_pass='666666'如有需要让Nginx和apach组操作时直接访问webserver即可# ansible webserver -m ping -o
7、自定义主机列表
# vim hostlist //自定义一个文件[dockers]host1host2[dockers:vars]ansible_ssh_user='root'ansible_ssh_pass='111111'# ansible -i /hostlist dockers -m ping -o //-i是调用一个路径下的一个hostlist的文件(跟绝对路径),dockers是组名-i:指定清单文件小注释:如果不通,手动连接第一次,第一次需要手动输入密码。"第一次"
五、Ad-Hoc-点对点模式
简介:临时的在ansible中是指需要快速执行的单挑命令,并且不需要保存的命令,对于复杂的命令则为playbook.
1、copy模块(帮助手册ansible-doc copy)
模块参数:src=sourc"指定源文件路径" owner:指定属主 group:指定属组 dest=destination"目标地址"(拷贝到的地方) mode:指定权限 backup:再覆盖之前原文件备份(备份文件包含时间信息)有yes/no两个选项[root@ansible-server ~]# vim a.txt #创建一个测试文件123123[root@ansible-server ~]# ansible weball -m copy -a 'src=/root/a.txt dest=/opt owner=root group=root mode=644' -o[root@ansible-server ~]# vim a.txt #追加如下内容123123234234[root@ansible-server ~]# ansible weball -m copy -a 'src=/root/a.txt dest=/opt/ owner=root group=root mode=644 backup=true' -o注释:如果文件没有变化,不会备份。只有文件内容不同,才会做备份。(-a修饰符)登录被控制机器其中一台查看[root@ansible-web1 ~]# cat /opt/a.txt.15301.2019-09-01\@00\:35\:18~[root@ansible-server ~]# ansible weball -m shell -a 'mv /mnt/qf.txt /tmp' -o移动被控制节点的文件
2、用户模块(帮助手册ansible-doc user)
创建用户[root@ansible-server ~]# ansible weball -m user -a 'name=qf state=present' //创建用户登录到被管理主机查看是否创建成功或观看管理主机操作回显修改密码[root@ansible-server ~]# echo '111111' | openssl passwd -1 -stdin //生成加密密码值$1$XVzsJMDr$5wI4oUaQ.emxap6s.N27注:openssl用于加密,-1是密码的类型(数值越复杂),stdin是标准输入输出[root@ansible-server ~]# ansible weball -m user -a 'name=qf password="$1$XVzsJMDr$5wI4oUaQ.emxap6s.N27"' //修改密码修改shell(默认登录shell是bin/bash)[root@ansible-server ~]# ansible weball -m user -a 'name=qf shell=/sbin/nologin append=yes' //append追加修改改变shell路径后用户则登录不上删除用户[root@ansible-server ~]# ansible weball -m user -a 'name=qf state=absent' //删除用户
3、软件包管理模块(帮助手册ansiable-doc yum)
state= #状态是什么,干什么state=absent 用于remove安装包state=latest 表示最新的state=removed 表示卸载[root@ansible-server ~]# ansible host1 -m yum -a 'name="*" state=latest' //升级所有包[root@ansible-server ~]# ansible host2 -m yum -a 'name="httpd" state=latest' //安装apache[root@ansible-server ~]# yum list | grep httpd //查看是否安装成功[root@ansible-server ~]# ansible host2 -m yum -a 'name="httpd" state=absent' //卸载apache或[root@ansible-server ~]# ansible host2 -m yum -a 'name="httpd" state=removed'
4、服务管理模块(帮助手册ansible-doc service)
[root@ansible-server ~]# ansible webservers1 -m service -a "name=httpd state=started" #启动[root@ansible-server ~]# ansible webservers1 -m service -a "name=httpd state=stopped" #停止[root@ansible-server ~]# ansible webservers1 -m service -a "name=httpd state=restarted" #重启[root@ansible-server ~]# ansible webservers1 -m service -a "name=httpd state=started enabled=yes" #开机启动[root@ansible-server ~]# ansible webservers1 -m service -a "name=httpd state=started enabled=no" #开机关闭
5、文件模块(帮助手册ansible-doc file)
模块参数属性: owner:修改属主 group:修改属组 mode:修改权限 path=:要修改文件的路径 recurse:递归的设置文件的属性,只对目录有效yes:表示使用递归设置state:状态touch:创建一个新的空文件 directory:创建一个新的目录,当目录存在时不会进行修改[root@ansible-server ~]# ansible webservers1 -m file -a 'path=/tmp/1.txt mode=777 state=touch' //创建文件[root@ansible-server ~]# ansible ansible-web2 -m file -a 'path=/tmp/2.txt mode=777 owner=nginx state=touch' //修改属主[root@ansible-server ~]# ansible webservers1 -m file -a 'path=/tmp/qf mode=777 state=directory' //创建一个目录
6、收集模块:收集目标主机的信息(帮助手册ansible-doc setup)
[root@ansible-server ~]# ansible webservers1 -m setup #收集所有信息[root@ansible-server ~]# ansible webservers1 -m setup -a 'filter=ansible_all_ipv4_addresses' #只查询ipv4的地址filter:过滤
7、shell模块(帮助手册ansible-doc shell)
shell模块能完成以上模块的所有功能[root@ansible-server ~]# ansible webserver -m shell -a 'hostname' -o //获取主机名[root@ansible-server ~]# ansible webserver -m shell -a 'hostname' -o -f 2 //-f 2指定线程数(并发量比较大时使用)[root@ansible-server ~]# ansible webserver -m shell -a 'yum -y install httpd' -o //部署apache[root@ansible-server ~]# ansible webserver -m shell -a 'uptime' -o //查询系统负载
转载地址:https://blog.csdn.net/YYYYang864/article/details/117363429 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
能坚持,总会有不一样的收获!
[***.219.124.196]2024年03月31日 22时37分58秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Atitit nlp自然语言处理类库(java python nodejs c#net) 目录 1.1. Python snownlp 1 1.2. NLP.js一个nodejs/javascri
2019-04-29
Atitit nlp用到的技术与功能自然语言处理 v3 t99.docx Atitit nlp用到的技术与常见类库 目录 1. 常用的技术 1 1.1. 语言处理基础技术 分词 相似度等 1 1.2
2019-04-29
Atitit 2000drmmr v2 t05.docx Atitit 2000drmmr v1 t99 2000.1.26 hs,yweywe jla n dashw ner kl .na ni
2019-04-29
常见方案 目录 1. 发现目前 WEB 上主流的视频直播方案有 HLS 和 RTMP, 1 2. 实现直播的方法有很多,但是常用的,就这几个。 3个直播协议:rtmp、rtsp、hls。 和三个端:
2019-04-29
Atitit 直播问题总结ffmpeg 目录 1.1. 屏幕太大,可以使用-s调整分辨率 1 1.2. Full size 1 1.3. 流畅度调整 1 2. 1 2.1. 没有录音 1 2.2.
2019-04-29
paip.索引优化---sql distict—order by 法
2019-04-29
paip.输入法编程---带ord gudin去重复-
2019-04-29
paip.输入法编程---增加码表类型
2019-04-29
paip.提升性能--- mysql 建立索引 删除索引 很慢的解决.
2019-04-29
paip.输入法编程---智能动态上屏码儿长调整--.txt
2019-04-29
Atitit sumdoc t0 final index D:\BaiduNetdiskDownload\sumdoc t0 final\sumdoc t0 wps cld bek D:\Baid
2019-04-29
Atitit sumdoc t0 final index D:\BaiduNetdiskDownload\sumdoc t0 final\sumdoc t0 wps cld bek D:\Baid
2019-04-29
Atitit sumdoc t0 final index
2019-04-29
atitit 编程语言选型知识点体系.docx 编程语言选型时,你需要考虑的几个方面 目录 1. 1.2. 类型系统 5 1 2. 1.5. 语言规范 25 1 3. 1.6. 编程范式 52
2019-04-29
Atitit 编程语言语言规范总结 目录 1. 语言规范 3 2. Types 3 2.1.1. Primitive types 3 2.1.2. Compound types 4 3. State
2019-04-29
Atitit QL查询语言总结 目录 1. QL = Query Language, 是查询语言的简称 1 2. 具体实现 1 2.1. Apcl 流程控制语言 1 2.2. 脚本流程控制 2 2.
2019-04-29
Atitit 开发效率大法 v0 t025.docx Atitit 提升开发效率几大策略 目录 1. 提升效率三原则 3 1.1. 更少的代码量简化 3 1.2. 优化配置减少等待 3 1.3.
2019-04-29