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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值