Ansible的介绍、安装与部署

Ansible的介绍、安装与部署

1. 介绍 Ansible

1.1 什么是 Ansible?

Ansible是一款自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。

Ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是Ansible所运行的模块,Ansible只是提供一种框架。主要包括:
(1) 连接插件connection plugins:负责和被监控端实现通信;
(2) host inventory:指定操作的主机,是一个配置文件里面定义监控的主机;
(3) 各种模块核心模块、command模块、自定义模块;
(4) 借助于插件完成记录日志邮件等功能;
(5) playbook:剧本执行多个任务时,非必需可以让节点一次性运行多个任务。

Ansible架构图:
img

img

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. 安装需求

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@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.
Installed Packages
platform-python.x86_64                   3.6.8-1.el8.0.1                    @anaconda

2.2 受管主机

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

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

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

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

yum module install python36

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

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受管主机上。

本课程不对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。

3. 管理Ansible配置文件

3.1 配置Ansible

可以通过修改 Ansible 配置文件中的设置来自定义 Ansible安装的行为。Ansible从控制节点上多个可能的位置之一选择其配置文件。

使用/etc/ansible/ansible.cfg
ansible软件包提供一个基本的配置文件,它位于**/etc/ansible/ansible.cfg**。如果找不到其他配置文件,则使用此文件。

使用~/.ansible.cfg
Ansible在用户的家目录中查找**.ansible.cfg文件。如果存在此配置文件并且当前工作目录中也没有ansible.cfg文件,则使用此配置取代/etc/ansible/ansible.cfg**。

使用./ansible.cfg
如果执行ansible命令的目录中存在ansible.cfg文件,则使用它,而不使用全局文件或用户的个人文件。这样,管理员可以创建一种目录结构,将不同的环境或项目存储在单独的目录中,并且每个目录包含为独特的一组设置而定制的配置文件。

推荐的做法是在需要运行Ansible命令的目录中创建ansible.cfg文件。此目录中也将包含任何供Ansible项目使用的文件,如清单和playbook。这是用于Ansible配置文件的最常用位置。实践中不常使用**~/.ansible.cfg/etc/ansible/ansible.cfg**文件

使用ANSIBLE_CONFIG环境变量
我们可以通过将不同的配置文件放在不同的目录中,然后从适当的目录执行Ansible命令,以此利用配置文件。但是,随着配置文件数量的增加,这种方法存在局限性并且难以管理。有一个更加灵活的选项,即通过ANSIBLE_CONFIG环境变量定义配置文件的位置。定义了此变量时,Ansible将使用变量所指定的配置文件,而不用上面提到的任何配置文件。

3.2 配置文件优先级

ANSIBLE_CONFIG环境变量指定的任何文件将覆盖所有其他配置文件。如果没有设置该变量,则接下来检查运行ansible命令的目录中是否有ansible.cfg文件。如果不存在该文件,则检查用户的家目录是否有**.ansible.cfg文件。只有在找不到其他配置文件时,才使用全局/etc/ansible/ansible.cfg文件。如果/etc/ansible/ansible.cfg**配置文件不存在,Ansible包含它使用的默认值。

由于Ansible配置文件可以放入的位置有多种,因此Ansible当前使用哪一个配置文件可能会令人困惑。我们可以运行以下命令来清楚地确认所安装的Ansible版本以及正在使用的配置文件。

ansible --version

Ansible仅使用具有最高优先级的配置文件中的设置。即使存在优先级较低的其他配置文件,其设置也会被忽略,不会与选定配置文件中的设置结合。因此,如果你选择自行创建配置文件来取代全局**/etc/ansible/ansible.cfg**配置文件,就需要将该文件中所有需要的设置复制到自己的用户级配置文件中。用户组配置文件中未定义的设置将保持设为内置默认值,即使已在全局配置文件中设为某个其他值也是如此。

3.3 管理配置文件中的设置

