在命令行上使用Ansible入门

时间:2020-03-05 15:29:46  来源:igfitidea点击:

ANSIBLE是用于配置管理,供应,应用程序部署和服务编排的开源软件平台。
它可用于在生产,登台和开发中配置我们的服务器。
它还可以用于管理应用程序服务器,例如Web服务器,数据库服务器和许多其他服务器。
其他类似于配置管理的系统是CHEF,Puppet,SALT和Distelli,相比之下,所有这些ANSIBLE是最简单易管理的工具。
使用Ansible的主要优点如下:

1.模块可以用任何编程语言编写。

2.客户端计算机上未运行任何代理。

3.易于安装和管理。

4.高度可靠。

5.可扩展

在本文中,我将解释有关使用Ansible的第一步的一些基础知识。

了解主机文件

安装Ansible后,第一件事就是了解其列表文件。
该文件包含由Ansible管理的目标服务器的列表。
默认的主机文件位置是/etc/ansible/hosts。
我们可以编辑此文件以包括我们的目标系统。
该文件指定了几个组,我们可以其中将主机分类为首选项。

如此处所述,创建主机文件时要注意的重要事项:

注释以“#”字符开头
空行将被忽略
主机组由[header]元素分隔
我们可以输入主机名或者IP地址
主机名/IP可以是多个组的成员
远程主机可以在多个组中进行分配
将主机范围包含在一个字符串中作为服务器-[01:12] -example.com

PS:不建议在默认列表文件中进行修改,相反,我们可以根据需要在任何位置创建自己的自定义列表文件。

Ansible如何运作?

首先,Ansible管理客户端使用SSH连接到目标服务器。
我们不需要在客户端服务器上设置任何代理。
我们只需要Python和一个可以登录并执行脚本的用户即可。
建立连接后,它将开始收集有关客户端计算机的事实,例如操作系统,运行的服务和程序包。
我们可以轻松使用Ansible执行不同的命令,复制/修改/删除文件和文件夹,管理或者配置软件包和服务。
我将在演示设置的帮助下进行演示。

我的客户端服务器是45.33.76.60和139.162.35.39.
我在用户下创建了自定义库存托管文件。
请查看我的列表文件,其中包括三组,分别是网络服务器,生产和测试。

在Web服务器中,我包括了两个客户端服务器。
并将它们分为两个组,一个在生产中,另一个在测试中。

linuxmonty@linuxmonty-Latitude-E4310:~$cat hosts
[webservers]
139.162.35.39
45.33.76.60
[production]
139.162.35.39
[Testing]
45.33.76.60

建立SSH连接

我们需要为管理服务器创建SSH密钥,并将其复制到目标服务器以增强SSH连接。
让我们看一下我如何为客户端服务器执行此操作。

linuxmonty@linuxmonty-Latitude-E4310:~$# ssh-keygen -t rsa -b4096
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:
2e:2f:32:9a:73:6d:ba:f2:09:ac:23:98:0c:fc:6c:a0 linuxmonty@linuxmonty-Latitude-E4310
The key's randomart image is:
+--[ RSA 4096]----+
| |
| |
| |
| |
|. S |
|.+ . |
|=.* .. . |
|Eoo*+.+o |
|o.+*=* .. |
+-----------------+

复制SSH密钥

这就是我们将SSH密钥从管理服务器复制到目标服务器的方式。

客户1:

linuxmonty@linuxmonty-Latitude-E4310# ssh-copy-id [email protected]
/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
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.
linuxmonty@linuxmonty-Latitude-E4310#

客户2:

linuxmonty@linuxmonty-Latitude-E4310# ssh-copy-id [email protected]
/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
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.
linuxmonty@linuxmonty-Latitude-E4310#

从管理服务器执行这些命令后,密钥将被添加到目标服务器并保存在授权密钥中。

熟悉一些基本的Ansible模块

模块控制系统资源,配置,软件包,文件等。
Ansible中使用了大约450多个模块。
首先,让我们使用该模块检查管理服务器和目标服务器之间的连接。
我们可以从管理服务器运行ping模块,以确认连接。

linuxmonty@linuxmonty-Latitude-E4310:~$ansible -i hosts all -m ping -u root
139.162.35.39 | success >> {
"changed": false,
"ping": "pong"
}
45.33.76.60 | success >> {
"changed": false,
"ping": "pong"
}
-i : Represents the inventory file selection
-m : Represents the module name selection
-u : Represents the user for execution.

