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/ansibfe/roles
创建ansibles文件
[devops@workstation ~]$ mkdir /home/devops/ansible
进入静态inventory文件中按要求编辑
[devops@workstation ansible]$ vim inventory
验证
[devops@workstation ansible]$ ansible-inventory -i inventory --graph
完成后配置清单文件
[devops@workstation ansible]$ vim ansible.cfg
创建角色目录
[devops@workstation ansible]$ mkdir roles
进入inventory文件中给所有人
[devops@workstation ansible]$ ansible all -m ping
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
- 启用此软件仓库
先启动机器
rht-vmctl start all
打开参考文件
[devops@workstation ansible]$ ansible-doc yum_repository
在文件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'
检验
[devops@workstation ansible]$ ansible all -a 'yum repolist'