解答:[student@workstation ansible]$ vim adhoc.sh#!/bin/bashansible 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=yesgpgkey=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/AppStreamgpgcheck=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
3、安装软件包创建一个名为 /home/student/ansible/packages.yml的 playbook:将 php 和 mariadb 软件包安装到 dev、test 和 prod 主机组中的主机上将 RPM Development Tools 软件包组安装到 dev主机组中的主机上将 dev 主机组中主机上的所有软件包更新为最新版本
解答:[student@workstation ansible]$ vim packages.yml---- name: install pkgshosts: dev, test, prodtasks:- name: install mariadb phpyum:name:- php- mariadbstate: present- name: install group pkgshosts: devtasks:- name: install Development Toolsyum:name: "@Development Tools"state: present- name: update all pkgshosts: devtasks:- name: update pkgsyum:name: '*'state: latest[student@workstation ansible]$ ansible-playbook packages.yml
4、使用RHEL系统角色安装 RHEL 系统角色软件包,并创建符合以下条件的playbook /home/student/ansible/timesync.yml:在所有受管节点上运行使用 timesync 角色配置该角色,以使用当前有效的 NTP 提供商配置该角色,以使用时间服务器 classroom.example.com配置该角色,以启用 iburst 参数
解答:[student@workstation ansible]$ sudo yum -y install rhel-system-roles[student@workstation ansible]$ mkdir roles[student@workstation ansible]$ cp -r /usr/share/ansible/roles/rhel-system-roles.timesync//home/student/ansible/roles/timesync[student@workstation ansible]$ vim timesync.yml---- name: set time synchosts: allvars:timesync_ntp_servers:- hostname: classroom.example.comiburst: yesroles:- timesync[student@workstation ansible]$ ansible-playbook timesync.yml使用selinux角色配置该角色,开启所有受控节点的selinux[student@workstation ansible]$ cp -r /usr/share/ansible/roles/rhel-system-roles.selinux/home/student/ansible/roles/selinuxvim selinux.yml---- name: set selinuxhosts: allvars:selinux_state: enforcingroles:- role: selinuxbecome: true[student@workstation ansible]$ ansible-playbook selinux.yml
5、使用Ansible Galaxy安装角色使用 Ansible Galaxy 和要求文件 /home/student/ansible/roles/requirements.yml,从以下 URL 下载角色并安装到 /home/student/ansible/roles:http://content.example.com/haproxy.tar.gz 此角色的名称应当为 balancerhttp://content.example.com/phpinfo.tar.gz 此角色的名称应当为 phpinfo
解答:[student@workstation ansible]$ vim roles/requirements.yml---- name: balancersrc: http://content.example.com/ansible2.8/haproxy.tar.gz- name: phpinfosrc: http://content.example.com/ansible2.8/phpinfo.tar.gz[student@workstation ansible]$ ansible-galaxy install -r /home/student/asnible/roles/requirements.yml -p /home/student/ansible/roles/
6、创建和使用角色根据下列要求,在/home/student/ansible/roles中创建名为apache的角色:httpd软件包已安装,设为在系统启动时启用并启动防火墙已启用并正在运行,并使用允许访问Web服务器的规则模板文件 index.html.j2 已存在,用于创建具有以下输出的文件/var/www/html/index.html:Welcome to HOSTNAME on IPADDRESS其中,HOSTNAME是受管节点的完全限定域名,IPADDRESS则是受管节点的IP地址 。按照下方所述,创建一个使用此角色的playbook /home/student/ansible/newrole.yml:该playbook在webservers主机组中的主机上运行
解答:[student@workstation ansible]$ cd roles/[student@workstation roles]$ ansible-galaxy init apache[student@workstation roles]$ vim http/tasks/main.yml---# tasks file for http- name: install httpd firewalldyum:name:- httpd- firewalldstate: present- name: cp filetemplate:src: index.html.j2dest: /var/www/html/index.html- name: start httpdservice:name: httpdstate: startedenabled: yes- name: restart firewalldservice:name: firewalldstate: restartedenabled: yes - name: firewalld for httpfirewalld:service: httpstate: enabledpermanent: yesimmediate: yes[student@workstation roles]$ vim http/templates/index.html.j2Welcome to {{ansible_fqdn}} on {{ansible_enp1s0.ipv4.address}} [student@workstation ansible]$ vim newrole.yml---- name: use http rolehosts: webserversroles:- apache[student@workstation ansible]$ ansible-playbook newrole.yml验证结果:[student@workstation ansible]$ curl http://servercWelcome to serverc.lab.example.com on 172.25.250.12[student@workstation ansible]$ curl http://serverdWelcome to serverd.lab.example.com on 172.25.250.13