Ansible配置文件由几个部分组成,每一部分含有以键值对形式定义的设置。部分的标题以中括号括起来。对于基本操作,请使用以下两部分:

  • [defaults]部分设置Ansible操作的默认值
  • [privilege_escalation]配置Ansible如何在受管主机上执行特权升级

例如,下面是典型的ansible.cfg文件:

[defaults]
inventory = ./inventory
remote_user = user
ask_pass = false

[privilege_escalation]
become = true
become_method = sudo
become_user = root
become_ask_pass = false

下表说明了此文件中的指令:
Ansible配置

指令描述
inventory指定清单文件的路径。
remote_user要在受管主机上登录的用户名。如果未指定则使用当前用户名
ask_pass是否提示输入SSH密码。如果使用SSH公钥身份验证则可以是false
become连接后是否自动在受管主机上切换用户(通常切换为root) 这也可以通过play来指定。
become_method如何切换用户(通常为sudo,这也是默认设置,但可选择su)
become_user要在受管主机上切换到的用户(通常是root,这也是默认值)
become_ask_pass是否需要为become_method提示输入密码。默认为false。

3.4 配置连接

Ansible需要知道如何与其受管主机通信。更改配置文件的一个最常见原因是为了控制Ansible使用什么方法和用户来管理受管主机。需要的一些信息包括:

  • 列出受管主机和主机组的清单的位置
  • 要使用哪一种连接协议来与受管主机通信(默认为SSH),以及是否需要非标准网络端口来连接服务器
  • 要在受管主机上使用哪一远程用户;这可以是root用户或者某一非特权用户
  • 如果远程用户为非特权用户,Ansible需要知道它是否应尝试将特权升级为root以及如何进行升级(例如,通过sudo)
  • 是否提示输入SSH密码或sudo密码以进行登录或获取特权
3.4.1 清单位置

在[defaults]部分中,inventory指令可以直接指向某一静态清单文件,或者指向含有多个静态清单文件和动态清单脚本的某一目录。

[defaults]
inventory = ./inventory
3.4.2 连接设置

默认情况下,Ansible使用SSH协议连接受管主机。控制Ansible如何连接受管主机的最重要参数在[defaults]部分中设置。

默认情况下,Ansible尝试连接受管主机时使用的用户名与运行ansible命令的本地用户相同。若要指定不同的远程用户,请将remote_user参数设置为该用户名。

如果为运行Ansible的本地用户配置了SSH私钥,使得它们能够在受管主机上进行远程用户的身份验证,则Ansible将自动登录。如果不是这种情况,可以通过设置指令ask_pass = true,将Ansible配置为提示本地用户输入由远程用户使用的密码。

【defaults]
inventory = ./inventory

remote_user = root
ask_pass = true

假设在使用一个Linux控制节点,并对受管主机使用OpenSSH,如果可以使用密码以远程用户身份登录,那么我们可以设置基于SSH密钥的身份验证,从而能够设置ask_pass = false

第一步是确保在**~/.ssh中为控制节点上的用户配置了SSH密钥对。并且使用ssh-copy-id**命令将本地的公钥复制到受管主机中。此过程请参考文章openssh

3.4.3 升级特权

鉴于安全性和审计原因,Ansible可能需要先以非特权用户身份连接远程主机,然后再通过特权升级获得root用户身份的管理权限。这可以在Ansible配置文件的**[privilege_escalation]**部分中设置。

要默认启用特权升级,可以在配置文件中设置指令become = true。即使默认为该设置,也可以在运行临时命令或Ansible Playbook时通过各种方式覆盖它。(例如,有时候可能要运行一些不需要特权升级的任务或play。)

become_method指令指定如何升级特权。有多个选项可用,但默认为使用sudo。类似地,become_user指令指定要升级到的用户,但默认为root。

如果所选的become_method机制要求用户输入密码才能升级特权,可以在配置文件中设置become_ask_pass = true指令。

以下示例ansible.cfg文件假设你可以通过基于SSH密钥的身份验证以someuser用户身份连接受管主机,并且someuser可以使用sudo以root用户身份运行命令而不必输入密码:

