Ansible自动化工具如何安装配置

发布时间:2021-10-25 13:59:18 作者:小新
来源:亿速云 阅读:120

这篇文章主要为大家展示了“Ansible自动化工具如何安装配置”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Ansible自动化工具如何安装配置”这篇文章吧。

市面上有很多自动化工具。我可以举几个例子,例如 Puppet、Chef、CFEngine、Foreman、Katello、Saltstock、Space Walk,它们被许多组织广泛使用。

自动化工具可以做什么?

自动化工具可以自动执行例行任务,无需人工干预,从而使 Linux 管理员的工作变得更加轻松。这些工具允许用户执行配置管理,应用程序部署和资源调配。

Ansible自动化工具如何安装配置

为什么喜欢 Ansible?

Ansible 是一种无代理的自动化工具,使用 SSH 执行所有任务,但其它工具需要在客户端节点上安装代理。

什么是 Ansible?

Ansible 是一个开源、易于使用的功能强大的 IT 自动化工具,通过 SSH 在客户端节点上执行任务。

它是用 Python 构建的,这是当今世界上最流行、最强大的编程语言之一。两端都需要使用 Python 才能执行所有模块。

它可以配置系统、部署软件和安排高级 IT 任务,例如连续部署或零停机滚动更新。你可以通过 Ansible 轻松执行任何类型的自动化任务,包括简单和复杂的任务。

在开始之前,你需要了解一些 Ansible 术语,这些术语可以帮助你更好的创建任务。

Ansible 如何工作?

Ansible 通过在客户端节点上推送称为 ansible 模块的小程序来工作,这些模块临时存储在客户端节点中,通过 JSON 协议与 Ansible 服务器进行通信。

Ansible 通过 SSH 运行这些模块,并在完成后将其删除。

模块是用 Python 或 Perl 等编写的一些脚本。

Ansible自动化工具如何安装配置

控制节点,用于控制剧本的全部功能,包括客户端节点(主机)。

测试环境

此环境包含一个控制节点(server.2g.lab)和三个受控节点(node1.2g.labnode2.2g.labnode3.2g.lab),它们均在虚拟环境中运行,操作系统分别为:

System PurposeHostnameIP AddressOS
Ansible Control Nodeserver.2g.lab192.168.1.7Manjaro 18
Managed Node1node1.2g.lab192.168.1.6CentOS7
Managed Node2node2.2g.lab192.168.1.5CentOS8
Managed Node3node3.2g.lab192.168.1.9Ubuntu 18.04
User: daygeek   

前置条件

如何在控制节点上安装 Ansible

对于 Fedora/RHEL 8/CentOS 8 系统,使用 DNF 命令 来安装 Ansible。

注意:你需要在 RHEL/CentOS 系统上启用 EPEL 仓库,因为 Ansible 软件包在发行版官方仓库中不可用。

$ sudo dnf install ansible

对于 Debian/Ubuntu 系统,使用 APT-GET 命令 或 APT 命令 来安装 Ansible。

配置下面的 PPA 以便在 Ubuntu 上安装最新稳定版本的 Ansible。

$ sudo apt update$ sudo apt install software-properties-common$ sudo apt-add-repository --yes --update ppa:ansible/ansible$ sudo apt install ansible

对于 Debian 系统,配置以下源列表:

$ echo "deb http://ppa.launchpad.net/ansible/ansible/ubuntu trusty main" | sudo tee -a /etc/apt/sources.list.d/ansible.list$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 93C4A3FD7BB9C367$ sudo apt update$ sudo apt install ansible

对于 Arch Linux 系统,使用 Pacman 命令 来安装 Ansible:

$ sudo pacman -S ansible

对于 RHEL/CentOS 系统,使用 YUM 命令 来安装 Ansible:

$ sudo yum install ansible

对于 openSUSE 系统,使用 Zypper 命令 来安装 Ansible:

$ sudo zypper install ansible

或者,你可以使用 Python PIP 包管理工具 来安装:

$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py$ sudo python get-pip.py$ sudo pip install ansible

在控制节点上检查安装的 Ansible 版本:

$ ansible --version ansible 2.9.2  config file = /etc/ansible/ansible.cfg  configured module search path = ['/home/daygeek/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']  ansible python module location = /usr/lib/python3.8/site-packages/ansible  executable location = /usr/bin/ansible  python version = 3.8.1 (default, Jan  8 2020, 23:09:20) [GCC 9.2.0]

如何在受控节点上安装 Python?

使用以下命令在受控节点上安装 python:

$ sudo yum install -y python$ sudo dnf install -y python$ sudo zypper install -y python$ sudo pacman -S python$ sudo apt install -y python

