Ansible介绍与安装

Ansible介绍与安装

Ansible介绍与安装

1. 介绍Ansible

1.1 什么是Ansible?  

1.2 Ansible的优点

2. 安装 Ansible

2.1 控制节点

2.2 受管主机

 2.3 基于Windows的受管主机

2.4 受管网络设备

2.5 安装Ansible 


1. 介绍Ansible

1.1 什么是Ansible?  

Ansible是新出现的自动化运维工具,Ansible是一个配置管理和应用部署工具,基于python开发,集合了众多运维工具(pupet,cfengine,chef,func,fabric,saltstack)的优点。实现了批量系统配置,批量程序部署,批量运行命令等功能。Ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是Ansible所运行的模块,Ansible只是提供了一种框架。

Ansible基本架构图:

上图为Ansible的基本架构,从上图可以了解到其由以下部分组成:

  •  核心: Ansible 
  •  核心模块(Core Modules) :这些都是Ansible自带的模块。
  •  扩展模块(Custom Modules) :如果核心模块不足以完成某种功能,可以添加扩展模块。
  •  插件(Plugins) :完成模块功能的补充。
  •  剧本(Playbooks) : Ansible的任务配置文件,将多个任务定义在剧本中,由Ansible自动执   行。
  •  连接插件(Connectior Plugins) : Ansible基于连接插件连接到各个主机上,虽然Ansible是使   用ssh连接到各个主机的,但是它还支持其他的连接方法,所以需要有连接插件。
  •  主机群(Host lnventory) :定义Ansible管理的主机。

1.2 Ansible的优点

  1. Ansible融合了众多老牌运维工具的优点,基本上pubbet和saltsatck能实现的功能,Ansible都可以显现。
  2. 轻量级,无需在客户端上安装agent,更新时,只需要在操作机上进行一次更新即可。
  3. Ansible是一个轻量级的工具,Ansible不需要启动服务,仅仅只是一个工具,可以轻松实现分布式扩展。
  4. 批量任务执行可以写成脚本,而且不用分发的远程就可以执行。
  5. Ansible是一致性,高可靠性,安全性设计的轻量级自动化工具。
  6. 使用python编写,维护更加简单。

2. 安装 Ansible


2.1 控制节点

Ansible 易于安装。 Ansible 软件只需要安装到要运行它的一个(或多个)控制节点上。由 Ansible管理的主机不需要安装 Ansible。

对控制节点的要求:

  • 控制节点应是Linux或UNIX系统。不支持将Windows用作控制节点,但Windows系统可以是受管主机。
  • 控制节点需要安装Python3(版本3.5或以上)或Python2(版本2.7或以上)。

如果操作系统是红帽8.0,Ansible 2.9可以自动使用 platform-python 软件包,该软件包支持使用Python的系统实用程序。你不需要从 AppStream安装python37或python27软件包。

[root@yibie ~]# yum list installed platform-python
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 BaseOS is listed more than once in the configuration
Repository 'AppStream' is missing name in configuration, using id.
Repository AppStream is listed more than once in the configuration
已安装的软件包
platform-python.x86_64                  3.6.8-23.el8                   @anaconda
[root@yibie ~]# 

2.2 受管主机

Ansible的一大优点是受管主机不需要安装特殊代理。Ansible控制节点使用标准的网络协议连接受管主机,从而确保系统处于指定的状态。

受管主机可能要满足一些要求,具体取决于控制节点连接它们的方式以及它们要运行的模块。

Linux和UNIX受管主机需要安装有Python2(版本2.6或以上)或Python3(版本3.5或以上),这样才能运行大部分的模块。

对于红帽8,可以启用并安装python36应用流(或python27应用流)

yum module install python36

 2.3 基于Windows的受管主机

Ansible有许多专门为Windows系统设计的模块。这些模块列在https://docs.ansible.com/ansible/latest/modules/list_of_windows_modules.html部分中。

大部分专门为Windows受管主机设计的模块需要在受管主机上安装PowerShell 3.0或更高版本,而不是安装Python。此外,受管主机也需要配置PowerShell远程连接。Ansible还要求至少将.NET Framework 4.0或更高版本安装在Windows受管主机上。

2.4 受管网络设备

Ansible还可以配置受管网络设备,例如路由器和交换机。Ansible包含大量专门为此目的而设计的模块。其中包括对Cisco IOS、IOS XR和NX-OS的支持;Juniper Junos;Arsta EOS;以及基于VyOS的网络设备等。

我们可以使用为服务器编写playbook时使用的相同基本技术为网络设备编写Ansible Playbook。由于大多数网络设备无法运行Python,因此Ansible在控制节点上运行网络模块,而不是在受管主机上运行。特殊连接方法也用于与网络设备通信,通常使用SSH上的CLI、SSH上的XML或HTTP(S)上的API。

更多受管网络设备说明,需要用到时请查阅官方文档链接https://docs.ansible.com/ansible/latest/modules/list_of_network_modules.html

2.5 安装Ansible 

提供yum源

 curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo

[root@yibie ~]# 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  15175      0 --:--:-- --:--:-- --:--:-- 15264
[root@yibie ~]# 

先安装ansible的源,并查看是否安装成功

[root@yibie ~]# yum 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
Repository BaseOS is listed more than once in the configuration
······
                                                                                 
已安装:
  epel-release-8-11.el8.noarch                                                                        

完毕!

[root@yibie ~]# yum list all | grep ansible
Repository AppStream is listed more than once in the configuration
Repository BaseOS is listed more than once in the configuration
Repository 'AppStream' is missing name in configuration, using id.
Repository AppStream is listed more than once in the configuration
ansible.noarch                                                    2.9.23-1.el8                                      epel      
ansible-collection-ansible-posix.noarch                           1.2.0-1.el8                                       epel      
ansible-collection-community-general.noarch                       3.1.0-2.el8                                       epel      
ansible-doc.noarch                                                2.9.23-1.el8                                      epel      
ansible-freeipa.noarch                                            0.3.2-2.el8                                       AppStream 
ansible-test.noarch                                               2.9.23-1.el8                                      epel      
centos-release-ansible-29.noarch                                  1-2.el8                                           extras    
vim-ansible.noarch                                                3.2-1.el8                                         epel      
[root@yibie ~]# 

安装ansible,查看版本信息

[root@yibie ~]# 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
Repository BaseOS is listed more than once in the configuration
Repository 'AppStream' is missing name in configuration, using id.
Repository AppStream is listed more than once in the configuration
上次元数据过期检查:0:01:02 前,执行于 2021年07月14日 星期三 02时59分31秒。
依赖关系解决。
=========================================================================================
(过程省略)
Installed products updated.

已安装:
  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-pyasn1-0.3.7-6.el8.noarch         
  python3-pynacl-1.3.0-5.el8.x86_64           sshpass-1.06-9.el8.x86_64                 

完毕!

[root@yibie ~]# ansible --version
ansible 2.9.23
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.8 (default, Dec  5 2019, 15:45:45) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]
[root@yibie ~]# 

创建用户

[root@yibie ~]# ansible localhost -m ping
localhost | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
[root@yibie ~]# ansible localhost -m user -a "name=yibie"
localhost | CHANGED => {
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 1001,
    "home": "/home/yibie",
    "name": "yibie",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 1001
}
[root@yibie ~]# 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值