[defaults]
inventory = ./inventory
remote_user = someuser
ask_pass = false

[privilege_escalation]
become = true
become_method = sudo
become_user = root
become_ask_pass = false
3.4.4 非SSH连接

默认情况下,Ansible用于连接受管主机的协议设置为smart,它会确定使用SHH的最高效方式。可以通过多种方式将其设置为其他的值。

例如,默认使用SSH的规则有一个例外。如果目录中没有localhost,Ansible将设置一个隐式localhost条目以便允许运行以localhost为目标的临时命令和playbook。这一特殊清单条目不包括在all或ungrouped主机组中。此外,Ansible不使用smart SSH连接类型,而是利用默认的特殊local连接类型来进行连接。

ansible localhost --list-hosts

local连接类型忽略remote_user设置,并且直接在本地系统上运行命令。如果使用特权升级,它会在运行sudo时使用运行Ansible命令的用户,而不是remote_user。如果这两个用户具有不同的sudo特权,这可能会导致混淆。

如果你要确保像其他受管主机一样使用SSH连接localhost,一种方法是在清单中列出它。但是,这会将它包含在all和ungrouped组中,而你可能不希望如此。

另一种方法是更改用于连接localhost的协议。执行此操作的最好方法是为localhost设置ansible_connection主机变量。为此,你需要在运行Ansible命令的目录中创建host_vars子目录。在该子目录中,创建名为localhost的文件,其应含有ansible_connection: smart这一行。这将确保对localhost使用smart(SSH)连接协议,而非local。

你也可以通过另一种变通办法来使用它。如果清单中列有127.0.0.1,则默认情况下,将会使用smart来连接它。也可以创建一个含有ansible_connection: local这一行的host_vars/127.0.0.1文件,它会改为使用local。

3.5 配置文件注释

Ansible配置文件允许使用两种注释字符:井号或分号。

位于行开头的#号会注释掉整行。它不能和指令位于同一行中。

分号字符可以注释掉所在行中其右侧的所有内容。它可以和指令位于同一行中,只要该指令在其左侧。

4. 运行临时命令

使用临时命令可以快速执行单个Ansible任务,不需要将它保存下来供以后再次运行。它们是简单的在线操作,无需编写playbook即可运行。

临时命令对快速测试和更改很有用。例如,可以使用临时命令确保一组服务器上的**/etc/hosts**文件中存在某一特定的行。可以使用另一个临时命令在许多不同的计算机上高效的重启服务,或者确保特定的软件包为最新版本。

临时命令对于通过Ansible快速执行简单的任务非常有用。它们确实也存在局限,而且总体而言,要使用Ansible Playbook来充分发挥Ansible的作用。但在许多情形中,临时命令正是快速执行简单任务所需要的工具。

4.1 运行临时命令

Ansible运行临时命令的语法如下:

ansible host-pattern -m module [-a 'module arguments'] [-i inventory]

host-pattern参数用于指定在其上运行临时命令的受管主机。它可以是清单中的特定受管主机或主机组。也可以用后面的-i选项指定特定的清单而不使用默认清单。

-m选项将Ansible应在目标主机上运行的module名称作为参数。模块是为了实施任务而执行的小程序。一些模块不需要额外的信息,但其他模块需要使用额外的参数来指定其操作详情。-a选项以带引号字符串形式取这些参数的列表。

一种最简单的临时命令使用ping模块。此模块不执行ICMP ping,而是检查能否在受管主机上运行基于Python的模块。例如,以下临时命令确定清单中的所有受管主机能否运行标准的模块:

[root@localhost ~]# ansible all -m ping
192.168.116.140 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
......

4.2 使用临时命令通过模块来执行任务

模块是临时命令用于完成任务的工具。Ansible提供了数百个能够完成不同任务的模块。通常我们可以查找一个经过测试的专用模块,作为标准安装的一部分来完成所需的任务。

ansible-doc -l命令可以列出系统上安装的所有模块。可以使用ansible-doc来按照名称查看特定模块的帮助文档,再查找关于模块将取什么参数作为选项的信息。例如以下命令显示ping模块的帮助文档,在帮助文档里面输入**q**命令表示退出:

