【ansible第三次作业】
题目
ansible自动化部署web服务
项目(一)
任务一:安装httpd,firewalld软件包
任务二:创建网站资源文件
任务三:启动httpd,firewalld
任务四:添加防火墙规则
项目(二)
uri测试
通过debug捕获打印测试结果
1.安装部署ansible
[student@workstation ~]$ lab data-variables start #创建data-variables工作目录
Setting up workstation for lab exercise work:
· Verifying Ansible installation.............................. SUCCESS
· Creating working directory.................................. SUCCESS
. Creating Ansible files...................................... SUCCESS
[student@workstation ~]$ ll #查看是否创建成功
total 0
drwxr-xr-x. 2 student student 42 Sep 9 20:43 data-variables
[student@workstation ~]$ cd data-variables/ #进入data-variables工作目录
[student@workstation data-variables]$ ll #查看目录下文件
total 8
-rw-r--r--. 1 student student 99 Sep 9 20:43 ansible.cfg
-rw-r--r--. 1 student student 37 Sep 9 20:43 inventory
2.vim ./ansible.cfg
[defaults]
inventory=./inventory
remote_user=root
[privilege_escalation]
become = true
3.vim inventory
[webserver]
servera.lab.example.com
4.剧本
---
- name: config web server #项目名
hosts: webserver #匹配主机组
vars: #定义变量
web_pkg: httpd
firewall_pkg: firewalld
web_service: httpd
firewall_service: firewalld
python_pkg: python3-PyMySQL
rule: http
tasks:
- name: install pkg
yum:
name: #引用变量
- "{{ web_pkg }}"
- "{{ firewall_pkg }}"
- "{{ python_pkg }}"
state: installed
- name: start firewalld #启动防火墙服务
service:
name: "{{ firewall_service }}"
state: started
enabled: true
- name: start httpd #启动httpd服务
service:
name: "{{ web_service }}"
state: started
enabled: true
- name: create file
copy:
content: "Example we content" #网页显示信息
dest: /var/www/html/index.html #目标路径
- name: firewall-rule #指定防火墙规则
firewalld:
service: "{{ rule }}"
permanent: true
immediate: true
state: enabled
- name: testing
hosts: webserver
tasks:
- name: curl file
uri:
url: http://servera.lab.example.com
status_code: 200
return_content: yes
5.测试
ansible-playbook -C playbook.yml
PLAY [config web server] ************************************************************
TASK [Gathering Facts] **************************************************************
ok: [servera.lab.example.com]
TASK [install pkg] ******************************************************************
changed: [servera.lab.example.com]
TASK [start firewalld] **************************************************************
ok: [servera.lab.example.com]
TASK [start httpd] ******************************************************************
changed: [servera.lab.example.com]
TASK [create file] ******************************************************************
changed: [servera.lab.example.com]
TASK [firewall-rule] ****************************************************************
changed: [servera.lab.example.com]
PLAY [testing] **********************************************************************
TASK [Gathering Facts] **************************************************************
ok: [servera.lab.example.com]
TASK [curl file] ********************************************************************
skipping: [servera.lab.example.com]
PLAY RECAP **************************************************************************
servera.lab.example.com : ok=7 changed=4 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
版权声明:本文为kouacbb原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。