ansible自动化工具
文章目录
环境
system | IP Address | Role |
---|---|---|
workstation.lab.example.com | 172.25.250.9 | Ansible control node |
servera.lab.example.com | 172.25.250.10 | Ansible managed node |
serverb.lab.example.com | 172.25.250.11 | Ansible managed nod |
serverc.lab.example.com | 172.25.250.12 | Ansible managed node |
serverd.lab.example.com | 172.25.250.13 | Ansible managed node |
bastion.lab.example.com | 172.25.250.254 | Ansible managed node |
workstation为ansible节点
servera、serverb、serverc、serverd、bastion为受控主机
已经全部配置好ssh的基于密钥认证
Ansible 控制节点上已创建了用户帐户 student。此帐户预装了 SSH密钥,
允许在 Ansible 控制节点和各个 Ansible 受管节点之间进行SSH 登录。
请勿对系统上的 student SSH 配置文件进行任何修改。
您可以从 root 帐户使用 su 访问此用户帐户
开启虚拟机:
[root@foundation ~]# virt-manager
[root@foundation ~]# rht-vmctl reset all
⼀、安装和配置Ansible
按照下⽅所述,在控制节点workstation.lab.example.com 上安装和配置 Ansible:
- 安装所需的软件包
- 创建名为/home/student/ansible/inventory的静态清单⽂件, 以满⾜以下需求:
servera是dev主机组的成员
serverb是test主机组的成员
serverc和serverd是prod主机组的成员
bastion是balancers主机组的成员
prod组是webservers主机组的成员 - 创建名为/home/student/ansible/ansible.cfg的配置⽂件, 以满⾜以下要求:
主机清单⽂件为/home/student/ansible/inventory
playbook中使⽤的⻆⾊的位置包括/home/student/ansible/roles
# 准备⼯作,由于教室环境中bastion虚拟机的⽤户devops未创建,需要创建,并设置sudo提权,和免密登陆
,考试已经做好了。
[kiosk@foundation0 ~]$ ssh root@workstation
[root@workstation ~]# ssh root@bastion "useradd devops; echo redhat |passwd --stdin
devops"
[root@workstation ~]# for i in server{a..d} bastion;do ssh root@$i "echo 'devops AL
L=(ALL) NOPASSWD: ALL' > /etc/sudoers.d/devops";done
[student@workstation ~]$ ssh devops@bastion #免密登陆成功
# 实际操作
[root@workstation ~]# yum -y install ansible #若考试已经安装好了,则不需要安装了
[root@workstation ~]# su - student #考试要求所有的配置都⽤⼀个普通⽤户进⾏配置
[student@workstation ~]$ mkdir ansible
[student@workstation ~]$ cd ansible/
[student@workstation ansible]$ vim inventory
[dev]
servera
[test]
serverb
[prod]
serverc
serverd
[balancers]
bastion
[webservers:children]
prod
[student@workstation ansible]$ cp /etc/ansible/ansible.cfg .
[student@workstation ansible]$ vim ansible.cfg
[defaults]
inventory = /home/student/ansible/inventory
remote_user = devops
roles_path = /home/student/ansible/roles
[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False
//验证
[student@workstation ansible]$ ansible all -m ping
二、创建和运行Ansible临时命令
作为系统管理员, 您需要在受管节点上安装软件.
请按照下方所述, 创建一个名为/home/student/ansible/adhoc.sh的shell脚本,
该脚本将使用Ansible临时命令在各个受管节点上安装yum存储库:
存储库1:
存储库的名称为 rh294_BASE
描述为 rh294 base software
基础URL为 http://content.example.com/rhel8.0/x86_64/dvd/BaseOS
GPG签名检查为启用状态
GPG密钥URL为 http://content.example.com/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release
存储库为开启状态
存储库2:
存储库的名称为 rh294_STREAM
描述为 rh294 stream software
基础URL为 http://content.example.com/rhel8.0/x86_64/dvd/AppStream
GPG签名检查为启用状态
GPG密钥URL为 http://content.example.com/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release
存储库为开启状态
[student@workstation ansible]$ vim adhoc.sh
#!/bin/bash
ansible all -m yum_repository -a "name=rh294_BASE description='rh294 base software'
file=rhel_dvd baseurl=http://content.example.com/rhel8.0/x86_64/dvd/BaseOS gpgcheck=yes
gpgkey=http://content.example.com/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release enabled=yes" //此处为一条命令,不能回车到下一行
ansible all -m yum_repository -a "name=rh294_STREAM description='rh294 stream software'
file=rhel_dvd baseurl=http://content.example.com/rhel8.0/x86_64/dvd/AppStream
gpgcheck=yes gpgkey=http://content.example.com/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release enabled=yes"
[student@workstation ansible]$ chmod +x adhoc.sh
[student@workstation ansible]$ ./adhoc.sh
三、安装软件包
创建⼀个名为 /home/student/ansible/packages.yml的 playbook:
- 将 php 和 mariadb 软件包安装到 dev、test 和 prod 主机组中的主机上
- 将 Development Tools 软件包组安装到 dev 主机组中的主机上
- 将 dev 主机组中主机上的所有软件包更新为最新版本
[student@workstation ansible]$ vim packages.yml
- name: install pkgs
hosts: dev, test, prod
tasks:
- name: install mariadb php
yum: