Playbook剧本案例实战

script模块

script模块⽤于在远程机器上执⾏本地脚本。

# 在master上准备⼀个脚本

[root@m0 ~]#vim test.sh

mkdir /tmp/three

touch /tmp/three/test

echo 'i am echo,is used write' > /tmp/three/test

[root@m0 ~]#source test.sh

# 在group02的远程机器⾥都执⾏master上的/tmp/test.sh

脚本(此脚本不⽤给执⾏权限)

[root@m0 ~]#ansible group02 -m script -a '~/test.sh'

扩展: 使⽤shell脚本实现在group1的被管理机⾥的mariadb⾥创建⼀个abc库

#!/bin/bash

yum install mariadb-server -y &> /dev/null

systemctl start mariadb

systemctl enable mariadb

mysql << EOF

create database abc;

quit

EOF

# 把上⾯的脚本使⽤script模块在group02被管理机⾥执⾏

command与shell模块

两个模块都是⽤于执⾏linux命令的,这对于命令熟悉的⼯程师来说,⽤起来⾮常high。

shell模块与command模块差不多(command模块不能执⾏⼀些类似$HOME,>,<,|等符号,但shell可以)

https://docs.ansible.com/ansible/latest/modules/command_modul

e.html

# 在管理机上安装nfs、rpcbind

[root@m0 ~]#ansible group02 -m service -a 'name=nfs,state=started, enabled=yes'

[root@m0 ~]#ansible group02 yum -a 'name=rpcbind state=latest'

# 启动nfs

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

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

# 测试

[root@m0 ~]#ansible group02 -m file -a 'path=/static state=directory'

[root@m0 ~]#ansible group02 -m file -a 'path=/static/test01 state=touch'

[root@ab ~]# ls /static/

test01

# 共享

[root@m0 ~]#yum -y install nfs-utils

[root@m0 ~]#yum list installed|grep nfs

[root@m0 ~]#ls /nfs/

[root@m0 ~]#mount -t nfs 192.168.2.177:/static /nfs/

Playbook的使用

1、概述:

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

考:https://docs.ansible.com/ansible/latest/user_guide/playboks_intro.html,使⽤的格式为yaml格式(saltstack,elk,docker,docker-compose,kubernetes等也都会⽤到yaml格式)

2、基础语法:

   YMAL格式

(1)以.yaml或.yml结尾

(2)⽂件的第⼀⾏以 "---"开始,表明YMAL⽂件的开始(可选的)

(3)以#号开头为注释

(4)列表中的所有成员都开始于相同的缩进级别, 并且使⽤⼀个 "-" 作为开头(⼀个横杠和⼀个空格)

(5)⼀个字典是由⼀个简单的 键: 值 的形式组成(这个冒号后⾯必须是⼀个空格)

参考: https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html#yaml-syntax

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

3、playbook实例

第1步: 创建⼀个存放playbook的⽬录(路径⾃定义)

[root@m0 ~]#mkdir /etc/ansible/playbook

第2步: 准备httpd配置⽂件,并修改成你想要的配置

[root@m0 ~]#yum install httpd -y

按需要修改你想要的配置(为了测试可以随意改动标记⼀下)

[root@m0 ~]#vim /etc/httpd/conf/httpd.conf

第3步: 写⼀个playbook⽂件(后缀为.yml或.yaml)

[root@m0 ~]#vim /etc/ansible/playbook/example.yaml

---

- hosts: group02

remote_user: root

tasks:

- name: 安装apache

yum: name=httpd,httpd-devel state=latest

- name: write the apache config file

copy: src=/etc/httpd/conf/httpd.conf

dest=/etc/httpd/conf/httpd.conf

notify:

 - restart apache、

- name:  重启apache

service: name=httpd state=started enabled=yes

handlers:

- name: restart apache

service: name=httpd state=restarted

第4步: 执⾏写好的palybook

会显示出执⾏的过程,并且执⾏的每⼀步都有ok,changed,failed等标识,执⾏如果有错误(failed)会回滚,解决问题后,直接再执⾏这条命令即可,并会把failed改为changed(幂等性)。

Playbook常见语法

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

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

tasks: 任务列表, 按顺序执⾏任务。如果⼀个host执⾏task失败, 整个tasks都会回滚, 修正playbook中的错误, 然后重新执⾏即可。

tasks:

- name: ensure apache is at the latest version

yum: name=httpd,httpd-devel state=latest

- name: write the apache config file

copy: src=/etc/httpd/conf/httpd.conf

dest=/etc/httpd/conf/httpd.conf

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

1)不管有多少个通知者进⾏了notify,等到play中的所有task执⾏完成之后,handlers也只会被执⾏⼀次。

2)handlers最佳的应⽤场景是⽤来重启服务,或者触发系统重启操作,除此以外很少⽤到了。

notify:

- restart apache

- name: ensure apache is running (and enable it

at boot)

service: name=httpd state=started enabled=yes

handlers:

- name: restart apache

service: name=httpd state=restarted

variables: 变量,定义变量可以被多次⽅便调⽤。

[root@m0 ~]#vim /etc/ansible/playbook/example2.yaml

---

- hosts: group1

remote_user: root

vars:

- user: test1

tasks:

- name: create user

user: name={{user}} state=present

[root@m0 ~]#ansible-playbook /etc/ansible/playbook/example2.yaml

案例一: playbook编排vsftpd

# 先卸载之前安装过的vsftpd,再安装vsftpd,最后启动vsftpd

[root@m0 ~]#vim test001.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 ./test001.yml

# 测试

[root@ab ~]# yum list installed|grep vsftpd

vsftpd.x86_64                         3.0.2-29.el7_9                   @updates

[root@m0 ~]# vim test001.yml

---

- hosts: group02

  remote_user: root

  tasks:

  - name: 卸载vsftpd

    yum: name=vsftpd state=absent

  - name: 安装vsftpd

    yum: name=vsftpd state=latest

  - name: 启动vsftpd

    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: restart vsftpd

service: name=vsftpd state=restarted

案例二: 修改httpd的端⼝为8080,再执⾏playbook测试

[root@m0 ~]# vim test002.yml

---

- hosts: group02

  remote_user: root

  tasks:

  - name: 将管理机的repo文件复制到被控主机

    copy: scr=/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 'xxx' > /var/www/html/index.html

- name: 启动httpd服务

  service: name=httpd state=started

playbook编排多个hosts任务

案例: 编排nfs搭建与客户端挂载

1, 在master上准备nfs配置⽂件

[root@m0 ~]# vim /etc/exports

/share *(ro)

2,修改hosts主机文件

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

[s0]

192.168.2.177

[s1]

192.168.2.178

3, 编写yaml编排⽂件

[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
                notify:
                -       rstss

        -       name:           启动服务nfs
                service:        name=nfs        state=started enabled=yes
        -       name:           启动服务rpcbind
                service:        name=rpcbind    state=started enabled=yes
        handlers:
                -       name:   rstss
                        service:        name=nfs state=restarted

                # 这⾥不能⽤---分隔,会报语法错误(后⾯课程玩k8s编排也写YAML ⽂件,是可以⽤---来分隔段落的)

-       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.71.178:/static /nfs

4, 执行playbook

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值