一、构建学习环境:
student用户在控制节点(workstation)上安装并配置 Ansible, 要求如下 :
1、安装所需的软件包
2、创建静态inventory文件/home/student/ansible/inventory,要求如下:
servera属于dev主机组
serverb属于test和balancers主机组
serverc和serverd属于prod主机组
prod主机组属于webservers主机组
3、创建ansible配置文件/home/student/ansible/ansible.cfg,要求如下:
使用/home/student/ansible/inventory清单文件
角色存放在/home/student/ansible/roles/ 目录
一. 首先启用环境
1.启用其他服务器:rht-vmctl start all
2.查看状态:rht-vmctl status all
二.,安装ansible
三,创建文件和部署
mkdir /home/student/ansible
[student@workstation ~]$ cat /home/student/ansible/inventory
[dev]
servera
[test]
serverb
[balancers]
serverb
[prod]
serverc
serverd
[webservers:children]
prod
[student@workstation ~]$ cp /etc/ansible/ansible.cfg /home/student/ansible/
[defaults]
inventory = /home/student/ansible/inventory 默认清单
roles_path = /home/student/ansible/roles/ 角色存放位置
host_key_checking = False
[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False
创建roles路径:
[student@workstation ansible]$ mkdir /home/student/ansible/roles
1. 验证清单文件
[student@workstation ansible]$ ansible-inventory --graph
2. 验证配置
ansilbe all -m ping
二、创建一个 shell 脚本名为 adhoc.sh 用以运行 ad-hoc 命令 。为每个受控节点配罝 yum仓库。要求如下:
仓库1 :
Name: RH294_Base
Description: RH294 base software
Base url: 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
启用此软件仓库
编写脚本:
[student@workstation ansible]$ cat adhoc.sh
#!/bin/bash
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'
增加执行权限,运行脚本验证:
[student@workstation ansible]$ chmod +x adhoc.sh
[student@workstation ansible]$ ./adhoc.sh