playbook(thirty-one day)

 [root@m0 ~]# vim /etc/tset000.sh

#!/bin/bash
mkdir /tmp/three
touch /tmp/three/test
echo 'I am echo at mttt' > /tmp/three/test
echo 'well done'

[root@m0 ~]# ansible -s -m copy -a 'src=./mysql_master.sh dest=~'
[root@m0 ~]# ansible -s -m command -a 'yum -y install nfs-utils'
[root@m0 ~]# ansible s -m command -a 'yum -y install rpcbind'
[root@m0 ~]# vim /etc/exports

/static *(ro,sync)
1、在被控制的主机上添加static目录

[root@m0 ~]# ansible s -m file -a 'path=/static state=directory'
[root@m0 ~]# ansible s -m file -a 'path=/static/test state=touch'
[root@m0 ~]# ansible s -m copy -a 'src=/etc/exports dest=/etc/exports'

2、两种启动方法
(1)command模块 

[root@m0 ~]# ansible s -m command -a 'systemctl start nfs'
[root@m0 ~]# ansible s -m command -a 'systemctl enable nfs'

(2)service模块

[root@m0 ~]# ansible s -m service -a 'name=rpcbind state=started enabled=yes'

3、控制机下载nfs并将nfs挂载到指定目录下

[root@M0 ~]# yum -y install nfs-utils
[root@M0 ~]# mkdir /nfs
[root@M0 ~]# mount -t nfs 192.168.1.50:/static /nfs/
[root@m0 ~]# ls /nfs/
test

一、playbook

playbook(剧本): 是ansible用于配置,部署,和管理被控节点的剧本。用于ansible操作的编排。

1、YMAL格式

(1)以.yaml或.ym结尾
(2)文件的第一行以"---"开始,表明YMAL文件的开始(可选的)
(3)以#号开头为注释
(4)列表中的所有成员都开始于相同的缩进级别,并且使用一个"一作为开头(一个横杠和一个空格)
(5)一个字典是由一个简单的 键:值 的形式组成(这个冒号后面必须是一个空格)
        注意:写这种文件可以使用tab键

#playbook剧本是保存在控制机的yml文件

(1)安装vsftpd

[root@M0 ~]# vim test002.yml

---
-       hosts:          group02
        remote_user:    root
        tasks:
        -        name:    安装vsftpd
                 yum:     name=vsftpd     state=latest

[root@M0 ~]# ansible-playbook ./test002.yml

(2)卸载vsftpd

[root@M0 ~]# vim test002.yml

---
-       hosts:          group02
        remote_user:    root
        tasks:
        -        name:    卸载vsftpd
                 yum:     name=vsftpd     state=absent
        -        name:    安装vsftpd
                 yum:     name=vsftpd     state=latest

[root@M0 ~]# ansible-playbook ./test002.yml

(3)启动vsftpd服务(开机自启)

[root@M0 ~]# vim test002.yml

---
-       hosts:          group02
        remote_user:    root
        tasks:
        -        name:    卸载vsftpd
                 yum:     name=vsftpd     state=absent
        -        name:    安装vsftpd
                 yum:     name=vsftpd     state=latest
        -        name:    启动服务
                 service:     name=vsftpd     state=started         enabled=yes

[root@M0 ~]# ansible-playbook ./test002.yml

(4) 修改配置文件

[root@M0 ~]# vim test002.yml

---
-       hosts:          group02
        remote_user:    root
        tasks:
        -        name:    卸载vsftpd
                 yum:     name=vsftpd     state=absent
        -        name:    安装vsftpd
                 yum:     name=vsftpd     state=latest
        -        name:    启动服务
                 service: name=vsftpd     state=started         enabled=yes
        -        name:    修改配置文件 
                 command: sed -i '/^anonymous_enable=YES/s/YES/NO/g' /etc/vsftpd/vsftpd.conf

[root@M0 ~]# ansible-playbook ./test002.yml

 (5)重启vsftpd服务

