实训day33(8.21)

playbook

1.调用剧本

        ansible-playbook /etc/ansible/playbook/book001.yml

2.编写剧本

        

3.剧本的语法

        ---

        - hosts:

         remote_user:

        vars:

        -   a

        tasks:

        - name:        说明

                调用模块

                notify:

                - handlername:

        -handlers:

                - name:        handlername

                模块语句

        ...

[root@m0 ~]# ansible group02 -m ping
[root@m0 ~]# vim /etc/ansible/playbook/nginx.yml

---
-       hosts:          group02
        remote_user:    root
        tasks:
        -       name:   卸载httpd
                yum:    name=httpd   state=present

        -       name:   安装nginx
                yum:    name=nginx      state=absent

        -       name:   修改资源文件
                shell:  echo 'i am nginx,port is 80' > /usr/share/nginx/html/index.html
        -       name:   修改端口
                command:        sed -i '/listen/s/80/8080/g' /etc/nginx/nginx.conf
                notify:
                -       restart nginx
        -       name:   启动服务
                service:        name=nginx   state=started  enabled=yes
        handlers:
                -       name:   restart nginx
                        service:        name=nginx   state=restarted
...
 

roles模块
[root@m0 ~]# cd /etc/ansible/roles/
[root@m0 roles]# ls
[root@m0 roles]# 
[root@m0 roles]# mkdir nginx
[root@m0 roles]# ls
nginx
[root@m0 roles]# vim /etc/ansible/playbook/nginx.yml
[root@m0 roles]# cd nginx/
[root@m0 nginx]# mkdir files
[root@m0 nginx]# mkdir tasks
[root@m0 nginx]# mkdir handlers
[root@m0 nginx]# mkdir templates
[root@m0 nginx]# mkdir vars
[root@m0 nginx]# mkdir tree
[root@m0 nginx]# rm -rf tree
[root@m0 nginx]# ls
files  handlers  tasks  templates  vars
[root@m0 nginx]# touch files/main.yml
[root@m0 nginx]# touch handlers/main.yml
[root@m0 nginx]# touch tasks/main.yml
[root@m0 nginx]# touch vars/main.yml
[root@m0 nginx]# rm -rf files/main.yml
[root@m0 nginx]# vim /etc/ansible/playbook/test002.yml

---
-       hosts:  group02
        remote_user:    root
        roles:
                -       nginx

...
 

[root@m0 nginx]# ansible-playbook /etc/ansible/playbook/test002.yml 

PLAY [group02] *************************************************************************

TASK [Gathering Facts] *****************************************************************
ok: [other]
ok: [192.168.2.61]
ok: [192.168.2.60]

PLAY RECAP *****************************************************************************
192.168.2.60               : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.2.61               : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
other                      : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@m0 nginx]# vim /etc/ansible/roles/nginx/tasks/main.yml 

---
-       name:   卸载httpd
        yum:    name=httpd      state=absent
-       name:   卸载nginx
        yum:    name=nginx      state=present
-       name:   安装nginx
        yum:    name=nginx      state=present
-       name:   启动服务
        service:    name=nginx      state=started   enabled=yes

[root@m0 nginx]# ansible-playbook /etc/ansible/playbook/test002.yml 

PLAY [group02] *************************************************************************

TASK [Gathering Facts] *****************************************************************
ok: [other]
ok: [192.168.2.60]
ok: [192.168.2.61]

TASK [nginx : 卸载httpd] *****************************************************************
changed: [other]
changed: [192.168.2.61]
changed: [192.168.2.60]

TASK [卸载nginx] *************************************************************************
changed: [192.168.2.60]
changed: [192.168.2.61]
changed: [other]

TASK [安装nginx] *************************************************************************
ok: [192.168.2.60]
ok: [192.168.2.61]
ok: [other]

TASK [nginx : 启动服务] ********************************************************************
changed: [192.168.2.60]
changed: [192.168.2.61]
changed: [other]

PLAY RECAP *****************************************************************************
192.168.2.60               : ok=5    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.2.61               : ok=5    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
other                      : ok=5    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@m0 nginx]# vim /etc/ansible/playbook/test002.yml

[root@m0 nginx]# ansible-playbook /etc/ansible/playbook/test002.yml 

PLAY [group02] *************************************************************************

TASK [Gathering Facts] *****************************************************************
ok: [192.168.2.60]
ok: [other]
ok: [192.168.2.61]

TASK [nginx : 卸载httpd] *****************************************************************
ok: [192.168.2.61]
ok: [192.168.2.60]
ok: [other]

TASK [卸载nginx] *************************************************************************
ok: [192.168.2.60]
ok: [192.168.2.61]
ok: [other]

TASK [安装nginx] *************************************************************************
ok: [192.168.2.60]
ok: [192.168.2.61]
ok: [other]

TASK [nginx : 启动服务] ********************************************************************
ok: [192.168.2.61]
ok: [192.168.2.60]
ok: [other]

TASK [mysql : 卸载httpd] *****************************************************************
ok: [192.168.2.60]
ok: [192.168.2.61]
ok: [other]

TASK [mysql : 卸载nginx] *****************************************************************
ok: [192.168.2.60]
ok: [other]
ok: [192.168.2.61]

TASK [mysql : 安装nginx] *****************************************************************
ok: [192.168.2.61]
ok: [192.168.2.60]
ok: [other]

TASK [mysql : 启动服务] ********************************************************************
ok: [192.168.2.60]
ok: [192.168.2.61]
ok: [other]

PLAY RECAP *****************************************************************************
192.168.2.60               : ok=9    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.2.61               : ok=9    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
other                      : ok=9    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

  • 17
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值