首先以student用户身份并使用student作为密码登录workstation.
登录workstation
[kiosk@foundation0 ~]$ ssh student@workstation
Warning: Permanently added 'workstation,172.25.250.9' (ECDSA) to the list of known hosts.
Activate the web console with: systemctl enable --now cockpit.socket
在workstation 上,运行 lab playbook-variables start 命令。此功能将确保受管主机servera.lab.example.com 可在网络上访问。他也将确保控制节点上安装正确的ansible 配置文件和清单文件。
1、以student用户身份,在workstation 上更改到/home/student/data-varibles目录。
[student@workstation ~]$ cd ~/data-variables
2、创建一个playbook ,他将安装Apache Web 服务器并开启相关的端口一十七服务可被访问。playbbok 查询web 服务器已确认其以设好并在运行。
[student@workstation data-variables]$ vim playbook.yml
添加:
---
- name: Deploy and start Apache HTTPD service
hosts: webserver
vars:
web_pkg: httpd
firewall_pkg: firewalld
web_service: httpd
firewall_service: firewalld
python_pkg: python3-PyMySQL
rule: http
tasks:
- name: Required packages are installed and up to date
yum:
name:
- "{{ web_pkg }}"
- "{{ firewall_pkg }}"
- "{{ python_pkg }}"
state: latest
- name: The {{ firewall_service }} service is started and enabled
service:
name: "{{ firewall_service }}"
enabled: true
state: started
- name: The {{ web_service }} service is started and enabled
service:
name: "{{ web_service }}"
enabled: true
state: started
- name: Web content is in place
copy:
content: "Example web content"
dest: /var/www/html/index.html
- name: The firewall port for {{ rule }} is open
firewalld:
service: "{{ rule }}"
permanent: true
immediate: true
state: enabled
- name: Verify the Apache service
hosts: localhost
become: false
tasks:
- name: Ensure the webserver is reachable
uri:
url: http://servera.lab.example.com
status_code: 200
3、在运行playbook之前,使用ansible-playbook --syntax-check 命令验证其语法。如哦报告任何错误,请更正后再继续下一步。
[student@workstation data-variables]$ ansible-playbook --syntax-check playbook.yml
playbook: playbook.yml
4、使用ansible-palybook 命令运行playbook.观察输出结果,ansible 将首选安装软件包,在启动并启用其服务,然后确保web服务器可被访问。
[student@workstation data-variables]$ ansible-playbook playbook.yml
PLAY [Deploy and start Apache HTTPD service] *************************************************************
TASK [Gathering Facts] ***********************************************************************************
ok: [servera.lab.example.com]
TASK [Required packages are installed and up to date] ****************************************************
changed: [servera.lab.example.com]
TASK [The firewalld service is started and enabled] ******************************************************
ok: [servera.lab.example.com]
TASK [The httpd service is started and enabled] **********************************************************
changed: [servera.lab.example.com]
TASK [Web content is in place] ***************************************************************************
changed: [servera.lab.example.com]
TASK [The firewall port for http is open] ****************************************************************
changed: [servera.lab.example.com]
PLAY [Verify the Apache service] *************************************************************************
TASK [Gathering Facts] ***********************************************************************************
ok: [localhost]
TASK [Ensure the webserver is reachable] *****************************************************************
ok: [localhost]
PLAY RECAP ***********************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
servera.lab.example.com : ok=6 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
完成
在workstation上,运行 lab data-variables finish 脚本俩清理本实验中创建的资源.
[student@workstation data-variables]$ lab data-variables finish
Cleaning up the lab on servera.lab.example.com:
· Removing packages
· Removing httpd............................................ SUCCESS
· Removing python3-PyMySQL.................................. SUCCESS
· Cleaning the web directory ................................. SUCCESS