ansible之playbook

一、介绍

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

使⽤的格式为yaml格式(saltstack,elk,docker,docker,compose,kubernetes等也都会⽤到yaml格式)

二、YMAL格式

1)以.yaml或.yml结尾

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

3)以#号开头为注释

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

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

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

三、playbook实例

1. playbook编排vsftpd

root@m0 \~# vim test001.yml


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

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

tasks: //任务列表, 按顺序执⾏任务,handlers: 类似task,但需要使⽤notify通知调⽤

  • 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 //执行

2.修改httpd的端⼝为8080,再执⾏playbook测试

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

3.编排nfs搭建与客户端挂载

root@m0 \~# vim /etc/ansible/hosts

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

s2 ansible_ssh_host=192.168.1.69 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.68:/static /nfs

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

相关推荐
垚垚学技术_聚焦云原生11 天前
Ansible Vault 安全实践:敏感密码、密钥加密存储方案
安全·elasticsearch·ansible
广目软件13 天前
HA分布式架构采集节点变更操作手册20261029
运维·服务器·ansible·zabbix·prometheus
Moshow郑锴14 天前
Ansible 批量部署 Node.js 服务:ZIP 直解与共享目录分发两种方案对比
node.js·ansible
垚垚学技术_聚焦云原生14 天前
Ansible Playbook 进阶:when 条件、loop 循环、handlers 触发器落地
linux·ansible·自动化运维·devops
Moshow郑锴14 天前
GCP MIG 重建后 Ansible 初始化实战:PowerShell + NSSM 挂载 Windows 服务
windows·ansible
承渊政道16 天前
新增一台服务器时还要重新装Nginx?Ansible只改hosts目标列表,同一份Playbook继续执行
运维·nginx·ansible·内网穿透·cpolar
Helix25018 天前
Ansible混合环境自动化:Windows与Linux统一配置管理实战
windows·自动化·ansible·playbook·配置管理·winrm·混合环境
垚垚学技术_聚焦云原生19 天前
Ansible 变量体系详解:主机变量、组变量、全局变量优先级实战
ansible·自动化运维·devops·变量优先级
I'mChloe1 个月前
不再逐台装Docker:我用Ansible Playbook批量统一服务器环境
服务器·docker·ansible
筑梦之路1 个月前
Alibaba Cloud Linux 4 制作 Ansible RPM包实践(离线可复用)——筑梦之路
linux·运维·ansible