ansible引论

Ansible基本概述

知识储备

什么是Ansible?
Ansible是一个自动化统一配置管理工具,自动化主要体现在Ansible集成了丰富模块以及功能组件,可以通过一个命令完成一系列的操作,进而能减少重复性的工作和维护成本,可以提高工作效率。

为什么要用ansible?
ansible可以通过一个命令完成运维人员的一系列操作,进而减少重复性的工作以及维护成本,所以使用ansible可以提高工作效率,提高工作准确度,减少维护成本,减少重复性的工作

如何使用ansible?
请看下面分解

自动化运维应用场景

“运维的未来是,让研发人员能够借助工具、自动化和流程,并且让他们能够在运维干预极少的情况下部署和运营服务,从而实现自助服务。每个角色都应该努力使工作实现自动化。” ——《运维的未来》

企业实际应用场景分析
Dev开发环境
Test测试环境
Pre预发布环境
Master生成环境

自动化运维应用场景
文件传输
应用部署
配置管理
任务流编排

常用自动化运维工具
Ansible:python开发,Agentless,中小型应用环境
Saltstack:python开发,一般需部署agent,执行效率更高,但对python版本要求较高
Puppet:ruby开发, 功能强大,配置复杂,重型,适合大型环境
Fabric:python开发,agentless
Chef:ruby开发,国内应用少

Ansible

Ansible是一个自动化统一配置管理工具,自动化主要体现在Ansible集成了丰富模块以及功能组件,可以通过一个命令完成一系列的操作,进而能减少重复性的工作和维护成本,可以提高工作效率。
官网:https://www.ansible.com/
官方文档:/https://docs.ansible.com
源代码仓库:https://github.com/ansible/ansible

Ansible发展史
作者:Michael DeHaan( Cobbler 与 Func 作者)
ansible 的名称来自科幻小说《安德的游戏》中跨越时空的即时通信工具,使用它可以在相距数光年的距离,远程实时控制前线的舰队战斗。
2012-03-09,发布0.0.1版,
2015-10-17,Red Hat宣布1.5亿美元收购

同类型软件对比
1.puppet 学习难,安装ruby环境难,没有远程执行功能
2.ansible 轻量级,大规模环境下只通过ssh会很慢,串行的
3.saltstack 一般选择salt会使用C/S结构的模式,salt-master和salt-minion,并行的,大规模批量操作的情况下,会比Ansible速度快一些,底层使用的是zero-MQ消息队列

Ansible特性
模块化:调用特定的模块完成特定任务,支持自定义模块,可使用任何编程语言写模块
Paramiko(python对ssh的实现),PyYAML,Jinja2(模板语言)三个关键模块
基于Python语言实现
部署简单,基于python和SSH(默认已安装),agentless,无需代理不依赖PKI(无需ssl)
安全,基于OpenSSH
幂等性:一个任务执行1遍和执行n遍效果一样,不因重复执行带来意外情况,此特性非绝对
支持playbook编排任务,YAML格式,编排任务,支持丰富的数据结构
较强大的多层解决方案 role
ansible服务端基于ssh来控制客户端

Ansible架构

在这里插入图片描述

Ansible安装与入门

ansible的安装方法有多种,官方文档:
https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html

Ansible安装

Ansible安装有多种方式,常用的方式有yum、源码包编译安装等

yum安装
[root@localhost ~]# curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@localhost ~]# yum install ansible -y
#查看安装的ansible版本
[root@m01 ~]# ansible --version
ansible 2.9.21
config file = /etc/ansible/ansible.cfg
configured module search path = [u’/root/.ansible/plugins/modules’, u’/usr/share/ansible/plugins/modules’]
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Nov 16 2020, 22:23:17) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]

编译安装
[root@localhost ~]# yum -y install python-jinja2 PyYAML python-paramiko python-babel python-crypto
[root@localhost ~]# wget https://releases.ansible.com/ansible/ansible-2.9.18.tar.gz
[root@localhost ~]# tar xf ansible-2.9.18.tar.gz
[root@localhost ~]# cd ansible-2.9.18
[root@localhost ~]# python setup.py build
[root@localhost ~]# python setup.py install
[root@localhost ~]# mkdir /etc/ansible
[root@localhost ~]# cp -r examples/* /etc/ansible

PIP安装
[root@localhost ~]# yum install python-pip
[root@localhost ~]# pip install --upgrade pip
[root@localhost ~]# pip install ansible --upgrade