如何在 Linux 设置 SSH 密钥身份验证(无密码身份验证)

使用以下命令创建 ssh 密钥,然后将其复制到远程计算机。

$ ssh-keygen$ ssh-copy-id daygeek@node1.2g.lab$ ssh-copy-id daygeek@node2.2g.lab$ ssh-copy-id daygeek@node3.2g.lab

具体参考这篇文章《在 Linux 上设置 SSH 密钥身份验证(无密码身份验证)》。

如何创建 Ansible 主机清单

在 /etc/ansible/hosts 文件中添加要管理的节点列表。如果没有该文件,则可以创建一个新文件。以下是我的测试环境的主机清单文件:

$ sudo vi /etc/ansible/hosts [web]node1.2g.labnode2.2g.lab [app]node3.2g.lab

让我们看看是否可以使用以下命令查找所有主机。

$ ansible all --list-hosts  hosts (3):   node1.2g.lab   node2.2g.lab   node3.2g.lab

对单个组运行以下命令:

$ ansible web --list-hosts  hosts (2):   node1.2g.lab   node2.2g.lab

如何使用点对点命令执行任务

一旦完成主机清单验证检查后,你就可以上路了。干的漂亮!

语法:

ansible [pattern] -m [module] -a "[module options]" Details:========ansible: A commandpattern: Enter the entire inventory or a specific group-m [module]: Run the given module name-a [module options]: Specify the module arguments

使用 Ping 模块对主机清单中的所有节点执行 ping 操作:

$ ansible all -m ping node3.2g.lab | SUCCESS => {    "ansible_facts": {        "discovered_interpreter_python": "/usr/bin/python"    },    "changed": false,    "ping": "pong"}node1.2g.lab | SUCCESS => {    "ansible_facts": {        "discovered_interpreter_python": "/usr/bin/python"    },    "changed": false,    "ping": "pong"}node2.2g.lab | SUCCESS => {    "ansible_facts": {        "discovered_interpreter_python": "/usr/libexec/platform-python"    },    "changed": false,    "ping": "pong"}

所有系统都返回了成功,但什么都没有改变,只返回了 pong 代表成功。

你可以使用以下命令获取可用模块的列表。

$ ansible-doc -l

当前有 3387 个内置模块,它们会随着 Ansible 版本的递增而增加:

$ ansible-doc -l | wc -l3387

使用 command 模块对主机清单中的所有节点执行命令:

$ ansible all -m command -a "uptime" node3.2g.lab | CHANGED | rc=0 >> 18:05:07 up  1:21,  3 users,  load average: 0.12, 0.06, 0.01node1.2g.lab | CHANGED | rc=0 >> 06:35:06 up  1:21,  4 users,  load average: 0.01, 0.03, 0.05node2.2g.lab | CHANGED | rc=0 >> 18:05:07 up  1:25,  3 users,  load average: 0.01, 0.01, 0.00

对指定组执行 command 模块。

检查 app 组主机的内存使用情况:

$ ansible app -m command -a "free -m" node3.2g.lab | CHANGED | rc=0 >>              total        used        free      shared  buff/cache   availableMem:           1993        1065          91           6         836         748Swap:          1425           0        1424

要对 web 组运行 hostnamectl 命令,使用以下格式:

$ ansible web -m command -a "hostnamectl" node1.2g.lab | CHANGED | rc=0 >>   Static hostname: CentOS7.2daygeek.com         Icon name: computer-vm           Chassis: vm        Machine ID: 002f47b82af248f5be1d67b67e03514c           Boot ID: dc38f9b8089d4b2d9304e526e00c6a8f    Virtualization: kvm  Operating System: CentOS Linux 7 (Core)       CPE OS Name: cpe:/o:centos:centos:7            Kernel: Linux 3.10.0-957.el7.x86_64      Architecture: x86-64node2.2g.lab | CHANGED | rc=0 >>   Static hostname: node2.2g.lab         Icon name: computer-vm           Chassis: vm        Machine ID: e39e3a27005d44d8bcbfcab201480b45           Boot ID: 27b46a09dde546da95ace03420fe12cb    Virtualization: oracle  Operating System: CentOS Linux 8 (Core)       CPE OS Name: cpe:/o:centos:centos:8            Kernel: Linux 4.18.0-80.el8.x86_64      Architecture: x86-64

以上是“Ansible自动化工具如何安装配置”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. ansible——组织变量
  2. ansible笔记——安装配置ansible

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ansible

上一篇:Bash中的尖括号怎么用

下一篇:Python爬虫经常会被封的原因是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》