Ansible剧本

Ansible剧本

文章目录

  • **Ansible剧本**
    • 一、部署web服务器
      • 要求
      • 编写剧本
      • 执行
      • 验证
    • 二、使用notify....handlers
      • 要求
      • 编写剧本
      • 执行

一、部署web服务器

要求

1、部署yum仓库
2、安装httpd
3、新建/www目录
4、在/www中新建index.html,内容为my name is chenyu(chenyu为你们自己名字的全拼)
5、该web服务器的DocumentRoot为/www
5、实现在ansible中能够使用http://node1访问到该网页内容

编写剧本

[student@server ansible]$ cat zbc.yml 
---
- name: web station 
  hosts: node1
  tasks: 
    - name: mount dev
      mount: 
        src: /dev/cdrom
        path: /mnt
        fstype: iso9660
        state: mounted

    - name: yum_repo
      yum_repository: 
        file: server
        name: aa
        description: aa
        baseurl: file:///mnt/BaseOS
        enabled: yes
        gpgcheck: no
        
    - name: yum_repo2
      yum_repository: 
        file: server
        name: bb
        description: bb
        baseurl: file:///mnt/AppStream
        enabled: yes
        gpgcheck: no

    - name: install httpd
      yum: 
       name: httpd
       state: latest

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

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

    - name: lineinfile
      lineinfile: 
        path: /www/index.html
        line: "my name is zhengbichao"

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

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

    - name: httpd_sys_content_t
      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: 
        replace: 

    - name: start httpd
      service: 
        name: httpd
        state: restarted
        enabled: yes
[student@server ansible]$ cat zbc.yml 
---
- name: web station 
  hosts: node1
  tasks: 
    - name: mount dev
      mount: 
        src: /dev/cdrom
        path: /mnt
        fstype: iso9660
        state: mounted

    - name: yum_repo
      yum_repository: 
        file: server
        name: aa
        description: aa
        baseurl: file:///mnt/BaseOS
        enabled: yes
        gpgcheck: no
        
    - name: yum_repo2
      yum_repository: 
        file: server
        name: bb
        description: bb
        baseurl: file:///mnt/AppStream
        enabled: yes
        gpgcheck: no

    - name: install httpd
      yum: 
       name: httpd
       state: latest

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

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

    - name: lineinfile
      lineinfile: 
        path: /www/index.html
        line: "my name is zhengbichao"

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

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

    - name: httpd_sys_content_t
      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: 
        replace: 

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

执行

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

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

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

TASK [mount dev] *******************************************************************************
ok: [node1]

TASK [yum_repo] ********************************************************************************
ok: [node1]

TASK [yum_repo2] *******************************************************************************
ok: [node1]

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

TASK [/www] ************************************************************************************
ok: [node1]

TASK [/www/index.html] *************************************************************************
changed: [node1]

TASK [lineinfile] ******************************************************************************
ok: [node1]

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

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

TASK [httpd_sys_content_t] *********************************************************************
changed: [node1]

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

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

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

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

PLAY RECAP *************************************************************************************
node1                      : ok=15   changed=7    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

验证

[student@server ansible]$ curl 192.168.87.129
my name is zhengbichao


[root@node1 www]# ll -Z 
total 8
-rw-r--r--. 1 root root unconfined_u:object_r:httpd_sys_content_t:s0 29 Oct 25 15:34 html
-rw-r--r--. 1 root root unconfined_u:object_r:httpd_sys_content_t:s0 23 Oct 25 16:55 index.html
[root@node1 www]# cat html 
Tue Oct 25 15:34:15 CST 2022

二、使用notify…handlers

要求

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

编写剧本

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

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

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

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

执行

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

PLAY [time] ************************************************************************************

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

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

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

TASK [date] ************************************************************************************
changed: [node1]

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

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

你可能感兴趣的:(1024程序员节)