RHCE-ansible(三)--- 编写playbook实现自动化部署web服务

目录

ansible自动化部署web服务

项目(一)

任务一:安装httpd,firewalld软件包

任务二:创建网站资源文件

任务三:启动httpd,firewalld

任务四:添加防火墙规则

项目(二)

uri测试

通过debug捕获打印测试结果

实验配置

1、RH294自动创建环境

2、编写剧本

3、运行测试


ansible自动化部署web服务

项目(一)

任务一:安装httpd,firewalld软件包

任务二:创建网站资源文件

任务三:启动httpd,firewalld

任务四:添加防火墙规则

项目(二)

uri测试

通过debug捕获打印测试结果

实验配置

1、RH294自动创建环境

[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

[student@workstation ~]$ ll
total 0
drwxr-xr-x. 3 student student 55 Sep 17 22:22 playbook-basic

[student@workstation playbook-basic]$ ll
total 8
-rw-r--r--. 1 student student 144 Sep 17 22:25 ansible.cfg
drwxr-xr-x. 2 student student  24 Sep 17 22:22 files
-rw-r--r--. 1 student student  55 Sep 17 22:25 inventory
[student@workstation playbook-basic]$ cat ansible.cfg 
[defaults]
inventory=inventory
remote_user=devops

[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False
[student@workstation playbook-basic]$ vim inventory 

[web]
serverc.lab.example.com
serverd.lab.example.com

2、编写剧本

[student@workstation playbook-basic]$ vim site.yml 

---
- name: install and start Apache HTTPD
  hosts: web
  tasks:
    - name: httpd package is present   #安装httpd
      yum:   #模块名
        name: httpd    #指定软件包名
        state: present  

    - name: firewalld package is present  #安装firewalld
      yum:
        name: firewalld
        state: present

    - name: create index.html   #创建网站资源文件
      copy:
        content: "Welcome to the example.com"    #网页内容
        dest: /var/www/html/index.html        #网页文件路径

    - name: httpd is started  #启动httpd
      service:
        name: httpd
        state: started
        enabled: true   #开机启动

    - name: firewalld is started   #启动firewalld
      service:
        name: firewalld
        state: started
        enabled: true

    - name: firewall-rule    #防火墙规则
      firewalld:
        service: http     #协议或80/tcp
        permanent: true   #永久生效
        immediate: true   #立即生效
        state: enabled

- name: test   #测试
  hosts: web
  tasks:
    - name: web testing
      uri:
        url: http://serverc.lab.example.com
        return_content: yes  #接收返回结果
        status_code: 200   #能返回200,代表接收成功

3、运行测试

[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] *****************************************************************************************************************************
changed: [serverc.lab.example.com]
changed: [serverd.lab.example.com]

TASK [firewalld package is present] *************************************************************************************************************************
ok: [serverc.lab.example.com]
ok: [serverd.lab.example.com]

TASK [create index.html] ************************************************************************************************************************************
changed: [serverc.lab.example.com]
changed: [serverd.lab.example.com]

TASK [httpd is started] *************************************************************************************************************************************
changed: [serverc.lab.example.com]
changed: [serverd.lab.example.com]

TASK [firewalld is started] *********************************************************************************************************************************
ok: [serverd.lab.example.com]
ok: [serverc.lab.example.com]

TASK [firewall-rule] ****************************************************************************************************************************************
changed: [serverd.lab.example.com]
changed: [serverc.lab.example.com]

PLAY [test] *************************************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************
ok: [serverc.lab.example.com]
ok: [serverd.lab.example.com]

TASK [web testing] ******************************************************************************************************************************************
ok: [serverc.lab.example.com]
ok: [serverd.lab.example.com]

PLAY RECAP **************************************************************************************************************************************************
serverc.lab.example.com    : ok=9    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
serverd.lab.example.com    : ok=9    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

你可能感兴趣的:(RHCE,服务器,linux,运维)