ansible的playbook的应用

一、playbook

playbook(剧本): 是ansible⽤于配置,部署,和管理被控节点的剧本。⽤ 于ansible操作的编排。 参考: https://docs.ansible.com/ansible/latest/user_guide/playbooks _intro.html 使⽤的格式为yaml格式(saltstack,elk,docker,docker compose,kubernetes等也都会⽤到yaml格式)

二、YML

YMAL格式 以.yaml或.yml结尾

⽂件的第⼀⾏以 "---"开始,表明YMAL⽂件的开始(可选的) 以#号开头为注释

列表中的所有成员都开始于相同的缩进级别, 并且使⽤⼀个 " 作为开头(⼀个横杠和⼀个空格) ⼀个字典是由⼀个简单的 "- 键: 值 的形式组成(这个冒号后⾯必须 是⼀个空格)

注意: 写这种⽂件不要使⽤tab键,都使⽤空格

简介;

hosts: ⽤于指定要执⾏任务的主机,其可以是⼀个或多个由冒号分 隔主机组

remote_user: ⽤于指定远程主机上的执⾏任务的⽤户.

tasks: 任务列表, 按顺序执⾏任务.

handlers: 类似task,但需要使⽤notify通知调⽤.

不管有多少个通知者进⾏了notify,等到play中的所有task执⾏完 成之后,handlers也只会被执⾏⼀次. handlers最佳的应⽤场景是⽤来重启服务,或者触发系统重启操 作.除此以外很少⽤到了

[root@m0 ~]# vim test001.yml

---

-       hosts:          group01

        remote_user:    root

        tasks:

        -       name:   卸载vsftp

                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 ./test001.yml

[root@m0 ~]# vim test002.yml

---

-       hosts:          group01

        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 "xxxxxxx" > /var/www/html/index.html

       

        -       name:   启动httpd服务

                service:        name=httpd   state=started

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

[root@m0 ~]# vim /etc/ansible/hosts

s1 ansible_ssh_host=192.168.1.51 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=q

s2 ansible_ssh_host=192.168.1.52 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=q

[root@m0 ~]# vim test003.yml

---

-     hosts:         s1

      remote_user:   root

      tasks:

      -    name:   安装nfs-utils

           yum:    name=nfs-utils    state=latest

      -    name:   安装rpcbind

           yum:    name=rpcbind      state=latest

      -    name:   创建共享目录

           file:   path=/static      state=directory

      -    name:   配置文件

           shell:  echo '/static    *(ro,sync)' > /etc/exports

      -    name:        启动服务nfs

           service:     name=nfs          state=started   enabled=yes

      -    name:        启动服务rpdbind  

           service:     name=rpcbind      state=started   enabled=yes

-     hosts:           s2

      remote_user:     root

      tasks:

      -         name:    安装nfs-utils

                yum:     name=nfs-utils   state=latest

      -         name:     创建挂载目录

                file:     path=/nfs       state=directory

      -         name:     挂载nfs文件

                command:      mount -t nfs 192.168.1.52:/static /nfs

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值