由于我们以用户身份运行此命令以连接到目标服务器,因此需要切换到root用户以执行模块。

这是检查主机文件中库存状态的方法。

linuxmonty@linuxmonty-Latitude-E4310:~$ansible -i hosts webservers --list-hosts
139.162.35.39
45.33.76.60
linuxmonty@linuxmonty-Latitude-E4310:~$
linuxmonty@linuxmonty-Latitude-E4310:~$ansible -i hosts production --list-hosts
139.162.35.39
linuxmonty@linuxmonty-Latitude-E4310:~$ansible -i hosts Testing --list-hosts
45.33.76.60

设定模块

现在,我们运行设置模块以收集有关目标服务器的更多事实,以组织剧本。
该模块为我们提供有关服务器硬件,网络以及一些与ansible相关的软件设置的信息。
这些事实可以在手册部分中进行描述,并代表有关系统的已发现变量。
这些也可用于实现任务的条件执行。

linuxmonty@linuxmonty-Latitude-E4310:~$ansible -i hosts all -m setup -u root

You can view the server architecture, CPU information, python version, memory, OS version etc by running this module.

命令模块

这是命令模块用法的一些示例。
我们可以将任何参数传递给此命令模块来执行。

正常运行时间:

linuxmonty@linuxmonty-Latitude-E4310:~$ansible -i hosts all -m command -a 'uptime' -u root
139.162.35.39 | success | rc=0 >>
14:55:31 up 4 days, 23:56, 1 user, load average: 0.00, 0.01, 0.05
45.33.76.60 | success | rc=0 >>
14:55:41 up 15 days, 3:20, 1 user, load average: 0.20, 0.07, 0.06

主机名:

linuxmonty@linuxmonty-Latitude-E4310:~$ansible -i hosts all -m command -a 'hostname' -u root
139.162.35.39 | success | rc=0 >>
client2.production.com
45.33.76.60 | success | rc=0 >>
client1.testing.com

w:

linuxmonty@linuxmonty-Latitude-E4310:~$ansible -i hosts all -m command -a 'w' -u root
139.162.35.39 | success | rc=0 >>
08:07:55 up 4 days, 17:08, 2 users, load average: 0.00, 0.01, 0.05
USER TTY LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 07:54 7:54 0.00s 0.00s -bash
root pts/1 08:07 0.00s 0.05s 0.00s w
45.33.76.60 | success | rc=0 >>
08:07:58 up 14 days, 20:33, 2 users, load average: 0.03, 0.03, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 101.63.79.157 07:54 8:01 0.00s 0.00s -bash
root pts/1 101.63.79.157 08:07 0.00s 0.05s 0.00s w

同样,我们可以使用Ansible中的命令模块在多个目标服务器上执行任何linux命令。

管理用户和组

Ansible提供了一个称为“用户”的模块,该模块用于此目的。
“用户”模块可轻松创建和操作现有用户帐户,以及根据我们的需要删除现有用户帐户。

用法:ansible -i库存选择-m用户-a“名称=用户名1密码= <此处为加密密码>”

linuxmonty@linuxmonty-Latitude-E4310:~$ansible -i hosts all -m user -a "name=adminsupport password=<default123>" -u root
45.33.76.60 | success >> {
"changed": true,
"comment": "",
"createhome": true,
"group": 1004,
"home": "/home/adminsupport",
"name": "adminsupport",
"password": "NOT_LOGGING_PASSWORD",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 1004
}
In the above server, this command initiates the creation of this adminsupport user. But in the server 139.162.35.39 this user is already present, hence, it skips any other modifications for that user.
139.162.35.39 | success >> {
"changed": true,
"comment": "",
"createhome": true,
"group": 1001,
"home": "/home/adminsupport",
"name": "adminsupport",
"password": "NOT_LOGGING_PASSWORD",
"shell": "/bin/bash",
"state": "present",
"stderr": "useradd: warning: the home directory already exists.\nNot copying any file from skel directory into it.\nCreating mailbox file: File exists\n",
"system": false,
"uid": 1001
}

用法:ansible -i库存选择-m user -a'名称=用户名状态=不存在'

linuxmonty@linuxmonty-Latitude-E4310:~$ansible -i hosts Testing -m user -a "name=adminsupport state=absent" -u root
45.33.76.60 | success >> {
"changed": true,
"force": false,
"name": "adminsupport",
"remove": false,
"state": "absent"
}

