Ansible自动化运维实战部署1

Ansible自动化运维

一、abstract-简介
1、LOGO

在这里插入图片描述
ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,
实现了批量系统配置、批量程序部署、批量运行命令等功能。
无客户端。

2、工作原理

在这里插入图片描述

二、Ansible安装部署
1、准备环境
环境:所有机器关闭防火墙和selinux
主机:4台  1个控制节点 3个被控制节点
解析:本地互相解析(所有机器)
# vim /etc/hosts
192.168.1.10 host1
192.168.1.11 host2
192.168.1.12 host3
192.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<<EOF
[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
baseurl=http://mirrors.aliyun.com/epel/7/$basearch
failovermethod=priority
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

[epel-debuginfo]
name=Extra Packages for Enterprise Linux 7 - $basearch - Debug
baseurl=http://mirrors.aliyun.com/epel/7/$basearch/debug
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=0

[epel-source]
name=Extra Packages for Enterprise Linux 7 - $basearch - Source
baseurl=http://mirrors.aliyun.com/epel/7/SRPMS
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=0
EOF
#yum -y install ansible
官方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    //在配置文件最下方追加主机名
host1
host2
host3 
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]         //使用[]标签指定主机组 ----标签自定义
host1
host2
host3
[root@localhost ~]# ansible webserver -m ping -u root -k -o
SSH 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]    //定义组与组之间的组合
apache
nginx
[webserver:vars]
ansible_ssh_user='root'
ansible_ssh_pass='666666'
如有需要让Nginx和apach组操作时直接访问webserver即可
# ansible webserver -m ping -o
7、自定义主机列表
# vim hostlist    //自定义一个文件
[dockers]
host1
host2
[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  #追加如下内容
123123
234234
[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    //查询系统负载
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ever Since!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值