ansible-doc ping

更多的模块信息请访问在线Ansible文档,网址:https://docs.ansible.com/ansible/latest/modules/modules_by_category.html。

Ansible常用模块

模块类别模块
文件模块copy:将本地文件复制到受管主机 file:设置文件的权限和其他属性 lineinfile:确保特定行是否在文件中 synchronize:使用rsync同步内容
软件包模块package:使用操作系统本机的自动检测软件包管理器管理软件包 yum:使用yum管理软件包 apt:使用APT管理软件包 dnf:使用dnf管理软件包 gem:管理Ruby gem pip:从PyPI管理Python软件包
系统模块firewalld:使用firewalld管理防火墙 reboot:重启计算机 service:管理服务 user:添加、删除和管理用户帐户
Net Tools模块get_url:通过HTTP、HTTPS或FTP下载文件 nmcli:管理网络 uri:与Web服务交互

大部分模块会取用参数。可在模块的文档中找到可用于该模块的参数列表。临时命令可以通过-a选项向模块传递参数。无需参数时,可从临时命令中省略-a选项。如果需要指定多个参数,请以引号括起的空格分隔列表形式提供。

例如,以下临时命令使用user模块来确保runtime用户存在于192.168.116.143上并且其UID为4000:

ansible 192.168.116.143 -m user -a 'name=runtime uid=4000 state=present'

大多数模块为idempotent,这表示它们可以安全地多次运行;如果系统已处于正确的状态,它们不会进行任何操作。

4.3 在受管主机上运行任意命令

command模块允许管理员在受管主机的命令行中运行任意命令。要运行的命令通过-a选项指定为该模块的参数。例如,以下命令将对webservers组的受管主机运行hostname命令:

[root@localhost ~]# ansible webservers -m command -a 'hostname'
192.168.116.140 | CHANGED | rc=0 >>
node03-linux.example.com
192.168.116.143 | CHANGED | rc=0 >>
node02-linux.example.com

这条命令为每个受管主机返回两行输出。第一行是状态报告,显示对其运行临时操作的受管主机名称及操作的结果。第二行是使用Ansible command模块远程执行的命令的输出。

若要改善临时命令输出的可读性和解析,管理员可能会发现使对受管主机执行的每一项操作具有单行输出十分有用。使用-o选项以单行格式显示Ansible临时命令的输出。

[root@localhost ~]# ansible webservers -m command -a 'hostname' -o
192.168.116.140 | CHANGED | rc=0 | (stdout) node03-linux.example.com
192.168.116.143 | CHANGED | rc=0 | (stdout) node02-linux.example.com

command模块允许管理员对受管主机快速执行远程命令。这些命令不是由受管主机上的shell加以处理。因此,它们无法访问shell环境变量,也不能执行重定向和管道等shell操作。

在命令需要shell处理的情形中,管理员可以使用shell模块。与command模块类似,可以在临时命令中将要执行的命令作为参数传递给该模块。Ansible随后对受管主机远程执行该命令。与command模块不同的是,这些命令将通过受管主机上的shell进行处理。因此,可以访问shell环境变量,也可以使用重定向和管道等操作。

以下示例演示了command与shell的区别。如果尝试使用这两个模块执行内建的Bash命令set,只有使用shell模块才会成功:

[root@localhost ~]# ansible 192.168.116.140 -m command -a 'set'
192.168.116.140 | FAILED | rc=2 >>
[Errno 2] No such file or directory

[root@localhost ~]# ansible 192.168.116.143 -m shell -a 'set'
192.168.116.143 | CHANGED | rc=0 >>
BASH=/bin/sh
BASHOPTS=cmdhist:extquote:force_fignore:hostcomplete:interactive_comments:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
......

command和shell模块都要求受管主机上安装正常工作的Python。第三个模块是raw,它可以绕过模块子系统,直接使用远程shell运行命令。在管理无法安装Python的系统(如网络路由器)时,可以利用这个模块。它也可以用于将Python安装到主机上。