检验
[root@localhost ~]# ansible --version
ansible 2.9.18
config file = /etc/ansible/ansible.cfg
configured module search path = [u’/root/.ansible/plugins/modules’, u’/usr/share/ansible/plugins/modules’]
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Nov 16 2020

Ansible相关文件

配置文件
/etc/ansible/ansible.cfg 主配置文件,配置ansible工作特性
/etc/ansible/hosts 主机清单
/etc/ansible/roles/ 存放角色的目录

ansible主配置文件
Ansible 的配置文件 /etc/ansible/ansible.cfg ,其中大部分的配置内容无需进行修改

[defaults]
#inventory = /etc/ansible/hosts # 主机列表配置文件
#library = /usr/share/my_modules/ # 库文件存放目录
#remote_tmp = $HOME/.ansible/tmp # 临时py命令文件存放在远程主机目录
#local_tmp = $HOME/.ansible/tmp # 本机的临时命令执行目录
#forks = 5 # 默认并发数
#sudo_user = root # 默认用户
#ask_sudo_pass = True # 每次执行ansible命令是否询问ssh密码,true代表询问
#ask_pass = True
#remote_port = 22
#host_key_checking = False # 检查对应服务器的host_key,建议取消注释
#log_path = /var/log/ansible.log # 日志文件,建议启用
#module_name = command # 默认模块,可以修改为shell模块

inventory 主机清单
ansible的主要功能在于批量主机操作,为了便捷地使用其中的部分主机,可以在inventory file中将其分组命名,默认的inventory file为 /etc/ansible/hosts,inventory file可以有多个,且也可以通过Dynamic Inventory来动态生成
官方文档:https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html

主机清单文件格式
inventory文件遵循INI文件风格,中括号中的字符为组名。可以将同一个主机同时归并到多个不同的组中
此外,当如若目标主机使用了非默认的SSH端口,还可以在主机名称之后使用冒号加端口号来标明,如果主机名称遵循相似的命名模式,还可以使用列表的方式标识各主机。
说明:使用主机清单,如果出现需要记录known_host的时候,可以在/etc/ssh/ssh_config中增加StrictHostKeyChecking no

场景一、基于密码连接
[root@oldboy.com ~]# cat /etc/ansible/hosts
#方式一、主机+端口+密码
[webservers]
172.16.1.7 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=‘1’
172.16.1.8 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=‘1’

#方式二、主机+端口+密码
[webservers]
web[1:2].oldboy.com ansible_ssh_pass=‘1’

#方式三、主机+端口+密码
[webservers]
web[1:2].oldboy.com
[webservers:vars]
ansible_ssh_pass=‘1’

[test]
192.168.15.8 ansible_connection=local #指定本地连接,无需ssh配置
#ansible_connection=ssh 需要StrictHostKeyChecking no
192.168.15.7 ansible_connection=ssh ansible_port=2222 ansible_user=chenyang ansible_password=chenyang
192.168.15.6 ansible_connection=ssh ansible_user=root ansible_password=1

#检查连接
[root@m01 ~]# ansible webservers -m ping
172.16.1.8 | SUCCESS => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/bin/python”
},
“changed”: false,
“ping”: “pong”
}
172.16.1.9 | SUCCESS => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/bin/python”
},
“changed”: false,
“ping”: “pong”
}
172.16.1.7 | SUCCESS => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/bin/python”
},
“changed”: false,
“ping”: “pong”
}

场景二、基于密钥连接,需要先创建公钥和私钥,并下发公钥至被控端
[root@m01 ~]# ssh-keygen
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.7
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.8


[root@m01 ~]# cat hosts
#方式一、主机+端口+密钥
[webservers]
172.16.1.7
172.16.1.8

[root@m01 ~]# ansible webservers -m ping -i ./hosts
172.16.1.8 | SUCCESS => {
“changed”: false,
“ping”: “pong”
}
172.16.1.7 | SUCCESS => {
“changed”: false,
“ping”: “pong”
}


[root@m01 ~]# cat hosts
#方式二、别名+主机+端口+密钥
[webservers]
web01 ansible_ssh_host=172.16.1.7 ansible_ssh_port=22
web02 ansible_ssh_host=172.16.1.8

[root@m01 ~]# ansible webservers -m ping -i ./hosts
web02 | SUCCESS => {
“changed”: false,
“ping”: “pong”
}
web01 | SUCCESS => {
“changed”: false,
“ping”: “pong”
}

场景三、主机组使用方式
#1.定义两个组
[lbservers]
172.16.1.5
172.16.1.6

