Ansible自动化运维实战部署1
发布日期:2021-06-29 02:31:22 浏览次数:2 分类:技术文章

本文共 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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:HTML基础知识与简介
下一篇:常用的JVM参数详解

发表评论

最新留言

很好
[***.229.124.182]2024年04月20日 18时32分35秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章