在大多数情况下,建议避免使用command、shell和raw这三个“运行命令”模块。

其他模块大部分都是幂等的,可以自动进行更改跟踪。它们可以测试系统的状态,在这些系统已处于正确状态时不执行任何操作。相反,以幂等方式使用“运行命令”模块要复杂得多。依靠它们,你更难以确信再次运行临时命令或playbook不会造成意外的失败。当shell或command模块运行时,通常会基于它是否认为影响了计算机状态而报告CHANGED状态。

有时候,“运行命令”模块是有用的工具,也是解决问题的好办法。如果确实需要使用它们,可能最好先尝试用command模块,只有在需要shell或raw模块的特殊功能时才利用它们。

4.4 配置临时命令的连接

受管主机连接和特权升级的指令可以在Ansible配置文件中配置,也可以使用临时命令中的选项来定义。使用临时命令中的选项定义时,它们将优先于Ansible配置文件中配置的指令。下表显示了与各项配置文件指令类同的命令行选项。

Ansible命令行选项

配置文件指令命令行选项
inventory-i
remote_user-u
become—become、-b
become_method–become-method
become_user–become-user
become_ask_pass–ask-become-pass、-K

在使用命令行选项配置这些指令前,可以通过查询ansible --help的输出来确定其当前定义的值。

ansible --help

5.安装Ansible

1.配置环境
//控制节点controller:192.168.116.145
//工作节点node1:     192.168.116.140
//工作节点node2:     192.168.116.143

2.修改主机名
[root@localhost ~]# hostnamectl set-hostname controller
[root@localhost ~]# bash
[root@controller ~]# 

[root@localhost ~]# hostnamectl set-hostname node1
[root@localhost ~]# bash
[root@node1 ~]# 

[root@localhost ~]# hostnamectl set-hostname node2
[root@localhost ~]# bash
[root@node2 ~]# 

3.关闭防火墙
[root@controller ~]# systemctl disable --now firewalld
Removed "/etc/systemd/system/multi-user.target.wants/firewalld.service".
Removed "/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service".
[root@controller ~]# setenforce 0
[root@controller ~]# vi /etc/selinux/config
[root@controller ~]# cat /etc/selinux/config | grep disabled
#     disabled - No SELinux policy is loaded.
# NOTE: Up to RHEL 8 release included, SELINUX=disabled would also
# fully disabled instead of SELinux running with no policy loaded, you
SELINUX=disabled

[root@node1 ~]# systemctl disable --now firewalld
Removed "/etc/systemd/system/multi-user.target.wants/firewalld.service".
Removed "/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service".
[root@node1 ~]# setenforce 0
[root@node1 ~]# vi /etc/selinux/config
[root@node1 ~]# cat /etc/selinux/config | grep SELINUX
# SELINUX= can take one of these three values:
# NOTE: Up to RHEL 8 release included, SELINUX=disabled would also
SELINUX=disabled
# SELINUXTYPE= can take one of these three values:
SELINUXTYPE=targeted

[root@node2 ~]# systemctl disable --now firewalld
Removed "/etc/systemd/system/multi-user.target.wants/firewalld.service".
Removed "/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service".
[root@node2 ~]# vi /etc/selinux/config
[root@node2 ~]# vi /etc/selinux/config
[root@node2 ~]# cat /etc/selinux/config | grep SELINUX
# SELINUX= can take one of these three values:
# NOTE: Up to RHEL 8 release included, SELINUX=disabled would also
SELINUX=disabled
# SELINUXTYPE= can take one of these three values:
SELINUXTYPE=targeted

