#linux ansible角色
文章目录
在RH294环境中完成以下题目
1、使用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]$ cp -r /usr/share/ansible/roles/rhel-system-roles.timesync/ /home/student/ansible/roles/timesync
[student@workstation ansible]$ vim timesync.yml
---
- name: set time sync
hosts: all
vars:
timesync_ntp_servers:
- hostname: classroom.example.com
iburst: yes
roles:
- timesync
[student@workstation ansible]$ ansible-playbook timesync.yml
2、使用selinux角色
配置该角色,编写selinux.yml的playbook开启所有受控节点的selinux
[student@workstation ansible]$ cp -r /usr/share/ansible/roles/rhel-system-roles.selinux /home/student/ansible/roles/selinux
[student@workstation ansible]$ ansible-galaxy list
# /home/student/ansible/roles
- timesync, (unknown version)
- selinux, (unknown version)
[student@workstation ansible]$ vim selinux.yml
---
- name: set selinux
hosts: all
vars:
selinux_state: enforcing
roles:
- role: selinux
become: true
[student@workstation ansible]$ ansible-playbook selinux.yml
3、使用Ansible Galaxy安装角色
使用 Ansible Galaxy 和要求文件 /home/student/ansible/roles/requirements.yml,从以下 URL 下载角色并安装到 /home/student/ansible/roles:
http://content.example.com/haproxy.tar.gz 此角色的名称应当为 balancer
http://content.example.com/phpinfo.tar.gz 此角色的名称应当为 phpinfo
[student@workstation ansible]$ vim roles/requirements.yml
---
- name: balancer
src: http://content.example.com/ansible2.8/haproxy.tar.gz
- name: phpinfo
src: http://content.example.com/ansible2.8/phpinfo.tar.gz
[student@workstation ansible]$ ansible-galaxy install -r requirements.yml -p roles/
4、创建和使用角色
根据下列要求,在/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 tasks]$ vim main.yml
---
# tasks file for http
- name: install httpd firewalld
yum:
name:
- httpd
- firewalld
state: present
- name: cp file
template:
src: index.html.j2
dest: /var/www/html/index.html
- name: start httpd
service:
name: httpd
state: started
enabled: yes
- name: restart firewalld
service:
name: firewalld
state: restarted
enabled: yes
- name: firewalld for http
firewalld:
service: http
state: enabled
permanent: yes
immediate: yes
[student@workstation tasks]$ cd ..
[student@workstation apache]$ ls
defaults files handlers meta README.md tasks templates tests vars
[student@workstation apache]$ cd templates/
[student@workstation templates]$ vim index.html.j2
Welcome to {{ansible_fqdn}} on {{ansible_enp1s0.ipv4.address}}
[student@workstation ansible]$ vim newrole.yml
---
- name: use http role
hosts: webservers
roles:
- apache
[student@workstation ansible]$ ansible-playbook newrole.yml
PLAY [use http role] *********************************************************************
TASK [Gathering Facts] *******************************************************************
ok: [serverd]
ok: [serverc]
TASK [apache : install httpd firewalld] **************************************************
changed: [serverd]
changed: [serverc]
TASK [apache : cp file] ******************************************************************
changed: [serverc]
changed: [serverd]
TASK [apache : start httpd] **************************************************************
changed: [serverc]
changed: [serverd]
TASK [apache : restart firewalld] ********************************************************
changed: [serverc]
changed: [serverd]
TASK [apache : firewalld for http] *******************************************************
changed: [serverd]
changed: [serverc]
PLAY RECAP *******************************************************************************
serverc : ok=6 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
serverd : ok=6 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[student@workstation ansible]$ curl http://serverc
Welcome to serverc.lab.example.com on 172.25.250.12
[student@workstation ansible]$ curl http://serverc
Welcome to serverc.lab.example.com on 172.25.250.12
5、从Ansible Galaxy使用角色
根据下列要求,创建一个名为 /home/student/ansible/roles.yml的playbook:
playbook中包含一个play,该play在balancers主机组中的主机上运行并将使用balancer角色。
此角色配置一项服务,以在webservers主机组中的主机之间平衡Web服务器请求的负载。
浏览到balancers主机组中的主机(例如http://bastion.lab.example.com/ )将生成以下输出:
Welcome to serverc.example.com on 172.25.1.12
重新加载浏览器将从另一Web服务器生成输出:
Welcome to serverd.example.com on 172.25.1.13
playbook 中包含一个 play,该 play 在 webservers主机组中的主机上运行并将使用 phpinfo 角色。
通过 URL /hello.php 浏览到 webservers 主机组中的主机将生成以下输出:
Hello PHP World from FQDN
其中,FQDN是主机的完全限定名称。
例如,浏览到 http://serverc.lab.example.com/hello.php 会生成以下输出:
Hello PHP World from serverc.lab.example.com
另外还有 PHP 配置的各种详细信息,如安装的PHP 版本等。
同样,浏览到 http://serverd.lab.example.com/hello.php 会生成以下输出:
Hello PHP World from serverd.lab.example.com
另外还有 PHP 配置的各种详细信息,如安装的PHP 版本
[student@workstation tasks]$ vim main.yml
---
# tasks file for http
- name: install httpd firewalld
yum:
name:
- httpd
- firewalld
state: present
- name: cp file
template:
src: index.html.j2
dest: /var/www/html/index.html
- name: start httpd
service:
name: httpd
state: started
enabled: yes
- name: restart firewalld
service:
name: firewalld
state: restarted
enabled: yes
- name: firewalld for http
firewalld:
service: http
state: enabled
permanent: yes
immediate: yes
[student@workstation tasks]$ cd ..
[student@workstation apache]$ ls
defaults files handlers meta README.md tasks templates tests vars
[student@workstation apache]$ cd templates/
[student@workstation templates]$ vim index.html.j2
Welcome to {{ansible_fqdn}} on {{ansible_enp1s0.ipv4.address}}
[student@workstation ansible]$ vim newrole.yml
---
- name: use http role
hosts: webservers
roles:
- apache
[student@workstation ansible]$ ansible-playbook newrole.yml
PLAY [use http role] *********************************************************************
TASK [Gathering Facts] *******************************************************************
ok: [serverd]
ok: [serverc]
TASK [apache : install httpd firewalld] **************************************************
changed: [serverd]
changed: [serverc]
TASK [apache : cp file] ******************************************************************
changed: [serverc]
changed: [serverd]
TASK [apache : start httpd] **************************************************************
changed: [serverc]
changed: [serverd]
TASK [apache : restart firewalld] ********************************************************
changed: [serverc]
changed: [serverd]
TASK [apache : firewalld for http] *******************************************************
changed: [serverd]
changed: [serverc]
PLAY RECAP *******************************************************************************
serverc : ok=6 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
serverd : ok=6 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[student@workstation ansible]$ curl http://serverc
Welcome to serverc.lab.example.com on 172.25.250.12
[student@workstation ansible]$ curl http://serverc
Welcome to serverc.lab.example.com on 172.25.250.12