此命令从我们的测试服务器45.33.76.60删除用户adminsupport。

文件传输

Ansible提供了一个名为“复制”的模块,以增强跨多个服务器的文件传输。
它可以安全地将大量文件并行传输到多个服务器。

用法:ansible -i库存选择-m复制-a“ src =文件名dest =要保存的文件路径”

我正在将一个名为test.sh的shell程序脚本从我的管理服务器复制到所有目标服务器/root。
请在下面查看命令用法:

linuxmonty@linuxmonty-Latitude-E4310:~$ansible -i hosts all -m copy -a "src=test.sh dest=/root/" -u root
139.162.35.39 | success >> {
"changed": true,
"dest": "/root/test.sh",
"gid": 0,
"group": "root",
"md5sum": "d910e95fdd8efd48d7428daafa7706ec",
"mode": "0755",
"owner": "root",
"size": 107,
"src": "/root/.ansible/tmp/ansible-tmp-1463040011.67-93143679295729/source",
"state": "file",
"uid": 0
}
45.33.76.60 | success >> {
"changed": true,
"dest": "/root/test.sh",
"gid": 0,
"group": "root",
"md5sum": "d910e95fdd8efd48d7428daafa7706ec",
"mode": "0755",
"owner": "root",
"size": 107,
"src": "/root/.ansible/tmp/ansible-tmp-1463040013.85-235107847216893/source",
"state": "file",
"uid": 0
}

输出结果

[root@client2 ~]# ll /root/test.sh
-rwxr-xr-x 1 root root 107 Jan 12 08:00 /root/test.sh

如果我们使用剧本,则可以利用模块模板执行相同的任务。

它还提供了一个名为“文件”的模块,该模块我们在多个服务器之间更改文件的所有权和权限。
我们可以将这些选项直接传递给“复制”命令。
此模块还可用于创建或者删除文件/文件夹。

例子 :

我已经修改了目标服务器上现有文件test.sh的所有权和组,并将其权限更改为600。

linuxmonty@linuxmonty-Latitude-E4310:~$ansible -i hosts production -m file -a "dest=/root/test.sh mode=600 owner=adminsupport group=adminsupport" -u root
139.162.35.39 | success >> {
"changed": true,
"gid": 1001,
"group": "adminsupport",
"mode": "0600",
"owner": "adminsupport",
"path": "/root/test.sh",
"size": 107,
"state": "file",
"uid": 1001
}
Output :
[root@client2 ~]# ll | grep test.sh
-rw------- 1 adminsupport adminsupport 107 Jan 12 08:00 test.sh

创建一个文件夹

现在,我需要创建一个具有所需所有权和权限的文件夹。
让我们看一下获取该命令的命令。
我正在生产服务器组上创建一个“ ansible”文件夹,并将其分配给拥有755个权限的所有者“ adminsupport”。

linuxmonty@linuxmonty-Latitude-E4310:~$ansible -i hosts production -m file -a "dest=/root/ansible mode=755 owner=adminsupport group=adminsupport state=directory" -u root
139.162.35.39 | success >> {
"changed": true,
"gid": 1001,
"group": "adminsupport",
"mode": "0755",
"owner": "adminsupport",
"path": "/root/ansible",
"size": 4096,
"state": "directory",
"uid": 1001
}

输出 :

[root@client2 ~]# ll | grep ansible
drwxr-xr-x 2 adminsupport adminsupport 4096 Jan 12 08:45 ansible
[root@client2 ~]# pwd
/root

删除资料夹

我们甚至可以使用此模块从多个目标服务器删除文件夹/文件。
请看看我是怎么做到的。

linuxmonty@linuxmonty-Latitude-E4310:~$ansible -i hosts production -m file -a "dest=/root/ansible state=absent" -u root
139.162.35.39 | success >> {
"changed": true,
"path": "/root/ansible",
"state": "absent"
}

决定操作的唯一变量是称为“状态”的任意变量,将其修改为不从服务器删除该特定文件夹。

管理包

让我们看看如何使用Ansible管理软件包。
我们需要确定目标服务器的平台,并使用适合目的的所需软件包管理器模块,例如yum或者apt。
我们可以根据目标服务器的操作系统版本使用apt或者yum。
它还具有用于在许多平台下管理软件包的模块。