4.配置yum源(以下拉的是阿里云的repo源)
//本地有ansible的话直接列出来下就行了,对版本没要求的情况下
[root@controller ~]# mv /etc/yum.repos.d/* /opt
[root@controller ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2495  100  2495    0     0   6835      0 --:--:-- --:--:-- --:--:--  6835
[root@controller ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@controller ~]# yum clean all
Failed to set locale, defaulting to C.UTF-8
21 files removed
[root@controller ~]# yum makecache
Failed to set locale, defaulting to C.UTF-8
CentOS-8.5.2111 - Base - mirrors.aliyun.com                                                                                           3.7 MB/s | 4.6 MB     00:01    
CentOS-8.5.2111 - Extras - mirrors.aliyun.com                                                                                          46 kB/s |  10 kB     00:00    
CentOS-8.5.2111 - AppStream - mirrors.aliyun.com                                                                                      2.9 MB/s | 8.4 MB     00:02    
Metadata cache created.

5.安装ansible(系统版本或系统不一样查出来的ansible也有不同)
[root@controller ~]# yum list all | grep ansible
Failed to set locale, defaulting to C.UTF-8
ansible-collection-microsoft-sql.noarch                1.1.0-1.el8                                            AppStream 
ansible-collection-redhat-rhel_mgmt.noarch             1.0.0-2.el8                                            AppStream 
ansible-freeipa.noarch                                 0.3.8-1.el8                                            AppStream 
ansible-freeipa-tests.noarch                           0.3.8-1.el8                                            AppStream 
ansible-pcp.noarch                                     2.2.1-1.el8                                            AppStream 
centos-release-ansible-29.noarch                       1-2.el8                                                extras    
[root@controller ~]# yum -y install centos-release-ansible-29
Complete!
[root@controller ~]# yum -y install ansible
Complete!
[root@controller ~]# ansible --version
ansible 2.9.27
  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, Jan 19 2022, 23:28:49) [GCC 8.5.0 20210514 (Red Hat 8.5.0-7)]

6.部署ansible

1.配置ansible
//修改默认清单文件
[root@controller ~]# cd /etc/ansible/
[root@controller ansible]# ls
ansible.cfg  hosts  roles
[root@controller ansible]# touch inventory
[root@controller ansible]# ls
ansible.cfg  hosts  inventory  roles
[root@controller ansible]# vim ansible.cfg 
[root@controller ansible]# cat ansible.cfg | grep inventory
inventory      = /etc/ansible/inventory

//映射
[root@controller ansible]# vim hosts 
[root@controller ansible]# cat hosts|grep 192.168.116
192.168.116.140 node1
192.168.116.143 node2

//向清单文件写入内容并查看(直接写入node1和node2)
[root@controller ansible]# vim inventory
[root@controller ansible]# cat inventory
192.168.116.140
node2
[root@controller ansible]# ansible all --list-hosts
  hosts (2):
    192.168.116.140
    node2
[root@controller ansible]# ansible node2 --list-hosts
  hosts (1):
    node2
[root@controller ansible]# ansible 192.168.116.140 --list-hosts
  hosts (1):
    192.168.116.140

//添加组名(把两台主机写入里面)
[root@controller ansible]# vim inventory
[root@controller ansible]# cat inventory
[web]
192.168.116.140
node2
[root@controller ansible]# ansible all --list-hosts
  hosts (2):
    192.168.116.140
    node2
[root@controller ansible]# ansible web --list-hosts
  hosts (2):
    192.168.116.140
    node2
[root@controller ansible]# ansible node2 --list-hosts
  hosts (1):
    node2

//用ungrouped查看(查看的结果是不在组名里面的)
[root@controller ansible]# ansible ungrouped --list-hosts
[WARNING]: No hosts matched, nothing to do
  hosts (0):

//一个写入组名中,一个不写入组名中
[root@controller ansible]# vim inventory
[root@controller ansible]# cat inventory
node2
[web]
192.168.116.140

[root@controller ansible]# ansible web --list-hosts
  hosts (1):
    192.168.116.140
[root@controller ansible]# ansible ungrouped --list-hosts
  hosts (1):
    node2
[root@controller ansible]# ansible all --list-hosts
  hosts (2):
    node2
    192.168.116.140

