一 Ansible Playbook简介

ansbile-playbook是一系列ansible命令的集合,利用yaml 语言编写。playbook命令根据自上而下的顺序依次执行。同时,playbook开创了很多特性,它可以允许你传输某个命令的状态到后面的指令,如你可以从一台机器的文件中抓取内容并附为变量,然后在另一台机器中使用,这使得你可以实现一些复杂的部署机制,这是ansible命令无法实现的。

playbook通过ansible-playbook命令使用,它的参数和ansible命令类似,如参数-k(–ask-pass) 和 -K (–ask-sudo) 来询问ssh密码和sudo密码,-u指定用户,这些指令也可以通过规定的单元写在playbook 。
ansible-playbook的简单使用方法: ansible-playbook example-play.yml 。

二 Playbook基本语法

下面是一个简单的ansible-playbook示例,可以了解其构成:

# cat user.yml
- name: create user
hosts: all
remote_user: root
gather_facts: false
vars:
user:"test"
tasks:
- name: create user
user: name="{{ user }}"

配置项说明:

  • name:对该playbook实现的功能做一个概述,后面执行过程中,会打印 name变量的值
  • hosts:指定对哪些被管理机进行操作;
  • remote_user:指定在远程被管理机上执行操作时使用什么用户,如不指定,则使用ansible.cfg中配置的remote_user
  • gather_facts:指定在执行任务之前,是否先执行setup模块获取主机相关信息,如未用到,可不指定
  • vars:定义后续任务中会使用到的变量,如未用到,可不指定
  • tasks:定义具体需要执行的任务name:对任务的描述,在执行过程中会打印出来。
  • user:指定调用user模块;
  • name:user模块里的一个参数,用于指定创建的用户名称

同样,如果想实现把这个新增的用户删除,只需将该playbook文件的最后一行替换为如下行再执行相应的playbook即可:

user: name="{{ user }}" state=absent remove=yes

三 playbook组成

3.1 palybook简单演示

- name: configure nginx
hosts: 192.168.132.133
tasks:
- name: install epel-release
package:
name: epel-release
state: present
- name: install nginx
package:
name: nginx
state: present
- name: create directory
file:
path: ./file
state: directory
owner: nginx
group: nginx
- name: copy file from remote serevr
fetch:
src: /usr/share/nginx/html/index.html
dest: file/
- name: delete file of remote server
file:
path: /usr/share/nginx/html/index.html
state: absent
- name: create a remote server file
file:
path: /usr/share/nginx/html/index.html
state: touch
owner: nginx
group: nginx
- name: content
copy:
content: "<h3>welcome ansible</h3>"
dest: /usr/share/nginx/html/index.html
- name: start service
systemd:
name: nginx
state: started
enabled: yes
daemon_reload: yes

[root@node1 ansible]# ansible-playbook nginx_config.yml

PLAY [configure nginx] ************************************************************************************************************************
TASK [Gathering Facts] ************************************************************************************************************************
ok: [192.168.132.133]
TASK [install epel-release] *******************************************************************************************************************
changed: [192.168.132.133]
TASK [install nginx] **************************************************************************************************************************
changed: [192.168.132.133]
TASK [create directory] ***********************************************************************************************************************
ok: [192.168.132.133]
TASK [copy file from remote serevr] ***********************************************************************************************************
changed: [192.168.132.133]
TASK [delete file of remote server] ***********************************************************************************************************
changed: [192.168.132.133]
TASK [create a remote server file] ************************************************************************************************************
changed: [192.168.132.133]
TASK [content] ********************************************************************************************************************************
changed: [192.168.132.133]
TASK [start service] **************************************************************************************************************************
changed: [192.168.132.133]
PLAY RECAP ************************************************************************************************************************************
192.168.132.133 : ok= changed= unreachable= failed= skipped= rescued= ignored=

查看192.168.132.133