[webservers]
172.16.1.7
172.16.1.8

#2.servers组包括两个子组[lbservers,webserver]
[servers:children]
[lbservers]
[webserver]

#列出当前某个组有多少台主机
[root@m01 ~]# ansible lbservers -m ping -i ./hosts --list-hosts
hosts (1):
web01
[root@m01 ~]# ansible webservers -m ping -i ./hosts --list-hosts
hosts (1):
web02
[root@m01 ~]# ansible servers -m ping -i ./hosts --list-hosts
hosts (2):
web01
web02
[root@m01 ~]# ansible all -m ping -i ./hosts --list-hosts
hosts (3):
web03
web02
web01

Ansible相关工具

在这里插入图片描述
利用ansible实现管理的主要方式:
Ad-Hoc即利用ansible命令,主要用于临时命令使用场景
Ansible-playbook主要用于长期规划好的,大型项目的场景,需要有前期的规划过程

在这里插入图片描述

ansible-doc
此工具用来显示模块帮助,相当于man
格式:
ansible-doc[options][module…]
-l,–list # 列出可用模块
-s,–snippet # 显示指定模块的playbook片段

范例:
#列出所有模块
[root@localhost ~]# ansible-doc -l
#查看指定模块帮助用法
[root@localhost ~]# ansible-doc ping

[root@localhost ~]# ansible-doc -l | wc -l
3387

ansible

此工具通过ssh协议,实现对远程主机的配置管理、应用部署、任务执行等功能。
建议:使用此工具前,先配置ansible主控端能基于密钥认证的方式联系各个被管理节点

范例:利用sshpass批量实现基于key验证脚本(ip与密码在另一个文件)
[root@instance-gvpb80ao ~]# vim /etc/ssh/ssh_config
StrictHostKeyChecking no
[root@instance-gvpb80ao ~]# vim hosts.list
172.16.0.4
[root@instance-gvpb80ao ~]# vim push_ssh_key.sh
#!/bin/bash
#********************************************************************
#WelCome To Login
#Author:oldboy
#WX:Alvins0918
#Date:2021-05-20
#Description:老男孩IT教育Linux学院
#Copyright©:2021 All rightsreserved
#********************************************************************
rpm -q sshpass &> /dev/null || yum -y install sshpass
[ -f /root/.ssh/id_rsa ] || ssh-keygen -f /root/.ssh/id_rsa -P ‘’
export SSHPASS=1
while read IP;do
sshpass -e ssh-copy-id -o StrictHostKeyChecking=no $IP
done<hosts.list
Copy to clipboardErrorCopied

范例:实现基于key验证的脚本(ip与脚本在同一个文件)
[root@instance-gvpb80ao ~]# vim push_ssh_key.sh
#!/bin/bash
##
#WelCome To Login #
#Author:chenyang #
#WX:Alvins0918 #
#Date:2021-05-20 #
#Description:老男孩IT教育Linux学院 #
#Copyright©:2021 All rightsreserved #
#
#
IPLIST="
172.16.0.7
172.16.0.8
172.16.0.9"

rpm -q sshpass &> /dev/null || yum -y install sshpass
[ -f /root/.ssh/id_rsa ] || ssh-keygen -f /root/.ssh/id_rsa -P ‘’
export SSHPASS=1
for IP in $IPLIST;do
sshpass -e ssh-copy-id -o StrictHostKeyChecking=no $IP
done

Ansible格式
ansible [-m module_name] [-a args]

选项说明:
–version #显示版本
-m module #指定模块,默认为command
-v #详细过程 –vv -vvv更详细
–list-hosts #显示主机列表,可简写 --list
-C, --check #检查,并不执行
-T, --timeout=TIMEOUT #执行命令的超时时间,默认10s
-k, --ask-pass #提示输入ssh连接密码,默认Key验证
-u, --user=REMOTE_USER #执行远程执行的用户
-b, --become #代替旧版的sudo 切换
–become-user=USERNAME #指定sudo的runas用户,默认为root
-K, --ask-become-pass #提示输入sudo时的密码

ansible的Host-pattern
用于匹配被控制的主机的列表。
范例:

[root@instance-gvpb80ao ~]# cat /etc/ansible/hosts
172.16.0.4
[root@instance-gvpb80ao ~]# ansible all -m ping
172.16.0.4 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
通配符
[root@instance-gvpb80ao ~]# cat /etc/ansible/hosts
[私网]
172.16.0.4
[公网]
106.13.81.75

#第一种方式