[root@M0 ~]# vim test002.yml

---
-       hosts:          group02
        remote_user:    root
        tasks:
        -        name:    卸载vsftpd
                 yum:     name=vsftpd     state=absent
        -        name:    安装vsftpd
                 yum:     name=vsftpd     state=latest
        -        name:    启动服务
                 service: name=vsftpd     state=started         enabled=yes
        -        name:    修改配置文件
                 command: sed -i '/^anonymous_enable=YES/s/YES/NO/g' /etc/vsftpd/vsftpd.conf
                 notify:
                 -      abcdefg
        handlers:
                 -       name:   abcdefg
                         service:       name=vsftpd     state=restarted

[root@M0 ~]# ansible-playbook ./test002.yml

 总结语法:
---
-    hosts:    组名/别名/ip/域名
    remote_user:    root
    tasks:
    -    name:    任务说明
         模块:    key0=values
         service:    name=vsftpd    state=started    enable=yes
    -    name    修改配置文件
        command:    sed.....
        notify:
        -    abcdefg

    handler:
        -    name:    abcdefg
           service:    name=vsftpd    start=restarted
练习:

修改httpd的端口为8080,再执行playbook测试

[root@M0 ~]# vim httpd.yml


---
-       hosts:          group02
        remote_user:    root
        tasks:
        -       name:   将关机的rope文件复制到被控制的主机
                copy:   src=/etc/yum.repos.d    dest=/etc/

        -       name:   安装httpd
                yum:    name=httpd   state=present

        -       name:   修改配置文件
                command:        sed -i '/^Listen/s/80/8080/g' /etc/httpd/conf/httpd.conf
        -       name:   修改默认的资源文件
                command:        echo 'xxxxxxxxxxxxxx' > /var/www/html/index.html
        -       name:   启动httpd服务
                service:        name=httpd      state=started

[root@M0 ~]# ansible-playbook ./httpd.yml 

nfs的步骤:

1、准备2台主机
2、在服务器端安装 rpcbind,nfs-utils
        1.创建共享目录
        2.配置文件 /etc/exports
        3.启动rpcbind nfs

3、在客户端安装nfs-utils

        1.创建挂在目录

        2.挂载文件

2、playbook编排多个hosts任务

将s组中的nfs和rpcbind卸载

[root@M0 ~]# ansible group02 -m yum -a 'name=nfs-utils state=absent'
[root@M0 ~]# vim test003.yml

---
-       hosts:  s1
        remote_user:    root
        tasks:
        -       name:   创建一个文件
                file:   path=/tmp/xxxxx.txt   state=touch


-       hosts:  s2
        remote_user:    root
        tasks:
        -       name:   也创建一个文件
                file:   path=/tmp/yyyyy.txt     state=touch

[root@M0 ~]# ansible-playbook ./test003.yml 

 3、用playbook安装mysql主从服务器

[root@M0 ~]# vim test003.yml

---
-       hosts:  s1
        remote_user:    root
        tasks:
        -       name:   安装nfs-utils
                yum:    name=nfs-utils   state=present

        -       name:   安装rpcbind
                yum:    name=rpcbind    state=present

        -       name:   创建共享目录
                file:   path=/static   state=directory

        -       name:   配置文件
                shell:  echo '/static   *(ro,sync)' > /etc/exports

        -       name:           启动服务nfs
                service:        name=nfs        state=started   enabled=yes

        -       name:           启动服务rpcbind
                service:        name=rpcbind    state=started   enabled=yes

-       hosts:  s2
        remote_user:    root
        tasks:
        -       name:   安装nfs-utils
                yum:    name=nfs-utils  state=present
        -       name:   创建挂载目录
                file:   path=/nfs   state=directory
        -       name:   挂在nfs文件
                command:        mount -t nfs 192.168.1.51:/static /nfs

[root@M0 ~]# ansible-playbook ./test003.yml

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值