首先以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 deploy-basic start 命令。此功能将确保受管主机 serverc.lab.example.com 和 serverd.lab.example.com 可在网络上访问。它将确保控制节点上安装了正确的ansible 配置文件和清单文件。
[student@workstation ~]$ lab playbook-basic start
Setting up workstation for lab exercise work:
· Verifying Ansible installation.............................. SUCCESS
· Creating working directory.................................. SUCCESS
· Deploying Ansible inventory................................. SUCCESS
· Deploying ansible.cfg....................................... SUCCESS
· Downloading index.html...................................... SUCCESS
· Stop firewalld on serverc................................... SUCCESS
· Stop firewalld on serverd................................... SUCCESS
已经在 workstation 上为此练习创建了工作目录 /home/student/playbook-basic.该目录中已填充了ansible.cfg配置文件,还填充了inventory 清单文件,后者定义包含上方所列受管主机作为其成员的web组。
在此目录中,使用文本编辑器创建名为 site.yml 的playbook.此playbook 中含有一个play,它应当以web主机组的成员为目标。该playbook应使用任务来确保受管主机上满足下列条件:
1、利用yum模块确保存在 httpd 软件包。
2、利用copy 模块将本地的files/index.html 文件复制到各受管主机上的 /var/www/html/index.html.
3、利用service 模块去启动和启用httpd服务。
可使用ansible-doc 命令帮助了解各个模块所需的关键字。
编写了playbook后,验证其语法,然后使用ansible-playbook 运行 playbook来实施配置。
1、更改为 /home/student/playbookk-basic目录。
[student@workstation ~]$ cd ~/playbook-basic
[student@workstation playbook-basic]$
2、使用文本编辑器,创建名为 /home/student/playbook-basic/site.yml 的新playbook. 以web 主机组中的主机为目标开始编写一个paly.
[student@workstation playbook-basic]$ vim site.yml
添加:
---
- name: Install and start Apache HTTPD
hosts: web
tasks:
- name: httpd package is present
yum:
name: httpd
state: present
- name: correct index.html is present
copy:
src: files/index.html
dest: /var/www/html/index.html
- name: httpd is started
service:
name: httpd
state: started
enabled: true
4、在运行playbook之前,请运行 ansible-playbook --syntax-check site.yml 命令以验证其语法是否正确。如果报告任何错误,请更正后再继续下一步。
[student@workstation playbook-basic]$ ansible-playbook --syntax-check site.yml
playbook: site.yml
5、运行playbook。 通读生成的输出,确保所有任务都已成功完成。
[student@workstation playbook-basic]$ ansible-playbook site.yml
PLAY [Install and start Apache HTTPD] ******************************************
TASK [Gathering Facts] *********************************************************
ok: [serverc.lab.example.com]
ok: [serverd.lab.example.com]
TASK [httpd package is present] ************************************************
ok: [serverc.lab.example.com]
ok: [serverd.lab.example.com]
TASK [correct index.html is present] *******************************************
ok: [serverc.lab.example.com]
ok: [serverd.lab.example.com]
TASK [httpd is started] ********************************************************
changed: [serverc.lab.example.com]
changed: [serverd.lab.example.com]
PLAY RECAP *********************************************************************
serverc.lab.example.com : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
serverd.lab.example.com : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
6、如果一切都正常,再次运行playbook,并且看到所有任务都完成且不对受管主机执行任何更改。
[student@workstation playbook-basic]$ ansible-playbook site.yml
PLAY [Install and start Apache HTTPD] ********************************************
TASK [Gathering Facts] ***********************************************************
ok: [serverc.lab.example.com]
ok: [serverd.lab.example.com]
TASK [httpd package is present] **************************************************
ok: [serverc.lab.example.com]
ok: [serverd.lab.example.com]
TASK [correct index.html is present] *********************************************
ok: [serverd.lab.example.com]
ok: [serverc.lab.example.com]
TASK [httpd is started] **********************************************************
ok: [serverd.lab.example.com]
ok: [serverc.lab.example.com]
PLAY RECAP ***********************************************************************
serverc.lab.example.com : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
serverd.lab.example.com : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
7、使用curl 命令验证serverc 和 serverd 是否都配置为HTTPD服务器。
[student@workstation playbook-basic]$ curl serverc.lab.example.com
This is a test page.
[student@workstation playbook-basic]$ curl serverd.lab.example.com
This is a test page.
完成:
清理本练习
[student@workstation ~]$ lab playbook-basic finish
Cleaning up exercise
· Remove web content.......................................... SUCCESS
· Remove httpd package........................................ SUCCESS
· Start firewalld on serverc.................................. SUCCESS
· Start firewalld on serverd.................................. SUCCESS