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/devops/ansible/inventory 清单文件
-
角色 role目录存放在 /home/devops/ansible/roles
解答:
1.安装ansible
[kiosk@foundation0 ~]$ rht-vmctl start all
[devops@workstation ~]$ sudo yum install ansible -y
Ansible 2.8 1.1 MB/s | 64 kB 00:00
Red Hat Enterprise Linux 8.0 6.3 MB/s | 525 kB 00:00
Red Hat Enterprise Linux 8.0 12 MB/s | 1.1 MB 00:00
Red Hat Enterprise Linux 8.0 69 MB/s | 5.3 MB 00:00
Red Hat Enterprise Linux 8.0 57 MB/s | 2.2 MB 00:00
Package ansible-2.8.0-1.el8ae.noarch is already installed.
Dependencies resolved.
Nothing to do.
Complete!
2.写清单文件
查看路径
[devops@workstation ~]$ pwd
/home/devops
创建目录并写清单
[devops@workstation ~]$ mkdir ansible
[devops@workstation ~]$ cd ansible
[devops@workstation ansible]$ vim inventory
[dev]
servera
[test]
serverb
[balancers]
serverb
[prod]
server[c:d]
[Webserver:children]
prod
[all:vars]
ansible_user=root
ansible_password=redhat
再起一台主机:sudo vim /etc/ansible/ansible.cfg
配置清单文件
[devops@workstation ansible]$ mkdir roles
[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]$ ansible-inventory --graph
[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
- 启用此软件仓库
先查看模块名字:
[devops@workstation ansible]$ ansible-doc -l | grep yum
yum Manages packages with the `yum' package manager
yum_repository Add or remove YUM repositories
查出模块名为:yum_repository
查看帮助:
[devops@workstation ansible]$ ansible-doc yum_repository
写Shell脚本:
[devops@workstation ansible]$ vim 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]$ chmod +x adhoc.sh
运行:
[devops@workstation ansible]$ ./adhoc.sh
检查:
[devops@workstation ansible]$ ansible all -a 'yum repolist'