2.自定义清单文件
[root@controller ~]# cd /etc/ansible/
[root@controller ansible]# ls
ansible.cfg  hosts  inventory  roles
[root@controller ansible]# vim inventory
[root@controller ansible]# cat inventory
192.168.116.140 ansible_user=root ansible_password=1
192.168.116.143 ansible_user=root ansible_password=2
[root@controller ansible]# ansible all -m ping
node2 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname node2: Name or service not known",
    "unreachable": true
}
node1 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname node1: Name or service not known",
    "unreachable": true
}

//解决方案(添加指纹,留下记号)
[root@controller ~]# ssh 192.168.116.140
The authenticity of host '192.168.116.140 (192.168.116.140)' can't be established.
ECDSA key fingerprint is SHA256:aYlCkr0NN3sBZm9mllDeFeYUs9j7Mysp1lwiVvMs9rw.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.116.140' (ECDSA) to the list of known hosts.
root@192.168.116.140's password: 
Last login: Tue Dec 12 19:37:40 2023 from 192.168.116.1
[root@node1 ~]# exit
logout
Connection to 192.168.116.140 closed.

[root@controller ~]# ssh 192.168.116.143
The authenticity of host '192.168.116.143 (192.168.116.143)' can't be established.
ECDSA key fingerprint is SHA256:ZSvQk4mMG3YZuunxeojAN+Iss/jdn/gTVQIUO4HFTuU.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.116.143' (ECDSA) to the list of known hosts.
root@192.168.116.143's password: 
Last login: Tue Dec 12 19:38:02 2023 from 192.168.116.1

[root@controller ~]# ls .ssh
known_hosts
[root@controller ~]# cat .ssh/known_hosts 
192.168.116.140 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBJIsAk/rz5hP3Wv0nBIhrDnMrddRjgpw9U+ZRBb03sCg3hRUXmczza015g9LtWH6He0gqq/6lrxw0nnNzoMPypI=
192.168.116.143 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBBivMGiLO3t1+s9re6upnfwU90UpF+v6WsNOsiYSjhE4ZRD9lgFwnQwRX85sLptWemVDQBne1s0srEj7E6QsLJQ=

//再次查看
[root@controller ansible]# ansible all -m ping
[WARNING]: Platform linux on host 192.168.116.143 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python
interpreter could change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
192.168.116.143 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
[WARNING]: Platform linux on host 192.168.116.140 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python
interpreter could change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
192.168.116.140 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

3.管理ansible的配置文件(配置文件为在当前所在的位置有ansible.cfg的文件)
//默认配置文件在/etc/ansible
[root@controller ansible]# ansible --version
ansible 2.9.27
  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, Jan 19 2022, 23:28:49) [GCC 8.5.0 20210514 (Red Hat 8.5.0-7)]

//修改配置文件的位置
/root家目录
[root@controller ~]# touch ansible.cfg
[root@controller ~]# pwd
/root
[root@controller ~]# ansible --version
ansible 2.9.27
  config file = /root/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, Jan 19 2022, 23:28:49) [GCC 8.5.0 20210514 (Red Hat 8.5.0-7)]

/etc目录
[root@controller ~]# cd /etc
[root@controller etc]# pwd
/etc
[root@controller etc]# touch ansible.cfg
[root@controller etc]# ansible --version
ansible 2.9.27
  config file = /etc/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, Jan 19 2022, 23:28:49) [GCC 8.5.0 20210514 (Red Hat 8.5.0-7)]

/指定ansible配置文件位置
[root@controller etc]# export ANSIBLE_CONFIG=/etc/ansible/ansible.cfg
[root@controller etc]# bash
[root@controller etc]# ansible --version
ansible 2.9.27
  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, Jan 19 2022, 23:28:49) [GCC 8.5.0 20210514 (Red Hat 8.5.0-7)]

4.配置免提登录
[root@controller ~]# cd /etc/ansible/
[root@controller ansible]# vim inventory
[root@controller ansible]# cat inventory
192.168.116.140 
192.168.116.143 
[root@controller ansible]# ansible all -m ping
192.168.116.140 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: root@192.168.116.140: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).",
    "unreachable": true
}
192.168.116.143 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: root@192.168.116.143: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).",
    "unreachable": true
}