在列表中的生产服务器上安装VsFTPD软件包。

linuxmonty@linuxmonty-Latitude-E4310:~$ansible -i hosts production -m yum -a 'name=vsftpd state=present' -u root
139.162.35.39 | success >> {
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\n * base: mirrors.linode.com\n * epel: mirror.wanxp.id\n * extras: mirrors.linode.com\n * updates: mirrors.linode.com\nResolving Dependencies\n--> Running transaction check\n---> Package vsftpd.x86_64 0:3.0.2-11.el7_2 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n============================\n Package Arch Version Repository Size\n===========================================\nInstalling:\n vsftpd x86_64 3.0.2-11.el7_2 updates 167 k\n\nTransaction Summary\n===============================================\nInstall 1 Package\n\nTotal download size: 167 k\nInstalled size: 347 k\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : vsftpd-3.0.2-11.el7_2.x86_64 1/1 \n Verifying : vsftpd-3.0.2-11.el7_2.x86_64 1/1 \n\nInstalled:\n vsftpd.x86_64 0:3.0.2-11.el7_2 \n\nComplete!\n"
]
}
linuxmonty@linuxmonty-Latitude-E4310:~$ansible -i hosts production -m yum -a 'name=vsftpd state=present' -u root
139.162.35.39 | success >> {
"changed": false,
"msg": "",
"rc": 0,
"results": [
"vsftpd-3.0.2-11.el7_2.x86_64 providing vsftpd is already installed"
]
}

如果我们注意到了,我们会看到,当我第一次执行ansible命令来安装软件包时,“ changed”变量为“ true”,这意味着该命令已安装了软件包。
但是,当我再次运行该命令时,它报告变量“ changed”为“ false”,这意味着该命令检查了软件包安装,并发现该软件包已安装,因此该服务器上没有执行任何操作。

同样,我们可以更新或者删除软件包,这是确定状态变量的唯一变量,可以将状态变量修改为最新以安装最新的可用软件包,而无需将其从服务器中删除。

例子:

更新包

linuxmonty@linuxmonty-Latitude-E4310:~$ansible -i hosts production -m yum -a 'name=vsftpd state=latest' -u root
139.162.35.39 | success >> {
"changed": false,
"msg": "",
"rc": 0,
"results": [
"All packages providing vsftpd are up to date"
]
}

这说明已安装的软件已经是最新版本,并且没有可用的更新。

取出包装

linuxmonty@linuxmonty-Latitude-E4310:~$ansible -i hosts production -m yum -a 'name=vsftpd state=absent' -u root
139.162.35.39 | success >> {
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Loaded plugins: fastestmirror\nResolving Dependencies\n--> Running transaction check\n---> Package vsftpd.x86_64 0:3.0.2-11.el7_2 will be erased\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nRemoving:\n vsftpd x86_64 3.0.2-11.el7_2 @updates 347 k\n\nTransaction Summary\n================================================================================\nRemove 1 Package\n\nInstalled size: 347 k\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Erasing : vsftpd-3.0.2-11.el7_2.x86_64 1/1 \n Verifying : vsftpd-3.0.2-11.el7_2.x86_64 1/1 \n\nRemoved:\n vsftpd.x86_64 0:3.0.2-11.el7_2 \n\nComplete!\n"
]
}
linuxmonty@linuxmonty-Latitude-E4310:~$ansible -i hosts production -m yum -a 'name=vsftpd state=absent' -u root
139.162.35.39 | success >> {
"changed": false,
"msg": "",
"rc": 0,
"results": [
 "vsftpd is not installed"
]
}

第一次运行ansible命令时,它删除了VsFTPD程序包,然后再次运行它以确认服务器中现在不存在任何程序包。

管理服务

有必要管理目标服务器上安装的服务。
Ansible提供模块服务来实现这一目标。
我们可以使用该模块来启用启动和启动/停止/重新启动服务。
请参阅每种情况的示例。

启动/启用服务

linuxmonty@linuxmonty-Latitude-E4310:~$ansible -i hosts all -m service -a 'name=nginx enabled=yes state=started' -u root
45.33.76.60 | success >> {
"changed": false,
"enabled": true,
"name": "nginx",
"state": "started"
}
139.162.35.39 | success >> {
"changed": false,
"enabled": true,
"name": "nginx",
"state": "started"
}

停止服务

