1.安装并配置Ansible 在控制节点上安装并配置 Ansible, 要求如下:?
安装所需的钦件包
- ? 创建静态 inventory 文件 /home/devops/ansible/inventory, 要求如下:
- servera 属于dev 主机组
- serverb 属于 test 和 balancers 主机组
- serverc 和 serverd 满于 prod 主机组
- prod 主机组属于 Webserver 主机组
- 创建 ansible配置文件/home/devops/ansible/ansible.cfg , 要求如下 :
- 使用 /home/devaps/ansible/inventory 清单文件
- 角色 role目录存放在 /home/devops/ansible/roles
2.创建并运行 Ansibie ad-hoc 命令 创建一个 shell 脚本名为 adhoc.sh 用以运行 ad-hoc 命令 . 为每个受控节点配罝 yum仓库. 要求如下: 仓库1 : - ? Name: RH294_Base - ? Description: RH294 base software - ? Base urt: http://content.example.com/rhel8.0/x86_64/dvd/BaseOS - ? 需要验证钦件包 GPG 签名 - ? GPG key 在: /etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release - ? 启用此软件仓库 仓库 2: - ? Name: RH294_Stream - ? Description : RH294 stream software - ? Base url: http://content.example.com/rhel8.0/x86_64/dvd/AppStream - ? 需要验证软件包 GPG 签名 - ? GPG key 在: /etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release - ? 启用此软件仓库
1.安装并配置Ansible 在控制节点上安装并配置 Ansible, 要求如下:
(1)通过SSH连接到devops用户中
[student@workstation ~]$ ssh devops@workstation
(2)在当前环境中下载ansible
[devops@workstation ~]$ sudo yum install ansible -y
Last metadata expiration check: 0:41:52 ago on Sat 02 Apr 2022 03:35:12 PM GMT.
Package ansible-2.8.0-1.el8ae.noarch is already installed.
Dependencies resolved.
Nothing to do.
Complete!
(3)创建ansible目录,并在此目录创建inventory文件;
[devops@workstation ~]$ mkdir ansible
[devops@workstation ~]$ cd ansible
[devops@workstation ansible]$ vim inventory
(4)在inventory文件中创建相应的主机组
[devops@workstation ansible]$ vim inventory
[dev]
servera
[test]
serverb
[blancers]
serverb
[prod]
server[c:d]
[Webserver:children]
prod
(5)可以通过测试查看创建的主机组
[devops@workstation ansible]$ ansible-inventory -i inventory --graph
@all:
|--@Webserver:
| |--@prod:
| | |--serverc
| | |--serverd
|--@blancers:
| |--serverb
|--@dev:
| |--servera
|--@test:
| |--serverb
|--@ungrouped:
(6)创建ansible相应配置文件和使用清单文件和存放role目录,创建角色目录
[devops@workstation ansible]$ vim ansible.cfg
[defaults]
inventory = /home/devops/ansible/inventory
roles_path = /home/devops/ansible/roles
host_key_checking = False
[devops@workstation ansible]$ mkdir roles
(7)在配置主机中的文件中添加变量
[devops@workstation ansible]$ vim inventory
[dev]
servera
[test]
serverb
[blancers]
serverb
[prod]
server[c:d]
[Webserver:children]
prod
[all:vars]
ansible_user=root
ansible_password=redhat
(8)可以验证测试配置——测试成功
[devops@workstation ansible]$ ansible-inventory --graph
@all:
|--@Webserver:
| |--@prod:
| | |--serverc
| | |--serverd
|--@blancers:
| |--serverb
|--@dev:
| |--servera
|--@test:
| |--serverb
|--@ungrouped:
[devops@workstation ansible]$ ansible all -m ping
servera | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
serverd | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
serverc | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
serverb | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
2.创建并运行 Ansibie ad-hoc 命令 创建一个 shell 脚本名为 adhoc.sh 用以运行 ad-hoc 命令 . 为每个受控节点配罝 yum仓库.
(1)编写shell脚本; 安照需求配置
[devops@workstation ansible]$ vim adhoc.sh
ansible all -m yum_repository \
-a 'name="RH294_Base" description="RH294 base software" \
baseurl=http://content.example.com/rhel8.0/x86_64/dvd/BaseOS \
gpgcheck=yes \
gpgkey=/etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release \
enabled=yes'
ansible all -m yum_repository \
-a 'name="RH294_Stream" description="RH294 stream software" \
baseurl=http://content.example.com/rhel8.0/x86_64/dvd/AppStream \
gpgcheck=yes \
gpgkey=/etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release \
enabled=yes'
(2)为脚本添加执行权限并执行权限;
[devops@workstation ansible]$ chmod +x adhoc.sh
[devops@workstation ansible]$ ./adhoc.sh
servera | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"repo": "RH294_Base",
"state": "present"
}
serverc | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"repo": "RH294_Base",
"state": "present"
}
serverb | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"repo": "RH294_Base",
"state": "present"
}
serverd | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"repo": "RH294_Base",
"state": "present"
}
serverc | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"repo": "RH294_Stream",
"state": "present"
}
servera | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"repo": "RH294_Stream",
"state": "present"
}
serverb | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"repo": "RH294_Stream",
"state": "present"
}
serverd | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"repo": "RH294_Stream",
"state": "present"
}
(3)检查配置;
[devops@workstation ansible]$ ansible all -a 'yum repolist'
[WARNING]: Consider using the yum module rather than running 'yum'. If you need to use command
because yum is insufficient you can add 'warn: false' to this command task or set
'command_warnings=False' in ansible.cfg to get rid of this message.
servera | CHANGED | rc=0 >>
repo id repo name status
RH294_Base RH294 base software 1,658
RH294_Stream RH294 stream software 4,672
rhel-8.0-for-x86_64-appstream-rpms Red Hat Enterprise Linux 8.0 AppStream 4,672
rhel-8.0-for-x86_64-baseos-rpms Red Hat Enterprise Linux 8.0 BaseOS (d 1,658RH294 base software 12 MB/s | 2.2 MB 00:00
RH294 stream software 31 MB/s | 5.3 MB 00:00
Red Hat Enterprise Linux 8.0 AppStream (dvd) 90 kB/s | 3.2 kB 00:00
Red Hat Enterprise Linux 8.0 BaseOS (dvd) 140 kB/s | 2.7 kB 00:00
serverc | CHANGED | rc=0 >>
repo id repo name status
RH294_Base RH294 base software 1,658
RH294_Stream RH294 stream software 4,672
rhel-8.0-for-x86_64-appstream-rpms Red Hat Enterprise Linux 8.0 AppStream 4,672
rhel-8.0-for-x86_64-baseos-rpms Red Hat Enterprise Linux 8.0 BaseOS (d 1,658RH294 base software 2.7 MB/s | 2.2 MB 00:00
RH294 stream software 7.4 MB/s | 5.3 MB 00:00
Red Hat Enterprise Linux 8.0 AppStream (dvd) 71 kB/s | 3.2 kB 00:00
Red Hat Enterprise Linux 8.0 BaseOS (dvd) 41 kB/s | 2.7 kB 00:00
serverd | CHANGED | rc=0 >>
repo id repo name status
RH294_Base RH294 base software 1,658
RH294_Stream RH294 stream software 4,672
rhel-8.0-for-x86_64-appstream-rpms Red Hat Enterprise Linux 8.0 AppStream 4,672
rhel-8.0-for-x86_64-baseos-rpms Red Hat Enterprise Linux 8.0 BaseOS (d 1,658RH294 base software 7.5 MB/s | 2.2 MB 00:00
RH294 stream software 18 MB/s | 5.3 MB 00:00
Red Hat Enterprise Linux 8.0 AppStream (dvd) 50 kB/s | 3.2 kB 00:00
Red Hat Enterprise Linux 8.0 BaseOS (dvd) 37 kB/s | 2.7 kB 00:00
serverb | CHANGED | rc=0 >>
repo id repo name status
RH294_Base RH294 base software 1,658
RH294_Stream RH294 stream software 4,672
rhel-8.0-for-x86_64-appstream-rpms Red Hat Enterprise Linux 8.0 AppStream 4,672
rhel-8.0-for-x86_64-baseos-rpms Red Hat Enterprise Linux 8.0 BaseOS (d 1,658RH294 base software 6.3 MB/s | 2.2 MB 00:00
RH294 stream software 9.8 MB/s | 5.3 MB 00:00
Last metadata expiration check: 0:00:02 ago on Sun 03 Apr 2022 04:53:39 PM GMT.
|