playbook

部署 web 服务器

  1. 部署 yum 仓库
  2. 安装 httpd
  3. 新建 /www 目录
  4. 在 /www 中新建 index.html,内容为 my name is kangyimei
  5. 该 web 服务器的 DocumentRoot 为 /www
  6. 实现在 ansible 中能够使用 http://node1 访问到该网页内容

playbook

[root@server ~]# su - student
[student@server ~]$ cd ansible/

[student@server ansible]$ vim k1.yml
---
- name: web station 
  hosts: node1 
  tasks: 
    - name: remove /etc/yum.repos.d
      file: 
        path: /etc/yum.repos.d/
        state: absent

    - name: create /etc/yum.repos.d 
      file: 
        path: /etc/yum.repos.d/
        state: directory 

    - name: mount cdrom
      mount: 
        src: /dev/cdrom
        path: /mnt
        fstype: iso9660
        state: mounted

    - name: yum_repo1
      yum_repository: 
        file: AppStream 
        name: appstream 
        description: aa
        baseurl: file:///mnt/AppStream
        enabled: yes
        gpgcheck: no

    - name: yum_repo2
      yum_repository: 
        file: BaseOS
        name: baseos
        description: bb
        baseurl: file:///mnt/BaseOS
        enabled: yes
        gpgcheck: no 

    - name: install httpd
      yum: 
        name: httpd 
        state: installed 

    - name: create /www 
      file: 
        path: /www
        state: directory

    - name: create index.html
      file: 
        path: /www/index.html
        state: touch 

    - name: insert index.html
      lineinfile: 
        path: /www/index.html
        line: my name is kangyimei

    - name: install semanage
      yum: 
        name: policycoreutils-python-utils
        state: installed 

    - name: set selinux context
      sefcontext: 
        target: /www/index.html
        setype: httpd_sys_content_t
        state: present

    - name: restorecon
      command: restorecon -Rv /www/index.html

    - name: httpd.conf
      replace: 
        path: /etc/httpd/conf/httpd.conf
        regexp: 'DocumentRoot "/var/www/html"'
        replace: 'DocumentRoot "/www"'

    - name: httpd1.conf
      replace: 
        path: /etc/httpd/conf/httpd.conf
        regexp: <Directory "/var/www">
        replace: <Directory "/">

    - name: start httpd
      service: 
        name: httpd
        state: started
        enabled: yes

    - name: firewalld httpd
      firewalld: 
        rich_rule: rule family=ipv4 source address=192.168.91.0/24 service name=http accept 
        permanent: yes 
        immediate: yes
        state: enabled    

执行

[student@server ansible]$ ansible-playbook k1.yml

PLAY [web station] *********************************************************************

TASK [Gathering Facts] *****************************************************************
ok: [node1]

TASK [remove /etc/yum.repos.d] *********************************************************
changed: [node1]

TASK [create /etc/yum.repos.d] *********************************************************
changed: [node1]

TASK [mount cdrom] *********************************************************************
changed: [node1]

TASK [yum_repo1] ***********************************************************************
changed: [node1]

TASK [yum_repo2] ***********************************************************************
changed: [node1]

TASK [install httpd] *******************************************************************
changed: [node1]

TASK [create /www] *********************************************************************
changed: [node1]

TASK [create index.html] ***************************************************************
changed: [node1]

TASK [insert index.html] ***************************************************************
changed: [node1]

TASK [install semanage] ****************************************************************
changed: [node1]

TASK [set selinux context] *************************************************************
changed: [node1]

TASK [restorecon] **********************************************************************
changed: [node1]

TASK [httpd.conf] **********************************************************************
changed: [node1]

TASK [httpd1.conf] *********************************************************************
changed: [node1]

TASK [start httpd] *********************************************************************
changed: [node1]

TASK [firewalld httpd] *****************************************************************
changed: [node1]

PLAY RECAP *****************************************************************************
node1                      : ok=17   changed=16    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

验证

[student@server ansible]$ curl http://node1
my name is kangyimei

使用 notify…handlers

  1. 写一个剧本 runtime.yml,只对 node1 操作
  2. 创建用户 aa,该用户不能用于登录,家目录 /www
  3. 在 /www 创建一个文件 html
  4. 每次执行该剧本时,将系统的当前时间输入到 html 文件中
  5. 如果 html 中的时间发生变化,那么创建 /tmp/kk 的文件

playbook

[student@server ansible]$ vim runtime.yml
---
- name: exercise
  hosts: node1
  tasks: 
    - name: create user
      user: 
        name: aa 
        shell: /sbin/nologin
        home: /www

    - name: create html
      file: 
        path: /www/html
        state: touch 

    - name: insert html 
      shell: date > /www/html 
      notify: 
        - kk

  handlers: 
    - name: kk 
      file: 
        path: /tmp/kk
        state: touch

执行

[student@server ansible]$ ansible-playbook runtime.yml

PLAY [exercise] ************************************************************************

TASK [Gathering Facts] *****************************************************************
ok: [node1]

TASK [create user] *********************************************************************
changed: [node1]

TASK [create html] *********************************************************************
changed: [node1]

TASK [insert html] *********************************************************************
changed: [node1]

RUNNING HANDLER [kk] *******************************************************************
changed: [node1]

PLAY RECAP *****************************************************************************
node1                      : ok=5    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

验证

[root@node1 ~]# 
[root@node1 ~]# cat /www/html 
Tue Oct 25 16:55:29 CST 2022

[root@node1 ~]# ll /tmp/kk 
-rw-r--r--. 1 root root 0 Oct 25 16:55 /tmp/kk
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值