在 Debian 11/10 上安装和使用 Ansible |
Ansible 是一款免费、开源的、用 Python 编写的流行的自动化和配置管理工具之一。在技术领域,由于环境复杂以及系统管理员和开发人员需要快速扩展,自动化的概念已在许多组织中得到高度采用。这个概念使得 Ansible、Puppet、Chef、Foreman、Katello 和 CFEngine 等工具找到了用途。从自动化工具的名称来看,ansible 是任何 IT 组织管理基于 UNIX 系统的首选工具,因为它具有以下功能:
- 免费和开源
- 易于设置和使用
- 它非常灵活,因为无论部署在何处,它都允许在整个环境上进行 orcherstartion。
- 高效,无需安装其他软件或防火墙端口
- 它功能强大,可用于对复杂的 IT 工作流程进行建模
- 安全与合规性
为了进行任务编排,需要在其中一个节点上安装 ansible。管理节点称为控制节点。该节点将具有 Ansible Playbook 文件。这是一个 YAML 文件,其中包含用户想要在通常称为托管节点的特定机器上执行的步骤。
本指南演示如何在 Debian 11/10 上安装和使用 Ansible。
先决条件
对于本指南,您将需要以下内容:
- 3 台服务器 – 带有 Debian11|10 控制节点
- 在所有服务器上具有 sudo 权限的用户
我的设置如下。
步骤 1 – 在 Debian 上安装 Ansible
在本指南中,我将介绍在 Debian 11/10 控制节点上安装 Ansible 的几种方法。
- Debian 默认上游存储库。
- Ubuntu APT 存储库
- 使用pip(Python包管理器)
在本指南中,我将使用 vim 文本编辑器创建和编辑各种文件
sudo apt update
sudo apt install vim
1a) 使用 PIP 在 Debian 上安装 Ansible
Ansible 也可以在 PIP(Python 包管理器)上找到。但首先,我们需要将 Python 和 PIP 安装到您的系统中。
sudo apt-get install python3 python3-pip -y
然后使用 PIP 安装 Ansible,如下所示。
sudo pip3 install ansible
检查安装的版本
$ ansible --version
ansible [core 2.14.6]
config file = None
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.9/dist-packages/ansible
ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/local/bin/ansible
python version = 3.9.2 (default, Feb 28 2021, 17:03:44) [GCC 10.2.1 20210110] (/usr/bin/python3)
jinja version = 3.1.2
libyaml = True
1b) 从 APT 存储库在 Debian 上安装 Ansible。
Ansible 存在于默认 Debian 存储库中,但可用版本不是最新的。使用这种方法安装 Ansible 非常简单,因为它不需要复杂的步骤。
首先,更新您的系统包索引。
sudo apt update
然后继续在 Debian 11/10 上安装 Ansible,如下所示。
sudo apt install ansible
通过检查安装的 Ansible 版本来确认您的安装。
$ which ansible
/usr/bin/ansible
$ ansible --version
1c) 从 Ubuntu APT 存储库在 Debian 上安装 Ansible。
在这种方法中,我们需要将 PPA 存储库添加到 Debian 11/10 系统中。首先,安装一些需要的依赖项,如下所示。
sudo apt-get install gnupg2 curl wget -y
安装依赖项后,现在添加 PPA 存储库,如下所示。
sudo vim /etc/apt/sources.list
在文件中,添加以下行
deb http://ppa.launchpad.net/ansible/ansible/ubuntu bionic main
将 Ansible GPG 密钥添加到您的 Debian 11/10 系统,如下所示。
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 93C4A3FD7BB9C367
示例输出:
Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)).
Executing: /tmp/apt-key-gpghome.SzIqXbWidp/gpg.1.sh --keyserver keyserver.ubuntu.com --recv-keys 93C4A3FD7BB9C367
gpg: key 93C4A3FD7BB9C367: public key "Launchpad PPA for Ansible, Inc." imported
gpg: Total number processed: 1
gpg: imported: 1
现在更新您的 APT 包索引并安装 Ansible,如下所示。
sudo apt-get update
sudo apt-get install ansible -y
检查已安装的 Ansible 版本:
ansible --version
步骤 2 – 创建 Ansible 主机清单文件
在控制节点上安装 Ansible 后,会自动创建 /etc/hosts 文件。在此文件中,我们需要添加托管节点。您还可以在主目录中创建自己的清单文件,如下所示
在该文件中,添加您的托管节点,如下所示。
[Node1]
192.168.100.118 ansible_ssh_user=your_username
[Node2]
192.168.100.119 ansible_ssh_user=your_username
请记住将 your_username 替换为要在托管主机上使用的用户名。然后在控制节点和受管节点之间创建 SSH 指纹密钥
从您的控制节点配置 SSH 密钥
ssh-keygen -t rsa
只需按 Enter 直到结束。
然后复制受管节点的公钥,如下所示
ssh-copy-id -i ~/.ssh/id_rsa.pub your_username@192.168.100.118
ssh-copy-id -i ~/.ssh/id_rsa.pub your_username@192.168.100.119
这允许您的控制节点无需密码身份验证即可管理节点。
第 3 步 – 使用 Ansible
借助 ansible,人们可以使用以下语法从控制节点发出命令来管理节点。
ansible -i [inventory_file] -m [module] [host]
现在测试受管节点是否已添加且可访问。
sudo ansible -i ~/.hosts -m ping all
样本输出。
对清单文件中的特定主机执行 Ping 操作,如下所示。
sudo ansible -i ~/.hosts -m ping Node1
###OR
sudo ansible -i ~/.hosts -m ping Node2
您还可以使用 free-m 命令检查可用空间,如下所示
sudo ansible -i ~/.hosts -m shell -a "free -m" Node1
示例输出:
您还可以使用df-h命令
sudo ansible -i ~/.hosts -m shell -a "df -h" Node2
示例输出:
使用 Ansible 安装应用程序。
在本指南中,我们将使用 playbook 文件在受管节点上进行安装。我们将安装 Nginx Web 服务器、vim,并检查 Rocky Linux 节点上的系统正常运行时间,因此我们需要创建此 playbook控制节点上的文件。
vim playbook.yaml
在 YAML 文件中,添加以下信息。
---
- hosts: all
become: yes
tasks:
- name: Install latest version of nginx on Rocky Linux Node
yum: name=nginx state=latest
- name: start nginx
service:
name: nginx
state: started
- name: Install latest version of vim on Rocky Linux Node
yum: name=vim state=latest
- name: start nginx
service:
name: nginx
state: started
- name: Check uptime of the remote host
shell: uptime
register: command_output
- debug:
var: command_output.stdout_lines
现在执行 playbook 文件,如下所示。
ansible-playbook -i ~/.hosts playbook.yaml
示例输出:
这就对了!您已在所有受管节点上成功安装Nginx。通过使用 ansible 命令检查受管节点上的 Nginx 状态来确认这一点:
ansible -i ~/.hosts -m shell -a "systemctl status nginx" all
示例输出:
192.168.100.119 | CHANGED | rc=0 >>
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Mon 2021-09-27 09:43:55 EDT; 15s ago
Process: 1789 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 1787 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 1786 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 1791 (nginx)
Tasks: 2 (limit: 4937)
Memory: 12.7M
CGroup: /system.slice/nginx.service
├─1791 nginx: master process /usr/sbin/nginx
└─1792 nginx: worker process
Sep 27 09:43:53 rockylinux8.linuxvmimages.local systemd[1]: Starting The nginx HTTP and reverse proxy server...
............
192.168.100.118 | CHANGED | rc=0 >>
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Mon 2021-09-27 09:43:55 EDT; 15s ago
Process: 1787 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 1785 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 1784 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 1789 (nginx)
Tasks: 2 (limit: 4937)
Memory: 12.7M
CGroup: /system.slice/nginx.service
├─1789 nginx: master process /usr/sbin/nginx
└─1790 nginx: worker process
Sep 27 09:43:53 rockylinux8.linuxvmimages.local systemd[1]: Starting The nginx HTTP and reverse proxy server...
Sep 27 09:43:55 rockylinux8.linuxvmimages.local nginx[1785]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
.............
结论。
这标志着本指南的结束,我们已经在 Debian 11/10 上安装并使用了 Ansible。您已经了解了如何使用 ansible 简化复杂的任务,因此,如果您正在处理利润紧张的复杂环境,那么更是如此。我希望你喜欢它。
查看更多:
- 如何使用 Ansible 管理 PostgreSQL 数据库
- 使用 Ansible 在 Ubuntu/Debian 上安装 LAMP Stack
- 使用 Ansible 自动化 Windows Server 和 Windows 管理
- 使用 Ansible 管理 SELinux 状态、上下文、端口和布尔值