Ansible介绍与安装使用

导读:本篇文章讲解 Ansible介绍与安装使用,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

Ansible介绍与安装使用

1. 介绍Ansible

Ansible是一款自动运维工具,基于Python开发,集合了众多运维工具的优点。实现了批量系统配置、程序部署以及运行命令等功能。完全借助SSH来进行通信,易学易用、具有一致性、高可靠性和安全性、唯一局限就是无法配置逻机和虚拟机。

1.1 Ansible架构组成

在这里插入图片描述

  • (1)Host Inventory/主机清单:需要知道每台账户的账号密码。但是它是一个文件,不安全。于是可以使用免密登录,在absible生产密钥,传到主机里面去,以后就不需要生产用户名和密码。(相当于演戏的招人管理)
  • (2)Play books/剧本:palybooks是写剧本,ansible是导演,host inventory告诉你招到了哪些人来演戏。
  • (3)Core Modules:是核心模块;Custom Modules:定制模块(二次开发),觉得功能不够完善,加上新的功能
  • (4)Plugins(email、logging、other):插件,别人开发的。除了你现有的一些模块,它还会安装一些插件,完成一些不同的事情
  • (5)Connection Plugins:连接插件,用来连接主机的,负责和被监控端实现通信
  • (6)Public/Private Cloud:除了可以连接自己的主机以外,还可以连接公有云或者私有云上面的主机,使用ssh连接上了就能管理
  • (7)一句话概括:管理员通过Ansible里面的清单、模块、插件、剧本来管理里的服务器,服务器可能有网址、数据库、应用等等的服务器。(各种服务器)

免密登录

  • ssh-keygen -t rsa
  • ssh-copy-id root@IP**

1.2 Ansible 无需代理

Ansible 围绕无代理架构构建。通常而言,Ansible 通过 OpenSSH 或 WinRM 连接它所管理的主机并且运行任务,方法通常是将称为 Ansible 模块的小程序推送至这些主机。这些程序用于将系统置于需要的特定状态。在 Ansible 运行完其任务后,推送的所有模块都会被删除。

Ansible 不需要批准使用任何特殊代理,然后再部署到受管主机上。由于没有代理,也不需要额外的自定义安全基础架构。

Ansible 具有多个重要的优点:

  • 跨平台支持:Ansible 提供Linux、Windows、UNIX和网络设备的无代理支持,适用于物理、虚拟、云和容器环境。
  • 人类可读的自动化:Ansible Playbook采用YAML文本文件编写,易于阅读,有助于确保所有人都能理解它们的用途。
  • 完美描述应用:可以通过 Ansible Playbook进行每种更改,并描述和记录应用环境的每一个方面。
  • 轻松管理版本控制:Ansible Playbook和项目是纯文本。它们可以视作源代码,放在现有版本控制系统中。
  • 支持动态清单:可以从外部来源动态更新 Ansible 管理的计算机列表,随时获取所有受管服务器的当前正确列表,不受基础架构或位置的影响。
  • 编排可与其他系统轻松集成:能够利用环境中现有的 HP SA、Puppet、Jenkins、红帽卫星和其他系统,并且集成到 Ansible 工作流中。

1.3 Ansible 方式

Ansible 的设计宗旨是工具易用,自动化易写易读。所以在创建自动化时我们应追求简单化。

Ansible 自动化语言围绕简单易读的声明性文本文件来构建。正确编写的 Ansible Playbook可以清楚地记录你的工作自动化。

Ansible 是一种要求状态引擎。它通过表达你所希望系统处于何种状态来解决如何自动化IT部署的问题。Ansible 的目标是通过仅执行必要的更改,使系统处于所需的状态。试图将 Ansible 视为脚本语言并非正确的做法。

2 安装Ansible

2.1 控制节点

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

对控制节点的要求

  • 控制节点应是Linux或UNIX系统。不支持将Windows用作控制节点,但Windows系统可以是受管主机
  • 控制节点需要安装Python3(版本3.5或以上)
  • 如果操作系统是红帽8.0,Ansible 2.9可以自动使用 platform-python 软件包,该软件包支持使用Python的系统实用程序。你不需要从 AppStream安装python37或python27软件包。
 [root@localhost ~]# 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 'AppStream' is missing name in configuration, using id.