//解决方案
[root@controller 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:wF2F5CahBoo6rJ0fMHi7HWU2V8ZcF5eS/zAHu4eN7x8 root@controller
The key's randomart image is:
+---[RSA 3072]----+
|    .   ..ooo +.o|
| . . o o *.. +.o |
|. .   = o B   oo |
|o.   . . =    +..|
|+.+   = S      O.|
|.+ = + o      + +|
|. + o          E |
|   + o          o|
|  . o          .+|
+----[SHA256]-----+
[root@controller ansible]# ssh-copy-id 192.168.116.140
/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.116.140's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.116.140'"
and check to make sure that only the key(s) you wanted were added.

[root@controller ansible]# ssh-copy-id 192.168.116.143
/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.116.143's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.116.143'"
and check to make sure that only the key(s) you wanted were added.
[root@controller ~]# ls .ssh
id_rsa  id_rsa.pub  known_hosts

//再次查看
[root@controller ~]# ansible all -m ping
[WARNING]: Platform linux on host 192.168.116.143 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python
interpreter could change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
192.168.116.143 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
[WARNING]: Platform linux on host 192.168.116.140 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python
interpreter could change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
192.168.116.140 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

5.在配置文件中修改默认登入用户
[root@controller ~]# vim /etc/ansible/ansible.cfg 
[root@controller ~]# cat /etc/ansible/ansible.cfg | grep remote_user
remote_user = student

[root@node1 ~]# useradd student
[root@node1 ~]# echo '1' | passwd --stdin student
Changing password for user student.
passwd: all authentication tokens updated successfully.

[root@node2 ~]# useradd student
[root@node2 ~]# echo '1' | passwd --stdin student
Changing password for user student.
passwd: all authentication tokens updated successfully.

//连接
[root@controller ~]# ansible all -m ping 
192.168.116.140 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: student@192.168.116.140: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).",
    "unreachable": true
}
192.168.116.143 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: student@192.168.116.143: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).",
    "unreachable": true
}

//解决方案
[root@controller ~]# ssh-copy-id student@192.168.116.140
[root@controller ~]# ssh-copy-id student@192.168.116.143
[root@controller ~]# ansible all -m ping
[WARNING]: Platform linux on host 192.168.116.143 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python
interpreter could change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
192.168.116.143 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
[WARNING]: Platform linux on host 192.168.116.140 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python
interpreter could change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
192.168.116.140 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

6.授权
[root@controller ~]# ansible 192.168.116.140 -a 'useradd tom'
192.168.116.140 | FAILED | rc=1 >>
useradd: Permission denied.
useradd: cannot lock /etc/passwd; try again later.non-zero return code

//node1
[root@node1 ~]# visudo
[root@node1 ~]# visudo | grep student
student ALL=(ALL)       NOPASSWD:ALL

//node2
[root@node2 ~]# su - student
Last login: Tue Dec 12 23:20:54 CST 2023 from 192.168.116.145 on pts/2
[student@node2 ~]$ useradd tom
useradd: Permission denied.
useradd: cannot lock /etc/passwd; try again later.
[root@node2 ~]# visudo
[root@node2 ~]# visudo | grep student
Vim: Warning: Output is not to a terminal
student ALL=(ALL)       NOPASSWD:ALL
[root@node2 ~]# su - student
Last login: Tue Dec 12 23:27:17 CST 2023 on pts/0
[student@node2 ~]$ sudo useradd tom
[student@node2 ~]$ id tom
uid=1002(tom) gid=1002(tom) groups=1002(tom)

[root@controller ~]# vim /etc/ansible/ansible.cfg  //取消这几行注释
[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False

[root@controller ~]# ansible 192.168.116.140 -a 'useradd tom'
192.168.116.140 | CHANGED | rc=0 >>
[root@node1 ~]# id tom
uid=1003(tom) gid=1003(tom) groups=1003(tom)

  • 24
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值