[root@node3 ~]# rpm -qa |grep epel-release
epel-release--.noarch
[root@node3 ~]# rpm -qa |grep nginx
nginx-mod-http-xslt-filter-1.16.-.el7.x86_64
nginx-all-modules-1.16.-.el7.noarch
nginx-mod-mail-1.16.-.el7.x86_64
nginx-mod-http-image-filter-1.16.-.el7.x86_64
nginx-mod-stream-1.16.-.el7.x86_64
nginx-filesystem-1.16.-.el7.noarch
nginx-mod-http-perl-1.16.-.el7.x86_64
nginx-1.16.-.el7.x86_64
[root@node3 ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Wed -- :: CST; 2min 25s ago
[root@node3 ~]# systemctl is-enabled nginx
enabled

访问

3.2 yml规则

yml文件它的基本语法规则如下。

  • 大小写敏感
  • 使用缩进表示层级关系
  • 缩进时不允许使用Tab键,只允许使用空格。
  • 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可

# 表示注释,从这个字符一直到行尾,都会被解析器忽略。

YAML 支持的数据结构有三种。

  • 对象:键值对的集合,又称为映射(mapping)/ 哈希(hashes) / 字典(dictionary)
  • 数组:一组按次序排列的值,又称为序列(sequence) / 列表(list)
  • 纯量(scalars):单个的、不可再分的值

ansible-playbook常用选项

3.3 ansible其他用法

1 打印详细信息

  • -v:打印任务运行结果
  • -vv:打印任务运行结果以及任务的配置信息
  • -vvv:包含了远程连接的一些信息
  • -vvvv:Adds extra verbosity options to the connection plug-ins,including the users being used in the managed hosts to execute scripts, and what scripts have been executed

[root@node1 ansible]# ansible-playbook nginx_config.yml -v

2. 校验playbook语法

[root@node1 ansible]# ansible-playbook nginx_config.yml --syntax-check

playbook: nginx_config.yml

3. 测试运行playbook

通过-C选项可以测试playbook的执行情况,但不会真的执行:

[root@node1 ansible]# ansible-playbook -C nginx_config.yml

4 Multiple Plays

# This is a simple playbook with two plays

- name: first play
hosts: web.example.com
tasks:
- name: first task
yum:
name: httpd
status: present
- name: second task
service:
name: httpd
state: started - name: second play
hosts: db.example.com
tasks:
- name: first task
yum:
name: mariadb-server
status: present
- name: second task
service:
name: mariadb
state: started

四 playbook的结构说明

playbook是由一个或多个"play"组成的列表。play的主要功能就是对一组主机应用play中定义好的task。从根本上来讲一个task就是对ansible一个module的调用。而将多个play按照一定的顺序组织到一个playbook中,我们称之为编排。

playbook主要有以下四部分构成:

  • Target section: 用于定义将要执行playbook的远程主机组及远程主机组上的用户,还包括定义通过什么样的方式连接远程主机(默认ssh)
  • Variable section: 定义playbook运行时需要使用的变量
  • Task section: 定义将要在远程主机上执行的任务列表
  • Handler section: 定义task执行完成以后需要调用的任务

4.1 Target section

playbook中的每一个play的目的都是为了让某个或某些主机以某个指定的用户身份执行任务。

Playbook中的远程用户

playbook中的远程用户和ad-hoc中的使用没有区别,默认不定义,则直接使用ansible.cfg配置中的用户相关的配置。也可在playbook中定义如下:

- name: /etc/hosts is up to date
hosts: datacenter
remote_user: automation
become: yes
become_mothod: sudo
become_user: root tasks:
- name: server.example.com in /etc/hosts
lineinfile:
path: /etc/hosts
line: '192.168.0.200 server.exmaple.com server'
state: present

Playbook中的hosts

playbook中的hosts即inentory中的定义主机与主机组,在《Ansible Inventory》中我们讲到了如何选择主机与主机组,在这里也完全适用。

4.2 Variable section

定义playbook运行时需要使用的变量

- name : test vars
hosts: node2
remote_user: ansible
become_user: root
become_method: sudo vars:
user:
name: "user1"
address: "IPaddr"
tasks:
- name: "print"
debug:
msg: "{{ user }}"

执行

PLAY [test vars] ******************************************************************************************************************************
TASK [Gathering Facts] ************************************************************************************************************************
ok: [node2]
TASK [print] **********************************************************************************************************************************
ok: [node2] => {
"msg": {
"address": "IPaddr",
"name": "user1"
}
}
PLAY RECAP ************************************************************************************************************************************
node2 : ok= changed= unreachable= failed= skipped= rescued= ignored=

4.3 Task section

play的主体部分是任务列表。

任务列表中的各任务按次序逐个在hosts中指定的所有主机上执行,在所有主机上完成第一个任务后再开始第二个。在自上而下运行某playbook时,如果中途发生错误,则整个playbook会停止执行,由于playbook的幂等性,playbook可以被反复执行,所以即使发生了错误,在修复错误后,再执行一次即可。

定义task可以使用action: module optionsmodule: options的格式,推荐使用后者以实现向后兼容。

tasks:
- name: make sure apache is running
service:
name: httpd
state: started - name: disable selinux
command: /sbin/setenforce

如果命令或脚本的退出码不为零可以使用如下方式替代:

tasks:
- name: run this command and ignore the result
shell: /usr/bin/somecommand || /bin/true

可以使用ignore_errors来忽略错误信息:

tasks:
- name: run this command and ignore the result
shell: /usr/bin/somecommand
ignore_errors: True

4.4 Handler section

  • 在Ansible Playbook中,handler事实上也是个task,只不过这个task默认并不执行,只有在被触发时才执行。

  • handler通过notify来监视某个或者某几个task,一旦task执行结果发生变化,则触发handler,执行相应操作。

  • handler会在所有的play都执行完毕之后才会执行,这样可以避免当handler监视的多个task执行结果都发生了变化之后而导致handler的重复执行(handler只需要在最后执行一次即可)。

在notify中定义内容一定要和tasks中定义的 - name 内容一样,这样才能达到触发的效果,否则会不生效。

- name: configure nginx
hosts: 192.168.132.133
tasks:
- name: install epel-release
package:
name: epel-release
state: present
- name: install nginx
package:
name: nginx
state: present
notify: restart nginx
- name: create directory
file:
path: ./file
state: directory
owner: nginx
group: nginx
- name: copy file from remote serevr
fetch:
src: /usr/share/nginx/html/index.html
dest: file/
- name: delete file of remote server
file:
path: /usr/share/nginx/html/index.html
state: absent
- name: create a remote server file
file:
path: /usr/share/nginx/html/index.html
state: touch
owner: nginx
group: nginx
- name: content
copy:
content: "<h3>welcome ansible</h3>"
dest: /usr/share/nginx/html/index.html
- name: start service
systemd:
name: nginx
state: started
enabled: yes
daemon_reload: yes
handlers:
- name: restart nginx
systemd:
name: nginx
daemon_reload: yes
state: restarted

执行

PLAY [configure nginx] ************************************************************************************************************************
TASK [Gathering Facts] ************************************************************************************************************************
ok: [192.168.132.133]
TASK [install epel-release] *******************************************************************************************************************
ok: [192.168.132.133]
TASK [install nginx] **************************************************************************************************************************
ok: [192.168.132.133]
TASK [create directory] ***********************************************************************************************************************
ok: [192.168.132.133]
TASK [copy file from remote serevr] ***********************************************************************************************************
ok: [192.168.132.133]
TASK [delete file of remote server] ***********************************************************************************************************
changed: [192.168.132.133]
TASK [create a remote server file] ************************************************************************************************************
changed: [192.168.132.133]
TASK [content] ********************************************************************************************************************************
changed: [192.168.132.133]
TASK [start service] **************************************************************************************************************************
ok: [192.168.132.133]
PLAY RECAP ************************************************************************************************************************************
192.168.132.133 : ok= changed= unreachable= failed= skipped= rescued= ignored=

没有触发,试音并没有change

- name: configure nginx
hosts: 192.168.132.133
tasks:
- name: install epel-release
package:
name: epel-release
state: present
- name: install nginx
package:
name: nginx
state: present
- name: create directory
file:
path: ./file
state: directory
owner: nginx
group: nginx
- name: copy file from remote serevr
fetch:
src: /usr/share/nginx/html/index.html
dest: file/
- name: delete file of remote server
file:
path: /usr/share/nginx/html/index.html
state: absent
notify: restart nginx
- name: create a remote server file
file:
path: /usr/share/nginx/html/index.html
state: touch
owner: nginx
group: nginx
- name: content
copy:
content: "<h3>welcome ansible</h3>"
dest: /usr/share/nginx/html/index.html
- name: start service
systemd:
name: nginx
state: started
enabled: yes
daemon_reload: yes
handlers:
- name: restart nginx
systemd:
name: nginx
daemon_reload: yes
state: restarted

执行

[root@node1 ansible]# ansible-playbook  nginx_config.yml

PLAY [configure nginx] ************************************************************************************************************************
TASK [Gathering Facts] ************************************************************************************************************************
ok: [192.168.132.133]
TASK [install epel-release] *******************************************************************************************************************
ok: [192.168.132.133]
TASK [install nginx] **************************************************************************************************************************
ok: [192.168.132.133]
TASK [create directory] ***********************************************************************************************************************
ok: [192.168.132.133]
TASK [copy file from remote serevr] ***********************************************************************************************************
ok: [192.168.132.133]
TASK [delete file of remote server] ***********************************************************************************************************
changed: [192.168.132.133
]
TASK [create a remote server file] ************************************************************************************************************
changed: [192.168.132.133]
TASK [content] ********************************************************************************************************************************
changed: [192.168.132.133]
TASK [start service] **************************************************************************************************************************
ok: [192.168.132.133]
RUNNING HANDLER [restart nginx] ***************************************************************************************************************
changed: [192.168.132.133
]
PLAY RECAP ************************************************************************************************************************************
192.168.132.133 : ok= changed= unreachable= failed= skipped= rescued= ignored=

触发执行

一个任务可以触发多个handlers

notify:
- restart nginx
- restart redis

默认情况下,在一个play中,只要有task执行失败,则play终止,即使是与handler关联的task在失败的task之前运行成功了,handler也不会被执行。如果希望在这种情况下handler仍然能够执行,则需要使用如下配置:

- hosts: all
force_handlers: yes
tasks:
- name: a task which always notifies its handler
command: /bin/true
notify: restart the database
- name: a task which fails because the package doesn't exist
yum:
name: notapkg
state: latest handlers:
- name: restart the database
service:
name: mariadb

如果与handler关联的task还未执行,在其前的task已经失败,整个play终止,则handler未被触发,也不会执行


博主声明:本文的内容来源主要来自誉天教育晏威老师,由本人实验完成操作验证,需要的博友请联系誉天教育(http://www.yutianedu.com/),获得官方同意或者晏老师(https://www.cnblogs.com/breezey/)本人同意即可转载,谢谢!

005.Ansible de palybook简单使用的更多相关文章

  1. ansible写一个简单的playbook

    前言 实现的功能很简单,就是通过ansible批量完成某个账户sudo权限的开通或关闭 目录结构 ├── group_vars #放置各种变量的目录,我这里没用 ├── hosts #主机和组配置,默 ...

  2. 1、Ansible简介及简单安装、使用

    参考Ansible权威指南:https://ansible-tran.readthedocs.io/en/latest/index.html 以下内容学习自马哥教育 Ansible: 运维工作:系统安 ...

  3. Ansible安装及简单使用备注

    1.安装epel源: rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm 2.安装: yum ...

  4. ansible简介,简单实用

    Ansible ansilbe是实现自动化运维的工具,基于python开发,实现批量系统配置,批量程序部署,批量运行命令等功能. ansible是基于模块工作的,自身是没有批量部署的能力.真正具有批量 ...

  5. ansible的playbook简单使用

    一.介绍 playbook就是一个用yaml语法把多个模块堆起来的一个文件 核心组件: Hosts:执行的远程主机列表Tasks:任务,由模块定义的操作的列表:Varniables:内置变量或自定义变 ...

  6. ansible学习之--简单学习笔记1

    1.利用dm-crypt来创建加密文件系统.编写shell脚本(安装和卸载两个shell脚本) 2.编写ansible,playbook文件 3.编写python脚本 首先编写shell脚本 inst ...

  7. 005 android jni 一个简单的报错

    在android中使用ndk开发需要使用到jni. 1. java.lang.UnsatisfiedLinkError: No implementation found for void com.fr ...

  8. php大力力 [005节] php大力力简单计算器001

    2015-08-22 php大力力005. php大力力简单计算器001: 上网看视频,看了半天,敲击代码,如下: <html> <head> <title>简单计 ...

  9. 15.Ansible安装与配置简单版

    Ansible是一个简单高效的自动化运维管理工具,用Python开发,能大批量管理N多台机器,可以并发的在多台机器上部署应用.安装软件.执行命令.配置和编排任务. 一.Ansible工作机制 从图中可 ...

随机推荐

  1. Android | 教你如何用代码开发一个拍照翻译小程序

    引子   想必有很多小伙伴喜欢外出旅游,能去海外玩一圈那是更好不过了,旅游前大家一定会对吃.穿.住.行.游玩路线做各种攻略,然后满怀期待的出发- 想象中的旅游   出发前,想象中的旅游目的地可能有漂亮 ...

  2. dict字典的用法

    在用dict遇到了一些困难,记一下. 代码1: books={"倚天屠龙记":{"id":1,"price":100}, "好吗好 ...

  3. webpack-bundle-analyzer打包文件分析工具

    一.安装 npm intall webpack-bundle-analyzer –save-dev 二.配置 在build/webpack.prod.config.js中的module.exports ...

  4. Html 慕课园编程练习9-22

    题目要求: 制作一个表格,显示班级的学生信息. 要求: 1. 鼠标移到不同行上时背景色改为色值为 #f2f2f2,移开鼠标时则恢复为原背景色 #fff 2. 点击添加按钮,能动态在最后添加一行 3. ...

  5. Golang入门(1):安装与配置环境变量的意义

    摘要 在几年前学习Java的时候,环境的配置就会劝退一部分的初学者.而对于Golang来说,也需要从环境的配置开始学起.这一篇文章将从如何安装Golang开始讲起,随后将会提到Golang中的环境变量 ...

  6. JVM类加载过程详细分析

    双亲委派加载模型 为什么需要双亲委派加载模型 主要是为了安全,避免用户恶意加载破坏JVM正常运行的字节码文件,比如说加载一个自己写的java.util.HashMap.class.这样就有可能造成包冲 ...

  7. 在写微信小程序如何 首次编译的是当前写的页面

    首先点击顶部的编译如下图 染后点击添加模式哈 选择页面加载是启动的是哪一个页面

  8. android 软件(app)之家庭版记账本首次进行helloword等相关测试

    在进行对于app的创建之前是对于android studio的相关安装的环境的配置,完成这些之后自己就写个一个简单的helloword的实例进行了测试.之后通过进一步的向下挖掘,发现当将hellowo ...

  9. 【python实现卷积神经网络】padding2D层实现

    代码来源:https://github.com/eriklindernoren/ML-From-Scratch 卷积神经网络中卷积层Conv2D(带stride.padding)的具体实现:https ...

  10. AJ学IOS 之微博项目实战(7)程序启动新特性用UICollectionViewController实现

    AJ分享,必须精品 一:效果 这里实现了大多数app都会有的软件新特性的功能,用的是UICollectionViewController实现的 二:思路 这里用了UICollectionViewCon ...