ansible汇总(2)

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

三、playbook

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


参考:https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html


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


YMAL格式

以.yaml或.yml结尾

文件的第一行以 "—"开始,表明YMAL文件的开始(可选的)

以#号开头为注释

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

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

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

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


下面看一个官方的示例感受一下


---
# 一位职工记录
name: Example Developer
job: Developer
skill: Elite
employed: True
foods:
    - Apple
    - Orange
    - Strawberry
    - Mango
languages:
    ruby: Elite
    python: Elite
    dotnet: Lame


playbook实例

先直接来看一个实例


第1步: 创建一个存放playbook的目录(路径自定义)


master# mkdir /etc/ansible/playbook


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

master# yum install httpd -y
按需要修改你想要的配置(为了测试可以随意改动标记一下)
master# vim /etc/httpd/conf/httpd.conf


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


# vim /etc/ansible/playbook/example.yaml
---
- hosts: group1
  remote_user: root
  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
    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


第4步: 执行写好的palybook


会显示出执行的过程,并且执行的每一步都有ok,changed,failed等标识

执行如果有错误(failed)会回滚,解决问题后,直接再执行这条命令即可,并会把failed改为changed(幂等性)

# ansible-playbook /etc/ansible/playbook/example.yaml


Playbook常见语法

hosts: 用于指定要执行任务的主机,其可以是一个或多个由冒号分隔主机组.


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


- hosts: group1   
  remote_user: root


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通知调用。


不管有多少个通知者进行了notify,等到play中的所有task执行完成之后,handlers也只会被执行一次.

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


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


variables: 变量


定义变量可以被多次方便调用

master# vim /etc/ansible/playbook/example2.yaml
---
- hosts: group1
  remote_user: root
  vars:
  - user: test1
  tasks:
  - name: create user
    user: name={{user}} state=present
~


master# ansible-playbook /etc/ansible/playbook/example2.yaml


案例: playbook编排vsftpd

写一个playbook实现


配置yum

安装vsftpd包

修改配置文件(要求拒绝匿名用户登录)

启动服务并实现vsftpd服务开机自动启动

---
- hosts: group1                 
  remote_user: root                     
  tasks:                                
  - name: rm yum repository      
    file: path=/etc/yum.repos.d/ state=absent
  - name: 同步master上的yum源到group1
    copy: src=/etc/yum.repos.d dest=/etc/
  - name: ensure vsftpd is at the latest version        
    yum: name=vsftpd state=latest
  - name: write the apache config file          
    copy: src=/etc/vsftpd/vsftpd.conf dest=/etc/vsftpd/vsftpd.conf 
    notify:                             
    - restart vsftpd
  - name: ensure vsftpd is running (and enable it at boot)
    service: name=vsftpd state=started enabled=yes
  handlers:                     
    - name: restart vsftpd              
      service: name=vsftpd state=restarted


playbook编排多个hosts任务

---   # ---代表开始(可选项,不写也可以)
- hosts: 10.1.1.12
  remote_user: root
  tasks:
  - name: 创建/test1/目录
    file: path=/test1/ state=directory
# 这里不能用---分隔,会报语法错误(后面课程玩k8s编排也写YAML文件,是可以用---来分隔段落的)
- hosts: 10.1.1.13
  remote_user: root
  tasks:
  - name: 创建/test2/目录
    file: path=/test2/ state=directory
...   # ...代表结束(可选项,不写也可以)


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

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


# vim /etc/exports
/share  *(ro)


2, 编写yaml编排文件


# vim /etc/ansible/playbook/nfs.yml
---
- hosts: 10.1.1.12
  remote_user: root
  tasks:
  - name: 安装nfs服务相关软件包
    yum: name=nfs-utils,rpcbind,setup  state=latest
  - name: 创建共享目录
    file: path=/share/ state=directory
  - name: 同步nfs配置文件
    copy: src=/etc/exports dest=/etc/exports
    notify: restart nfs
  - name: 启动rpcbind服务,并设置为开机自启动
    service: name=rpcbind state=started enabled=on
  - name: 启动nfs服务,并设置为开机自启动
    service: name=nfs state=started enabled=on
  handlers:
  - name: restart nfs
    service: name=nfs state=restarted
- hosts: 10.1.1.13
  remote_user: root
  tasks:
  - name: 安装nfs客户端软件包
    yum: name=nfs-utils state=latest
  - name: 挂载nfs服务器的共享
    shell: mount 10.1.1.12:/share /mnt


3, 执行playbook


# ansible-playbook /etc/ansible/playbook/nfs.yaml



目录
相关文章
|
机器学习/深度学习 存储 API
2.2 通过极简方案实现数字识别任务
这篇文章通过极简方案快速实现了基于飞桨的手写数字识别任务,涵盖了数据加载、模型设计、训练配置、训练过程和模型测试等步骤,并指出了当前模型的局限性,同时提出了进一步优化模型的思考方向。
|
JSON 数据格式 Python
Python生成JSON数据
Python生成JSON数据
302 0
|
Java Spring
SpringBoot 开启事务Spring事务常用
SpringBoot 开启事务Spring事务常用
116 0
|
Java
Java Virtual Machine Process Status Tool <jps>
jps(Java Virtual Machine Process Status Tool)是java提供的一个显示当前所有java进程pid的命令
296 0
|
Cloud Native OLAP 数据库
有奖互动|7.19数据库升舱计划实战峰会:行业领袖齐聚,他们因何而来?
技术大咖联袂参与,多元视角深入洞察,与您一同探索数智化创新之路
有奖互动|7.19数据库升舱计划实战峰会:行业领袖齐聚,他们因何而来?
|
算法 Java Android开发
LinearLayout(线性布局)
本节开始讲Android中的布局,今天我们要讲解的就是第一个布局,LinearLayout(线性布局),我们屏幕适配的使用用的比较多的就是LinearLayout的weight(权重属性),在这一节里,我们会详细地解析LinearLayout,包括一些基本的属性,Weight属性的使用,以及比例如何计算,另外还会说下一个用的比较少的属性:android:divider绘制下划线!
197 0
|
JSON 缓存 JavaScript
.NET微信网页开发之JS-SDK使用步骤和配置信息timestamp(时间戳),nonceStr(随机串),signature(签名),access_token(接口调用凭据)的生成获取详解
.NET微信网页开发之JS-SDK使用步骤和配置信息timestamp(时间戳),nonceStr(随机串),signature(签名),access_token(接口调用凭据)的生成获取详解
862 0
.NET微信网页开发之JS-SDK使用步骤和配置信息timestamp(时间戳),nonceStr(随机串),signature(签名),access_token(接口调用凭据)的生成获取详解
|
数据可视化 Java 调度
可视化定时任务,quartz集成全解析
在日常的工作中,定时任务也是一个非常常见的需求,可以使用注解实现,但是使用这种方式有几个问题,首先就是如果修改比较麻烦,而且没有提供特定的页面对定时任务进行可视化管理。所以quartz就应运而生。本文将介绍如何实现springboot与quartz的整合。
817 0
可视化定时任务,quartz集成全解析
|
计算机视觉 Python
趣味Python — 不到20行代码制作一个 “手绘风” 视频
本期推文与计算机视觉相关,用不到 20 行Python代码将一张图片由自然风转化为手绘风,期间未对图片进行任何预处理、后处理;代码中只借助了两个常见库,核心计算由 Numpy 负责 ,Pillow 负责图片读写
趣味Python — 不到20行代码制作一个 “手绘风” 视频