Ansible的练习
环境介绍
系统 | IP 地址 | Ansible 角色 |
---|---|---|
bastion | 172.25.250.254 | ansible 的控制节点 |
workstation | 172.25.250.9 | 用于进行系统管理的图形工作站 |
servera | 172.25.250.10 | 通过ansible管理的主机 |
serverb | 172.25.250.11 | 通过ansible管理的主机 |
serverc | 172.25.250.12 | 通过ansible管理的主机 |
serverd | 172.25.250.13 | 通过ansible管理的主机 |
重要信息
编写的 Ansible playbook 将通过以 greg
用户身份从控制节点上的目录 /home/greg/ansible 目录运行来应用。在 playbook 运行后,系统会对您的受管节点进行评估,以判断它们是否按照规定进行了配置
进入foundation
[kiosk@foundation0 ~]$ rht-vmctl status all
bastion DEFINED
workstation DEFINED
servera DEFINED
serverb DEFINED
serverc DEFINED
serverd DEFINED
[kiosk@foundation0 ~]$ rht-vmctl start all
Starting bastion.
Starting workstation.
Starting servera.
Starting serverb.
Starting serverc.
Starting serverd.
开启所有服务器
挂载ISO2.ios
在foundation0上
[kiosk@foundation0 init]$ ssh root@localhost 'yum -y install /run/media/kiosk/20191229_164114/ex300v8-1.3.0-191229.x86_64.rpm'
cd /home/kiosk/ex300/roles/init/files
将两个Ansible Galaxy 角色和硬件报告模板复制进materials目录里
[kiosk@foundation0 files]$ ssh root@localhost 'cp /home/kiosk/ex300/roles/init/files/haproxy.tar /home/kiosk/ex300/roles/init/files/phpinfo.tar /home/kiosk/ex300/roles/init/files/hwreport.empty /content/courses/rh294/rhel8.0/materials/'
[kiosk@foundation0 files]$ ls /content/courses/rh294/rhel8.0/materials
Ansible-Tower-license.txt dynamic jinja2 roles-library yum
ansible-vim.tar.gz grading labs solutions yum.conf.d
classroom haproxy.tar phpinfo.tar tower
docs hwreport.empty playbooks troubleshooting
连接bastion
ssh root@172.25.254.250
[root@bastion ~]# yum install ansible
创建greg
[root@bastion ~]# useradd greg
[root@bastion ~]# passwd greg
考试要求
安装和配置 Ansible
按照下方所述,在控制节点 172.25.250.254 上安装和配置 Ansible:
-
安装所需的软件包
-
创建名为 /home/greg/ansible/inventory 的静态清单文件,以满足以下要求:
-
172.25.250.9 是 dev 主机组的成员
-
172.25.250.10 是 test 主机组的成员
-
172.25.250.11 和 172.25.250.12 是 prod 主机组的成员
-
172.25.250.13 是 balancers 主机组的成员
-
prod 组是 webservers 主机组的成员
-
-
创建名为 /home/greg/ansible/ansible.cfg 的配置文件,以满足以下要求:
-
主机清单文件为 /home/greg/ansible/inventory
-
playbook 中使用的角色的位置包括 /home/greg/ansible/roles
-
解答
[root@bastion ~]# su - greg
创建ansible的目录
[greg@bastion ~]$ mkdir ansible
查看ansible版本
[greg@bastion ansible]$ ansible --version
ansible 2.8.0
config file = /etc/ansible/ansible.cfg
发现config文件是/etc/下面的
这里需要说明一下:
ansible的配置文件有四种优先级
参数命令>当前目录的配置文件>家目录的配置文件>/etc/下的配置文件
因此我们将配置文件拷贝到家目录里
[greg@bastion ansible]$ cp /etc/ansible/ansible.cfg ./
同时查找主机清单的路径
[greg@bastion ansible]$ rpm -qc ansible
/etc/ansible/ansible.cfg
/etc/ansible/hosts
拷贝主机清单
[greg@bastion ansible]$ cp /etc/ansible/hosts ./inventory
[greg@bastion ansible]$ ansible --version
ansible 2.8.0
config file = /home/greg/ansible/ansible.cfg
配置文件优先级变更成家目录了
创建角色的目录
[greg@bastion ansible]$ mkdir roles
[greg@bastion ansible]$ vim ansible.cfg
# 主机清单
inventory = /home/greg/ansible/inventory
# 角色目录
roles_path = /home/greg/ansible/roles
# 表示受控端在执行主控端下达的命令时用什么身份进行执行 因此是root
remote_user = root
# 主机密钥检查性关闭,管理端就不会进行ssh密钥验证了
host_key_checking = False
# 受控端执行ansible用户提权的参数
[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False
配置主机清单
[greg@bastion ansible]$ vim inventory
[dev]
172.25.250.9
[test]
172.25.250.10
[prod]
172.25.250.11
172.25.250.12
[balancers]
172.25.250.13
[webservers:children]
prod
[greg@bastion ansible]$ ansible-inventory --graph
@all:
|--@balancers:
| |--172.25.250.13
|--@dev:
| |--172.25.250.9
|--@test:
| |--172.25.250.10
|--@ungrouped:
|--@webservers:
| |--@prod:
| | |--172.25.250.11
| | |--172.25.250.12
主机清单已经配置完毕
我们测试能否ping通节点服务器
-k 是输入密码进行验证 密码是redhat
[greg@bastion ansible]$ ansible all -m ping -k
SSH password:
172.25.250.12 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
在主机清单下面添加自动连接的密码
[greg@bastion ansible]$ vim inventory
[all:vars]
ansible_password=redhat
验证
主机清单
[greg@bastion ansible]$ ansible-inventory --graph
@all:
|--@balancers:
| |--172.25.250.13
|--@dev:
| |--172.25.250.9
|--@test:
| |--172.25.250.10
|--@ungrouped:
|--@webservers:
| |--@prod:
| | |--172.25.250.11
| | |--172.25.250.12
ping所有节点
[greg@bastion ansible]$ ansible all -m ping
172.25.250.11 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
172.25.250.12 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
172.25.250.10 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
172.25.250.13 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
172.25.250.9 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
创建和运行 Ansible 临时命令
作为系统管理员,您需要在受管节点上安装软件。
请按照正文所述,创建一个名为 /home/greg/ansible/adhoc.sh 的 shell 脚本,该脚本将使用 Ansible 临时命令在各个受管节点上安装 yum 存储库:
-
存储库1:
-
存储库的名称为 EX294_BASE
-
描述为 EX294 base software
-
基础 URL 为 http://content/rhel8.0/x86_64/dvd/BaseOS
-
GPG 签名检查为启用状态
-
GPG 密钥 URL 为 http://content/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release
-
存储库为启用状态
-
-
存储库2:
-
存储库的名称为 EX294_STREAM
-
描述为 EX294 stream software
-
基础 URL 为 http://content/rhel8.0/x86_64/dvd/AppStream
-
GPG 签名检查为启用状态
-
GPG 密钥 URL 为 http://content/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release
-
存储库为启用状态
-
解答
因为实现安装 yum 存储库,当接触一个不了解的模块时,我通常的做法就