[root@instance-gvpb80ao ~]# ansible "*" -m ping 
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
172.16.0.4 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
106.13.81.75 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

#第二种方式

[root@instance-gvpb80ao ~]# ansible "私网" -m ping
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
172.16.0.4 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

#第三种方式

[root@instance-gvpb80ao ~]# ansible '172*' -m ping
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
172.16.0.4 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

#第四种方式

[root@instance-gvpb80ao ~]# ansible '172.16.0.4 106.13.81.75' -m ping
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
172.16.0.4 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
106.13.81.75 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
或关系:

#在公网组里面或者在私网组里面

[root@instance-gvpb80ao ~]# ansible '公网:私网' -m ping
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
172.16.0.4 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
106.13.81.75 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
逻辑与:&

#在公网组并且在私网组

[root@instance-gvpb80ao ~]# cat /etc/ansible/hosts
[私网]
172.16.0.4
106.13.81.75
[公网]
106.13.81.75
[root@instance-gvpb80ao ~]# ansible '公网:&私网' -m ping
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
106.13.81.75 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
逻辑非:!(注意此时要用单引号)

#在公网组不在私网组

[root@instance-gvpb80ao ~]# cat /etc/ansible/hosts
[私网]
172.16.0.4
[公网]
106.13.81.75
[root@instance-gvpb80ao ~]# ansible '公网:!私网' -m ping
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
106.13.81.75 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

ansible ‘web:lb:!web01’ -m ping

正则表达式~匹配以某某开头的
[root@instance-gvpb80ao ~]# cat /etc/ansible/hosts
[private]
172.16.0.4
[public]
106.13.81.75
#以pu开头
[root@instance-gvpb80ao ~]# ansible ‘~pu’ -m ping
106.13.81.75 | SUCCESS => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/bin/python”
},
“changed”: false,
“ping”: “pong”
}

显示具体主机ip

[root@instance-gvpb80ao ~]# ansible private --list-hosts
hosts (1):
172.16.0.4

ansible命令执行过程
加载自己的配置文件,默认/etc/ansible/ansible.cfg
加载自己对应的模块文件,如:ping。通过ansible将模块或命令生成对应的临时py文件,并将该文件传输至远程服务器的对应执行用户$HOME/.ansible/tmp/ansible-tmp-数字/XXX.PY文件
给文件+x执行
执行并返回结果
删除临时py文件,退出

ansible的执行状态
[root@instance-gvpb80ao tmp]# grep -A 14 ‘[colors]’ /etc/ansible/ansible.cfg
[colors]
#highlight = white
#verbose = blue
#warn = bright purple
#error = red
#debug = dark gray
#deprecate = purple
#skip = cyan
#unreachable = red
#ok = green
#changed = yellow
#diff_add = green
#diff_remove = red
#diff_lines = cyan

绿色:执行成功并且不需要做改变的操作
黄色:执行成功并且对目标主机做变更
红色:执行失败

ansible使用范例
#以shanhe用户执行ping存活检测

[root@instance-gvpb80ao tmp]# ansible all -m ping -u shanhe -k
SSH password: 
106.13.81.75 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
172.16.0.4 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

#以wang sudo至root执行ping存活检测

[root@instance-gvpb80ao tmp]# ansible all -m ping -u shanhe -k -b
SSH password: 
106.13.81.75 | UNREACHABLE! => {
    "changed": false, 
    "msg": "Invalid/incorrect password: Permission denied, please try again.", 
    "unreachable": true
}
172.16.0.4 | UNREACHABLE! => {
    "changed": false, 
    "msg": "Invalid/incorrect password: Permission denied, please try again.", 
    "unreachable": true
}

#以shanhe sudo至chenyang用户执行ping存活检测

[root@instance-gvpb80ao tmp]# ansible all -m ping -u shanhe -k -b --become-user=chenyang
SSH password: 
172.16.0.4 | FAILED! => {
    "msg": "Missing sudo password"
}
106.13.81.75 | FAILED! => {
    "msg": "Missing sudo password"
}

#以shanhe sudo至root用户执行ls

[root@instance-gvpb80ao ~]# ansible all -m command -u shanhe -a 'ls /root' -b --become-user=root -k -K
SSH password: root用户密码
BECOME password[defaults to SSH password]: shanhe用户密码
172.16.0.4 | CHANGED | rc=0 >>
hosts.list
openvpn-user-crt.sh
push_ssh_key.sh
106.13.81.75 | CHANGED | rc=0 >>
hosts.list
openvpn-user-crt.sh
push_ssh_key.sh
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值