linuxmonty@linuxmonty-Latitude-E4310:~$ansible -i hosts all -m service -a 'name=nginx state=stopped' -u root
139.162.35.39 | success >> {
"changed": true,
"name": "nginx",
"state": "stopped"
}
45.33.76.60 | success >> {
"changed": true,
"name": "nginx",
"state": "stopped"
}

重新启动服务

linuxmonty@linuxmonty-Latitude-E4310:~$ansible -i hosts all -m service -a 'name=nginx state=restarted' -u root
139.162.35.39 | success >> {
"changed": true,
"name": "nginx",
"state": "started"
}
45.33.76.60 | success >> {
"changed": true,
"name": "nginx",
"state": "started"
}

如我们所见,状态变量被修改为启动,重新启动和停止状态以管理服务。

剧本

剧本是Ansible的配置,部署和编排语言。
他们可以分配不同的角色,执行复制或者删除文件/文件夹之类的任务,利用成熟的模块来转移大部分功能,或者使用替代变量来使部署动态且可重复使用。

剧本定义了部署步骤和配置。
它们是模块化的,可以包含变量。
它们可用于协调多台计算机上的步骤。
它们是用简单的YAML文件(Ansible自动化语言)编写的配置文件。
它们可以包含多个任务,并且可以使用“成熟”模块。

这是一个简单的Playbook的示例。

linuxmonty@linuxmonty-Latitude-E4310:~$cat simpleplbook.yaml
--
- hosts: production
remote_user: root
tasks:
- name: Setup FTP
yum: pkg=vsftpd state=installed
- name: start FTP
service: name=vsftpd state=started enabled=yes

这是一个简单的剧本,具有以下两个任务:

  • 安装FTP服务器
  • 确保服务状态

让我们详细查看每个语句

主机:生产这将选择库存主机以启动此过程。

remote_user:root这指定用于在目标服务器上执行此过程的用户。

任务:
1.名称:设置FTP
2.Yum:pkg = vsftpd状态=已安装
3.名称:启动FTP
4.服务:名称= vsftpd状态=已启动已启用=是

这些指定了在运行此剧本时要执行的两个任务。
为了更清楚,我们可以将其分为四个语句。
第一条语句描述设置FTP服务器的任务,第二条语句通过选择/在目标服务器上安装程序包来执行该任务。
第三个语句描述了下一个任务,第四个语句通过启动FTP服务器并在启动时启用它来确保服务状态。

现在,让我们看一下该剧本的输出。

linuxmonty@linuxmonty-Latitude-E4310:~$ansible-playbook -i hosts simpleplbook.yaml
PLAY [production] *
GATHERING FACTS ***
ok: [139.162.35.39]
TASK: [Setup FTP] *
changed: [139.162.35.39]
TASK: [start FTP] *
changed: [139.162.35.39]
PLAY RECAP 
139.162.35.39 : ok=3 changed=2 unreachable=0 failed=0

我们可以看到,剧本是根据剧本中指定的任务顺序执行的。
首先,它选择库存,然后开始一个接一个地播放剧本。

应用程序部署

我将使用剧本来设置我的网络服务器。
我为“网络服务器”库存组创建了一个剧本。
请在下面查看我的剧本详细信息:

linuxmonty@linuxmonty-Latitude-E4310:~$cat webservers_setup.yaml
--
- hosts: webservers
vars:
- Welcomemsg: "Welcome to Ansible Application Deployment"
tasks:
- name: Setup Nginx
yum: pkg=nginx state=installed
- name: Copying the index page
template: src=index.html dest=/usr/share/nginx/html/index.html
- name: Enable the service on boot
service: name=nginx enabled=yes
- name: start Nginx
service: name=nginx state=started

现在,让我们从我的管理服务器上运行此剧本以进行部署。

linuxmonty@linuxmonty-Latitude-E4310:~$ansible-playbook -i hosts -s webservers_setup.yaml -u root
PLAY [webservers] *
GATHERING FACTS ***
ok: [139.162.35.39]
ok: [45.33.76.60]
TASK: [Setup Nginx] ***
changed: [139.162.35.39]
changed: [45.33.76.60]
TASK: [Copying the index page] 
changed: [139.162.35.39]
changed: [45.33.76.60]
TASK: [Enable the service on boot] 
changed: [139.162.35.39]
changed: [45.33.76.60]
TASK: [start Nginx] ***
changed: [139.162.35.39]
changed: [45.33.76.60]
PLAY RECAP 
139.162.35.39 : ok=5 changed=4 unreachable=0 failed=0
45.33.76.60 : ok=5 changed=4 unreachable=0 failed=0

