搭建环境

本文环境我们使用3台 CentOS 7 虚拟机:

Ansible 控制节点:

  • 主机名 - ansible-01.example.com
  • IP 地址 - 192.168.116.201 /24
  • 操作系统 - CentOS 7.2

Lighttpd web服务器:

  • 主机名 - lighttpd-01.example.com
  • IP 地址 - 192.168.116.202 /24
  • 操作系统 - CentOS 7.2

MariaDB 数据库服务器:

  • 主机名 - mariadb-01.example.com
  • IP 地址 - 192.168.116.203 /24
  • 操作系统 - CentOS 7.2

在这里,我们将在 ansible-01.example.com 上安装 Ansible,并将其他两台服务器配置为 Ansible 托管节点。

在 CentOS 7 上创建 Ansible Inventory:

文件 /etc/ansible/hosts 保存 Ansible 受管节点的列表。

[root@ansible-01 ~]# vi /etc/ansible/hosts

在此文件中添加 lighttpd-01 和 mariadb-01 节点。

[centos72-servers]
lighttpd-01.example.com
mariadb-01.example.com
[lighttpd-webservers]
lighttpd-01.example.com
[mariadb-dbserver]
mariadb-01.example.com

我们在这里创建了三个组。

  • centos72-servers - 一组基于 CentOS 7.2 的服务器,因此我们可以使用单个命令配置我们所有的 CentOS 7.2 服务器。
  • lighttpd-servers - 基于 Lighttpd 的 Web 服务器组。
  • mariadb-servers - 基于 MariaDB 的数据库服务器组。

在这里,我们可以根据需要创建任意数量的组。

我们已经在 CentOS 7 上成功安装了 Ansible,并在 Ansible Inventory 中添加了两台 Linux 服务器。

如何在 CentOS 7 上安装 Ansible 并使用 Playbooks

在本文中,我们将在 CentOS 7 上安装 Ansible,然后编写并执行 Ansible playbook 以使用 Ansible 配置我们的 Linux 服务器。

在 CentOS 7 上为基于密钥的身份验证配置 SSH:

使用 ssh-keygen 命令在 ansible-01.example.com 服务器上生成 SSH 密钥。