Installed Packages
platform-python.x86_64                                        3.6.8-23.el8                                        @anaconda

2.2 受管主机

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

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

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

yum module install python36

如果受管主机上启用了SELinux,还需要确保安装python3-libselinux软件包,然后才能使用与任何复制、文件或模板功能相关的模块。所以在工作的时候,应当把SELinux功能关闭。

2.3基于Windows的受管主机

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

Ansible有许多专门为Windows系统设计的模块。这些模块列在
官方安装链接:

大部分专门为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。

官方安装链接:

2.5 安装Ansible

/ / 1.过滤本地yum,查询有无ansible
[root@localhost ~]# dnf list all | grep ansible

/ / 2.提供YUM源
[root@localhost ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
[root@localhost ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo

/ / 3.安装epel-release源
[root@localhost ~]# yum -y install epel-release

/ / 4.查看epel-release源安装成功没有
[root@localhost ~]# ls /etc/yum.repos.d/
CentOS-Base.repo   epel-playground.repo  epel-testing-modular.repo  redhat.repo
epel-modular.repo  epel.repo             epel-testing.repo

/ / 5.清理yum缓存
[root@localhost ~]# yum clean all

/ / 6.过滤yum查询,有无ansible包
[root@localhost ~]# yum list all | grep ansible
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   

/ / 7.安装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.
Last metadata expiration check: 0:00:27 ago on Tue 13 Jul 2021 04:39:49 PM CST.
Dependencies resolved.
===========================================================================================================================
 Package                             Architecture    Version                                      Repository          Size
===========================================================================================================================
Installing:
 ansible                             noarch          2.9.23-1.el8                                 epel                17 M
(后面太长了,就不全部复制了)

/ / 8.检查ansible版本:
[root@localhost ~]# 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)]

/ / 9.通过使用setup模块验证localhost上的ansible_python_version
[root@localhost ~]# ansible -m setup localhost|grep ansible_python_version
        "ansible_python_version": "3.6.8",

3.Ansible的基础使用

3.1 ansible –version:检查版本

[root@localhost ~]# 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)]

3.2 ansible -m(模块)+ 模块名字:检查模块安装情况

[root@localhost ~]# ansible -m setup localhost|grep ansible_python_version
        "ansible_python_version": "3.6.8",
/ / 看本机信息
[root@localhost ~]# ansible -m setup localhost | head -10
localhost | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "192.168.91.128"
        ],
        "ansible_all_ipv6_addresses": [
            "fe80::5853:e154:a1c0:7fa2"
        ],
        "ansible_apparmor": {
            "status": "disabled"

/ / 过滤python版本
[root@localhost ~]# ansible -m setup localhost | grep python
            "_": "/usr/bin/python3.6"
        "ansible_python": {
            "executable": "/usr/bin/python3.6",
            "type": "cpython",
        "ansible_python_version": "3.6.8",
        "ansible_selinux_python_present": true,

3.3 使用静态清单指定受管主机

静态清单文件是指定Ansible目标受管主机的文本文件。可以使用多种不同的格式编写此文件,包括INI样式或YAML。

在最简单的形式中。INI样式的静态清单文件是受管主机的主机名或IP地址的列表,每行一个:

/ / 进入ansible目录下
[root@localhost ansible]# cd /etc/ansible/

/ / 查看文件
[root@localhost ansible]# ls
ansible.cfg  hosts  roles

/ / 创建一个inventory空文件
[root@localhost ansible]# touch inventory
[root@localhost ansible]# ls
ansible.cfg  hosts  inventory  roles

/ / 编辑ansible.cfg文件
[root@localhost ansible]# vim ansible.cfg
[root@localhost ansible]# cat ansible.cfg 
# config file for ansible -- https://ansible.com/
# ===============================================

# nearly all parameters can be overridden in ansible-playbook
# or with command line flags. ansible will read ANSIBLE_CONFIG,
# ansible.cfg in the current working directory, .ansible.cfg in
# the home directory or /etc/ansible/ansible.cfg, whichever it
# finds first

[defaults]

# some basic default values...

#inventory      = /etc/ansible/hosts
inventory      = /etc/ansible/inventory    复制上面并把hosts修改为inventory

/ / 编辑inventory,添加IP或者组
[root@localhost ansible]# vim inventory
[root@localhost ansible]# cat inventory p
192.168.91.129

[root@localhost ansible]# cat inventory 
[webservers]
192.168.91.129
192.168.91.130

3.4 验证清单

若有疑问,可使用 ansible 命令验证计算机是否存在于清单中:

/ / 输入ip地址单个查看(控制优先以第一个IP:192.168.91.129为准)
[root@localhost ansible]# ansible 192.168.91.129 --list-hosts
  hosts (1):
    192.168.91.129   

/ / 运行以下命令来列出指定组中的所有主机:
[root@localhost ansible]# ansible  webservers --list-hosts
  hosts (2):
    192.168.91.129
    192.168.91.130

如果清单中含有名称相同的主机和主机组,ansible 命令将显示警告并以主机作为其目标。主机组则被忽略。

应对这种情况的方法有多种,其中最简单的是确保主机组不使用与清单中主机相同的名称。

/ / 不要这么写就行了
[root@localhost ansible]# cat inventory 
192.168.91.129

[webservers]
192.168.91.129
192.168.91.130

3.5 覆盖清单的位置

/etc/ansible/hosts文件被视为系统的默认静态清单文件。通常的做法是不使用该文件,而是在Ansible配置文件中为清单文件定义一个不同的位置。

/ / 编辑ansible.cfg文件
[root@localhost ansible]# vim ansible.cfg
[root@localhost ansible]# cat ansible.cfg 
# config file for ansible -- https://ansible.com/
# ===============================================

# nearly all parameters can be overridden in ansible-playbook
# or with command line flags. ansible will read ANSIBLE_CONFIG,
# ansible.cfg in the current working directory, .ansible.cfg in
# the home directory or /etc/ansible/ansible.cfg, whichever it
# finds first

[defaults]

# some basic default values...

#inventory      = /etc/ansible/hosts
inventory      = /etc/ansible/inventory    复制上面并把hosts修改为inventory     这个就是为清单文件定义一个不同的位置

3.6 构建Ansible清单

修改默认清单文件/etc/ansible/hosts添加以下内容:

/ / 用vim进行编译
[root@localhost ansible]# cat inventory 
192.168.91.131

[webservers]
192.168.91.129

使用以下命令列出不属于任何组的受管主机:

[root@localhost ansible]# ansible ungrouped --list-hosts
  hosts (1):
    192.168.91.131

使用以下命令列出属于某组的受管主机:

[root@localhost ansible]# ansible webservers --list-hosts
  hosts (1):
    192.168.91.129

使用以下命令列出默认清单文件中的所有受管主机:

[root@localhost ansible]# ansible all --list-hosts
  hosts (2):
    192.168.91.131
    192.168.91.129

3.7 自定义清单文件

在/etc/ansible/目录中,创建一个名为inventory的自定义静态清单文件。
服务器清单规格

主机IP 用途 位置 运行环境
192.168.91.129 web服务器 北京 测试
192.168.91.130 web服务器 上海 生产
192.168.91.131 数据库服务器 上海 生产

编辑/etc/ansible/inventory文件,将上表中所列出的主机加入受管主机序列。

[root@localhost ansible]# vim inventory
[root@localhost ansible]# cat inventory 
[webservers]
192.168.91.129 ansible_user=root ansible_password=your_pass  这里写你主机的账户和密码

/ / 账号密码给好了就可以ping你的主机了
[root@localhost ansible]# ansible all -m ping

ansible -i +绝对路径 –list-hosts 列出所有受管主机:

[root@localhost ansible]# ansible all -i /etc/ansible/inventory --list-hosts
  hosts (1):
    192.168.91.129

ansible +组名 -i +绝对路径 –list-hosts命令列出webservers组中的所有受管主机:

[root@localhost ansible]# ansible webservers -i /etc/ansible/inventory --list-hosts
  hosts (1):
    192.168.91.129

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之家整理,本文链接:https://www.bmabk.com/index.php/post/5656.html

(0)
小半的头像小半

相关推荐

极客之家——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!