从结果可以明显看出,这本剧本描述了四个任务。
运行完该手册后,我们可以通过在浏览器中检查目标服务器来确认状态。

现在,我计划向目标服务器添加一个PHP模块,即php-gd。
我可以编辑我的剧本以包括该任务,然后再次运行。
让我们看看现在会发生什么。
我修改过的剧本如下:

linuxmonty @ linuxmonty-Latitude-E4310:~$cat webservers_setup.yaml

- hosts: webservers
vars:
- Welcomemsg: "Welcome to Nginx default page"
- WelcomePHP: "PHP GD module enabled"
tasks:
- name: Setup Nginx
yum: pkg=nginx state=installed
- name: Copying the index page
template: src=index.html dest=/usr/share/nginx/html/index.html
- name: Enable the service on boot
service: name=nginx enabled=yes
- name: start Nginx
service: name=nginx state=started
- name: Setup PHP-GD
 yum: pkg=php-gd state=installed

如我们所见,我将这些突出显示的行添加到我的剧本中。
所以这就是现在的样子。

linuxmonty@linuxmonty-Latitude-E4310:~$ansible-playbook -i hosts -s webservers_setup.yaml -u root
PLAY [webservers] *
GATHERING FACTS ***
ok: [139.162.35.39]
ok: [45.33.76.60]
TASK: [Setup Nginx] ***
ok: [139.162.35.39]
ok: [45.33.76.60]
TASK: [Copying the index page]  
changed: [139.162.35.39]
changed: [45.33.76.60]
TASK: [Enable the service on boot] 
ok: [139.162.35.39]
ok: [45.33.76.60]
TASK: [start Nginx] ***
ok: [139.162.35.39]
ok: [45.33.76.60]
TASK: [Setup PHP-GD] ** 
changed: [45.33.76.60]
changed: [139.162.35.39]
PLAY RECAP 
139.162.35.39 : ok=6 changed=2 unreachable=0 failed=0
45.33.76.60 : ok=6 changed=2 unreachable=0 failed=0

在仔细分析此结果后,我们可以看到其中只有两个部分报告了对目标服务器的修改。
一种是修改索引文件,另一种是安装其他PHP模块。
现在,我们可以在浏览器中看到目标服务器的更改。

的角色

Ansible角色是一种特殊的剧本,完全独立且可移植。
角色包含任务,变量,配置模板和其他需要完成复杂业务流程的支持任务。
角色可用于简化更复杂的操作。
我们可以创建不同的角色,例如common,webservers,db_servers等,并根据不同的目的进行分类,只需提及角色即可将其包含在主要剧本中。
这就是我们创建角色的方式。

linuxmonty@linuxmonty-Latitude-E4310:~$ansible-galaxy init common
common was created successfully

现在,我创建了一个名为common的角色,以在所有目标服务器中执行一些常见任务。
每个角色都包含各自的任务,配置模板,变量,处理程序等。

total 40
drwxrwxr-x 9 linuxmonty linuxmonty 4096 Jan 13 14:06 ./
drwxr-xr-x 34 linuxmonty linuxmonty 4096 Jan 13 14:06 ../
drwxrwxr-x 2 linuxmonty linuxmonty 4096 Jan 13 14:06 defaults/
drwxrwxr-x 2 linuxmonty linuxmonty 4096 Jan 13 14:06 files/
drwxrwxr-x 2 linuxmonty linuxmonty 4096 Jan 13 14:06 handlers/
drwxrwxr-x 2 linuxmonty linuxmonty 4096 Jan 13 14:06 meta/
-rw-rw-r-- 1 linuxmonty linuxmonty 1336 Jan 13 14:06 README.md
drwxrwxr-x 2 linuxmonty linuxmonty 4096 Jan 13 14:06 tasks/
drwxrwxr-x 2 linuxmonty linuxmonty 4096 Jan 13 14:06 templates/
drwxrwxr-x 2 linuxmonty linuxmonty 4096 Jan 13 14:06 vars/

我们可以根据需要在每个文件夹中创建YAML文件。
稍后,我们只需在剧本中指定这些角色即可运行所有这些任务。
我们可以在此处获取有关Ansible角色的更多详细信息。