[root@ansible-01 ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 回车
Created directory '/root/.ssh'.
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.

将 SSH 公钥复制到 lighttpd-01 和 mariadb-01 服务器。

[root@ansible-01 ~]# ssh-copy-id root@lighttpd-01
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'lighttpd-01 (192.168.116.202)' can't be established.
ECDSA key fingerprint is SHA256:kzyCimDDwGPsfsuGXxdrcBqlxVQlU8FZTsYrwbPzZHM.
ECDSA key fingerprint is MD5:c7:3f:a2:82:33:7a:b7:d7:b3:b0:12:8f:a3:1e:8a:bc.
Are you sure you want to continue connecting (yes/no)? 输入yes
/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@lighttpd-01's password:  服务器lighttpd的root密码
Number of key(s) added: 1
Now try logging into the machine, with:   "ssh 'root@lighttpd-01'"
and check to make sure that only the key(s) you wanted were added.

[root@ansible-01 ~]# ssh-copy-id root@mariadb-01

连接到每个服务器,验证 SSH 身份验证。

[root@ansible-01 ~]# ssh root@lighttpd-01

[root@ansible-01 ~]# ssh root@mariadb-01

配置 Linux 服务器的名称解析:

如果我们为域配置了权威 DNS 服务器,那么我们可以其中添加所有三个服务器的 RR(资源记录)。

否则,我们必须在本地 DNS 解析器 (/etc/hosts) 文件中添加名称解析条目。

[root@ansible-01 ~]# cat >> /etc/hosts << EOF
> 192.168.116.201 ansible-01.example.com ansible-01
> 192.168.116.202 lighttpd-01.example.com lighttpd-01
> 192.168.116.203 mariadb-01.example.com mariadb-01
> EOF

在 lightttpd-01 和 mariadb-01 机器上重复上述命令。

使用 Ansible Playbooks 配置 CentOS 7 节点:

虽然,我们可以在 adhoc 模式下执行命令(上节所示)来配置我们的节点。
但是,它违反了配置管理的概念。

因此,我们将编写 Playbooks(YAML 脚本)以在我们的节点上执行一致的配置。

下面的Playbook 将执行一些初始配置,例如创建用户,安装一些软件包,并在 centos72-servers 组上执行一些配置。

[root@ansible-01 ~]# vi centos72_servers_initial_conf.yaml

并添加以下 YAML 脚本。

--
 - hosts: centos72-servers
   user: root
   tasks:
    - name: Installing Common Packages
      action: yum name=wget,bzip2 state=installed

    - name: Create an Admin User
      user:
       name: "JackLiu"
       groups: "wheel"
       password: "{{ '123' | password_hash('sha512') }}"

保存并退出 vim 编辑器。

使用 ansible-playbook 命令执行此playbook。

[root@ansible-01 ~]# ansible-playbook centos72_servers_initial_conf.yaml

PLAY [centos72-servers] 
TASK [Gathering Facts] *
ok: [lighttpd-01.example.com]
ok: [mariadb-01.example.com]
TASK [Installing Common Packages] **
changed: [lighttpd-01.example.com]
changed: [mariadb-01.example.com]
TASK [Create an Admin User] 
changed: [mariadb-01.example.com]
changed: [lighttpd-01.example.com]
PLAY RECAP *
lighttpd-01.example.com    : ok=3    changed=2    unreachable=0    failed=0
mariadb-01.example.com     : ok=3    changed=2    unreachable=0    failed=0

playbook成功执行,没有任何错误,并且在两个节点上都执行了所需的配置。

现在创建另一个playbook 来使用 ansible 命令安装 Lighttpd Web 服务器。

[root@ansible-01 ~]# vi lighttpd_servers.yaml

其中添加以下 YAML 脚本。

--
 - hosts: lighttpd-webservers
   user: root
   vars:
    myhomepage: '<html><h1>Apache installed using Ansible</h1></html>'
   tasks:
    - name: Installing EPEL yum Repository
      action: yum name=epel-release state=installed
    - name: Installing Lighttpd Server
      action: yum name=lighttpd state=installed
    - name: Configure Lighttpd Server
      replace:
       path: /etc/lighttpd/lighttpd.conf
       regexp: 'server.use-ipv6 = "enable"'
       replace: 'server.use-ipv6 = "disable"'
       backup: yes
    - name: Create Index.html File.
      copy:
       dest: /var/www/lighttpd/index.html
       content: '{{ myhomepage }}'
       backup: yes
    - name: Allow HTTPS Service in Linux Firewall
      firewalld:
       service: http
       permanent: yes
       state: enabled
    - name: Restart Lighttpd service
      service:
       name: lighttpd
       enabled: yes
       state: restarted
    - name: Restart Firewalld service
      service:
       name: firewalld
       state: restarted

执行此playbook如下所示:

[root@ansible-01 ~]# ansible-playbook lighttpd_servers.yaml

PLAY [lighttpd-webservers] *
TASK [Gathering Facts] *
ok: [lighttpd-01.example.com]
TASK [Installing EPEL yum Repository] **
ok: [lighttpd-01.example.com]
TASK [Installing Lighttpd Server] **
ok: [lighttpd-01.example.com]
TASK [Configure Lighttpd Server] ***
changed: [lighttpd-01.example.com]
TASK [Create Index.html File.] *
ok: [lighttpd-01.example.com]
TASK [Allow HTTPS Service in Linux Firewall] ***
ok: [lighttpd-01.example.com]
TASK [Restart Lighttpd service] 
changed: [lighttpd-01.example.com]
TASK [Restart Firewalld service] ***
changed: [lighttpd-01.example.com]
PLAY RECAP *
lighttpd-01.example.com    : ok=8    changed=3    unreachable=0    failed=0

使用 curl 命令验证我们的 Lighttpd Web 服务器。

[root@ansible-01 ~]# curl http://lighttpd-01.example.com
<html><h1>Apache installed using Ansible</h1></html>

我们已经在 CentOS 7 上成功安装 Ansible 并使用 playbook 来配置托管节点。

on  it road.com

在 CentOS 7 上安装 Ansible

登录到: ansible-01.example.com。

Ansible 在 extras yum 存储库中可用,因此,我们可以使用 yum 命令安装它。

[root@ansible-01 ~]# yum install -y ansible

检查 Ansible 软件的版本。

[root@ansible-01 ~]# ansible --version

  python version = 2.7.5 (default, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]

在 Adhoc 模式下配置 Ansible 受管节点:

现在使用 ansible 命令 ping 一组服务器。

[root@ansible-01 ~]# ansible -m ping 'centos72-servers'
lighttpd-01.example.com | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
mariadb-01.example.com | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

同样,我们可以使用以下语法执行任何命令。

[root@ansible-01 ~]# ansible -m command -a 'cat /etc/redhat-release' 'centos72-servers'
192.168.116.203 | SUCCESS | rc=0 >>
CentOS Linux release 7.2.1810 (Core)
192.168.116.202 | SUCCESS | rc=0 >>
CentOS Linux release 7.2.1810 (Core)
日期:2020-09-17 00:16:42 来源:oir作者:oir