赞
踩
ansible自动化运维(一)——基本架构及安装使用
ansible是一种自动化运维工具,基于paramiko开发的,并且基于模块化工作,Ansible是一种集成IT系统的配置管理、应用部署、执行特定任务的开源平台。
它是基于python语言,由Paramiko和PyYAML两个关键模块构建。集合了众多运维工具的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。
ansible是基于模块工作的,本身没有批量部署的能力,真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架.ansible不需要在远程主机上安装client/agents,因为它们是基于ssh来和远程主机通讯的。
ansible被定义为配置管理工具,配置管理工具通常具有以下功能:
项目 | puppet | SaltStack | Ansible |
---|---|---|---|
开发语言 | Ruby | Python | Python |
是否有客户端 | 有 | 有 | 无 |
是否支持二次开发 | 不支持 | 支持 | 支持 |
服务器与远程机器是否相互验证 | 是 | 是 | 是 |
服务器与远程机器的通信是否加密 | 是,标准的SSL协议 | 是,使用AES加密 | 是,使用OpenSSH |
平台支持 | AIX , BSD, HP-UX, Linux , Mac OSX , Solaris, Windows | BSD, Linux , Mac OS X , Solaris, Windows | AIX , BSD , HP-UX , Linux , Mac OS X , Solaris |
是否提供Web UI | 提供 | 提供 | 提供,但是前提是商务版本 |
配置文件 | Ruby 语法模式 | YAML | YAML |
命令行执行 | 不支持,但是可以通过配置模块实现 | 支持 | 支持 |
一、ansible基础架构
ansible系统由控制主机和被管理主机组成,控制主机不支持windows平台
1.ansible特点
部署简单, 只需要在控制主机上部署ansible环境,被控制端上只要求安装ssh和python 2.5以上版本(python3),这个对于类unix系统来说相当与无需配置。
ansible程序目录结构:
配置文件: /etc/ansible/
执行文件目录: /usr/bin/
二、ansible任务执行
1.ansible工作原理:
2.ansible主要组成部分功能说明:
3.ansible注意事项:
4.ansible任务执行模式
Ansible任务执行模式分为以下两种:
5.ansible执行流程:
一台控制主机:192.168.58.30
一台被管理主机:192.168.58.40
安装要求:
控制服务器:需要安装 Python2.6/2.7
管理服务器:需要安装 Python2.4 以上版本,若低于 Python2.5 需要安装 pythonsimplejson; 若启用了 selinux,则需要安装 libselinux-python。
[root@localhost ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 2595 100 2595 0 0 3639 0 --:--:-- --:--:-- --:--:-- 3639 [root@localhost ~]# yum -y install epel-release Updating Subscription Management repositories. Unable to read consumer identity This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register. 上次元数据过期检查:0:04:14 前,执行于 2021年07月13日 星期二 16时17分02秒。 依赖关系解决。 =========================================================================================================== 软件包 架构 版本 仓库 大小 =========================================================================================================== 安装: epel-release noarch 8-11.el8 extras 24 k 事务概要 =========================================================================================================== 安装 1 软件包 总下载:24 k 安装大小:35 k 下载软件包: epel-release-8-11.el8.noarch.rpm 57 kB/s | 24 kB 00:00 ----------------------------------------------------------------------------------------------------------- 总计 56 kB/s | 24 kB 00:00 运行事务检查 事务检查成功。 运行事务测试 事务测试成功。 运行事务 准备中 : 1/1 安装 : epel-release-8-11.el8.noarch 1/1 运行脚本: epel-release-8-11.el8.noarch 1/1 验证 : epel-release-8-11.el8.noarch 1/1 Installed products updated. 已安装: epel-release-8-11.el8.noarch 完毕! [root@localhost ~]# [root@localhost ~]# cd /etc/yum.repos.d/ [root@localhost yum.repos.d]# ls CentOS-Base.repo epel-playground.repo epel-testing-modular.repo redhat.repo epel-modular.repo epel.repo epel-testing.repo
1.控制节点
[root@localhost ~]# yum -y install ansible Updating Subscription Management repositories. Unable to read consumer identity This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register. Extra Packages for Enterprise Linux Modular 8 - x86_64 55 kB/s | 663 kB 00:11 Extra Packages for Enterprise Linux 8 - x86_64 339 kB/s | 10 MB 00:30 上次元数据过期检查:0:00:04 前,执行于 2021年07月13日 星期二 16时24分47秒。 依赖关系解决。 =========================================================================================================== 软件包 架构 版本 仓库 大小 =========================================================================================================== 安装: ansible noarch 2.9.23-1.el8 epel 17 M 升级: platform-python-pip noarch 9.0.3-19.el8 base 1.7 M platform-python-setuptools noarch 39.2.0-6.el8 base 632 k 安装依赖关系: libsodium x86_64 1.0.18-2.el8 epel 162 k python3-babel noarch 2.5.1-5.el8 验证 : platform-python-pip-9.0.3-16.el8.noarch 17/19 验证 : platform-python-setuptools-39.2.0-6.el8.noarch 18/19 验证 : platform-python-setuptools-39.2.0-5.el8.noarch 19/19 Installed products updated. 已升级: platform-python-pip-9.0.3-19.el8.noarch platform-python-setuptools-39.2.0-6.el8.noarch 已安装: ansible-2.9.23-1.el8.noarch libsodium-1.0.18-2.el8.x86_64 python3-babel-2.5.1-5.el8.noarch python3-bcrypt-3.1.6-2.el8.1.x86_64 python3-jinja2-2.10.1-2.el8_0.noarch python3-jmespath-0.9.0-11.el8.noarch python3-markupsafe-0.23-19.el8.x86_64 python3-paramiko-2.4.3-1.el8.noarch python3-pip-9.0.3-19.el8.noarch python3-pyasn1-0.3.7-6.el8.noarch python3-pynacl-1.3.0-5.el8.x86_64 python3-pytz-2017.2-9.el8.noarch python3-setuptools-39.2.0-6.el8.noarch python36-3.6.8-2.module_el8.4.0+790+083e3d81.x86_64 sshpass-1.06-9.el8.x86_64 完毕!
2.被控节点
//安装网络yum源 [root@localhost ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 2595 100 2595 0 0 3639 0 --:--:-- --:--:-- --:--:-- 3639 //安装epel源 [root@localhost ~]# yum -y install epel-release Updating Subscription Management repositories. Unable to read consumer identity This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register. Repository AppStream is listed more than once in the configuration CentOS-8 - Base - mirrors.aliyun.co 1.3 MB/s | 3.6 MB 00:02 CentOS-8 - Extras - mirrors.aliyun. 20 kB/s | 9.8 kB 00:00 CentOS-8 - AppStream - mirrors.aliy 924 kB/s | 8.1 MB 00:08 ..... //安装ansible [root@localhost ~]# yum -y install ansible Updating Subscription Management repositories. Unable to read consumer identity This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register. Repository AppStream is listed more than once in the configuration Extra Packages for Enterprise Linux 172 kB/s | 663 kB 00:03 Extra Packages for Enterprise Linux 1.7 MB/s | 10 MB 00:05 ....... 已安装: ansible-2.9.23-1.el8.noarch libsodium-1.0.18-2.el8.x86_64 python3-babel-2.5.1-5.el8.noarch python3-bcrypt-3.1.6-2.el8.1.x86_64 python3-jinja2-2.10.1-2.el8_0.noarch python3-jmespath-0.9.0-11.el8.noarch python3-markupsafe-0.23-19.el8.x86_64 python3-paramiko-2.4.3-1.el8.noarch python3-pip-9.0.3-19.el8.noarch python3-pyasn1-0.3.7-6.el8.noarch python3-pynacl-1.3.0-5.el8.x86_64 python3-pytz-2017.2-9.el8.noarch python3-pyyaml-3.12-12.el8.x86_64 python3-setuptools-39.2.0-5.el8.noarch python36-3.6.8-2.module_el8.4.0+790+083e3d81.x86_64 sshpass-1.06-9.el8.x86_64 完毕! [root@localhost ~]#
3.1构建ansible清单
[root@localhost ~]# cd /etc/ansible/ #进入到ansible配置文件目录中 [root@localhost ansible]# ls ansible.cfg hosts roles [root@localhost ansible]# [root@localhost ansible]# touch inventory [root@localhost ansible]# ls ansible.cfg hosts inventory roles //添加受管主机 [root@localhost ansible]# vim inventory 192.168.58.30 [webservers] 192.168.58.40 //使用命令列出默认清单文件中所有受管主机 [root@localhost ansible]# ansible all --list-hosts hosts (2): 192.168.58.30 192.168.58.40 //使用以下命令列出不属于任何组的受管主机 [root@localhost ansible]# ansible ungrouped --list-hosts hosts (1): 192.168.58.40 //使用以下命令列出属于某组的受管主机 [root@localhost ansible]# ansible webservers --list-hosts hosts (1): 192.168.58.30 //添加用户 密码 [root@localhost ansible]# vim inventory [webs] 192.168.58.30 ansible_user=root ansible_password=1 //ping 主机 [root@localhost ansible]# ansible all -m ping 192.168.58.30 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": false, "ping": "pong" } // 做ssh 互信 [root@localhost ansible]# ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: SHA256:ZX7EsseGIyy6NzFuxt2irfhyJc7lAy+ZqZ2bxSfP3U root@localhost.localdomain The key's randomart image is: +---[RSA 3072]----+ | .=.+. o | | .. O .= . | | .+ +o B | | ..*+.+ o | | =+SO.oo | | =. X B.. . E| | o .= = = . . | | . . .o . o | | .. . | +----[SHA256]-----+ [root@localhost ansible]# ssh-copy-id root@192.168.58.30 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@192.168.58.30's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@192.168.58.30'" and check to make sure that only the key(s) you wanted were added. //删除用户和密码 ping主机 [root@localhost ansible]# vim inventory [webs] 192.168.58.30 [root@localhost ansible]# ansible all -m ping 192.168.58.30 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": false, "ping": "pong" } //移动清单文件ping主机需要指定移动到的目录 [root@localhost ansible]# mv inventory /opt/ [root@localhost ansible]# cd /opt/ [root@localhost opt]# ls inventory [root@localhost opt]# ansible all -m ping [WARNING]: Unable to parse /etc/ansible/inventory as an inventory source [WARNING]: No inventory was parsed, only implicit localhost is available [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all' // 这个时候ping不通需要指定 [root@localhost opt]# ansible all -i /opt/inventory -m ping 192.168.58.30 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": false, "ping": "pong" } // 指定后可以ping通
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。