Ansible Playbooks

栏目: 服务器 · 发布时间: 7年前

内容简介:与ad-hoc任务执行模式相比,Playbooks使用ansible是一种完全不同的方式,并且功能特别强大。简而言之,playbooks是真正简单的配置管理和多机器部署系统的基础,与已有的系统不同,并且非常适合部署复杂的应用程序。Playbooks可以声明配置,但它们也可以协调任何手动有序流程的步骤,即使不同的步骤必须按照特定顺序在机器组之间来回跳转。它们可以同步或异步启动任务。

与ad-hoc任务执行模式相比,Playbooks使用ansible是一种完全不同的方式,并且功能特别强大。

简而言之,playbooks是真正简单的配置管理和多机器部署系统的基础,与已有的系统不同,并且非常适合部署复杂的应用程序。

Playbooks可以声明配置,但它们也可以协调任何手动有序流程的步骤,即使不同的步骤必须按照特定顺序在机器组之间来回跳转。它们可以同步或异步启动任务。

更多playbooks的介绍参考 官方文档

Playbooks以YAML格式表示(请参阅 YAML语法 ),具有最少的语法,它不是编程语言或脚本,而是配置或进程的模型。

每个剧本由列表中的一个或多个“戏剧”组成。戏剧的目标是将一组主机映射到一些定义明确的角色,由ansible调用任务表示。在基本级别,任务只不过是对ansible模块的调用(请参阅 使用模块 )。

通过编写多个“戏剧”的剧本,可以编排多机部署,在Web服务器组中的所有计算机上运行某些步骤,然后在数据库服务器组上执行某些步骤,然后在Web服务器组上执行更多命令,等等。。

你可以有很多戏剧影响你的系统做不同的事情。这并不是说你只是定义了一个特定的状态或模型,而是可以在不同的时间运行不同的戏剧。

Playbooks Demo

以下是只包含一个戏剧的剧本:

---
- hosts: webservers
  vars:
    http_port: 80
    max_clients: 200
  remote_user: root
  tasks:
  - name: ensure apache is at the latest version
    yum:
      name: httpd
      state: latest
  - name: write the apache config file
    template:
      src: /srv/httpd.j2
      dest: /etc/httpd.conf
    notify:
    - restart apache
  - name: ensure apache is running
    service:
      name: httpd
      state: started
  handlers:
    - name: restart apache
      service:
        name: httpd
        state: restarted
  • 文件开头三个横杠代表yaml文件。
  • hosts表示一个主机组。
  • tasks表示动作集合。
  • name是一个注释说明。
  • yum和下面两行,表示使用yum安装最新版的httpd。
  • template和下面两行,表示使用ansible的template模块传输/srv/httpd.j2作为客户机的/etc/httpd.conf。和copy相比,template支持jinja语法。
  • notify和下面一行,表示传输成功后触发重启apache命令。它需要和handlers组合使用。
  • handlers中的name和notify中的信息要完全相同。service和下面两行,表示重启httpd。

Playbooks可以包含多个戏剧。您可能有一个首先针对Web服务器,然后是数据库服务器的playbook。例如:

---
- hosts: webservers
  remote_user: root

  tasks:
  - name: ensure apache is at the latest version
    yum:
      name: httpd
      state: latest
  - name: write the apache config file
    template:
      src: /srv/httpd.j2
      dest: /etc/httpd.conf

- hosts: databases
  remote_user: root

  tasks:
  - name: ensure postgresql is at the latest version
    yum:
      name: postgresql
      state: latest
  - name: ensure that postgresql is started
    service:
      name: postgresql
      state: started

入门实例

目标:编写一个playbook,在客户机上安装chrony,然后检查启动情况。

1、新建chrony.yml,内容为:

---
- hosts: commonservers
  tasks:
  - name: install chrony
    apt:
      name: chrony
      state: latest
    notify:
    - restart chrony
  - name: ensure chrony is running
    service:
      name: chrony
      state: started
  handlers:
    - name: restart chrony
      service:
        name: chrony
        state: restarted

上面的playbook中用到了apt模块,如果有疑问可以查看帮助, ansible-doc apt 。实际上,有一种更加简单的写法,就是把apt模块换成command模块,然后直接写命令。

2、检查playbook

ansible-playbook chrony.yml --syntax-check

3、以sudo权限执行playbook

ansible-playbook chrony.yml --user=voidking --private-key=/home/voidking/.ssh/id_rsa -s
Ansible Playbooks

4、在客户机测试

chronyc sources
Ansible Playbooks

角色

如果需要将一个大文件拆分为各个小文件,我们经常使用的就是include,这也是原先ansible拆分文件的做法。如今ansible使用roles来拆分文件,将nginx、 mysql 等分为各个角色,在各个角色内定义具体的小任务,方便管理。另一方面,类似于 php 类的自动加载,roles基于一个已知的文件结构,可以自动去加载某些vars_files、tasks、handlers等。

目标:使用角色的方式,编写配置playbook,在客户机上安装chrony,然后检查启动情况。

1、创建chrony角色目录

mkdir -p roles/chrony/tasks

mkdir -p roles/chrony/handlers

2、创建总的入口文件site.yml,内容为:

---
- hosts: commonservers
  roles:
    - chrony

3、创建安装剧本

vim roles/chrony/tasks/main.yml ,内容为:

---
- name: install chrony
  apt:
    name: chrony
    state: latest
  notify:
  - restart chrony
- name: ensure chrony is running
  service:
    name: chrony
    state: started

vim roles/chrony/handlers/main.yml ,内容为:

---
- name: restart chrony
  service:
    name: chrony
    state: restarted

4、以sudo权限执行playbook

ansible-playbook site.yml --user=voidking --private-key=/home/voidking/.ssh/id_rsa -s

Ansible Playbooks

5、在客户机测试

chronyc sources
Ansible Playbooks

简单编程

变量

---
- hosts: commonservers
  vars:
    pre1: all info is 
    pre2: hostname is
  tasks:
  - name: register vars
    shell: hostname
    register: info
  - name: display info
    debug: msg="{{pre1}} {{info}}"
  - name: display hostname
    debug: msg="{{pre2}} {{info.stdout}}"

Ansible Playbooks

基本循环

---
- hosts: commonservers
  tasks:
  - name: loops demo
    debug: msg="{{item}}"
    with_items:
      - one
      - two
      - three

Ansible Playbooks

循环字典

---
- hosts: commonservers
  tasks:
  - name: loops dict
    debug: msg="key -> {{item.key}},value -> {{item.value}}"
    with_items:
      - {key: 1, value: "one"}
      - {key: 2, value: "two"}
      - {key: 3, value: "three"}

Ansible Playbooks

嵌套循环

---
- hosts: commonservers
  tasks:
  - name: loops2 
    debug: msg="item0 -> {{item[0]}},item1 -> {{item[1]}}"
    with_nested:
      - ['1','2']
      - ['one','two','three']

Ansible Playbooks

散列循环

---
- hosts: commonservers
  vars:
    user: 
      voidking:
        name: voidking
        tel: 17600000000
      haojin:
        name: haojin
        tel: 15100000000
  tasks:
  - name: loops3 
    debug: msg="key -> {{item.key}},value -> {{item.value}}"
    with_dict:
      - "{{user}}"

Ansible Playbooks

文件循环

---
- hosts: commonservers
  tasks:
  - name: loop file
    debug: msg="{{item}}"
    with_fileglob:
      - /home/voidking/*.yml

Ansible Playbooks

命令循环

---
- hosts: commonservers
  tasks:
    - name: exec command
      shell: "{{item}}"
      with_items:
        - hostname
        - uname
      register: ret
    - name: display result
      debug: msg="{% for i in ret.results %} {{i.stdout}} {% endfor %}"

Ansible Playbooks

条件判断

ansible的条件判断使用关键字when,有两种方式:

  • python语法支持的原生态格式 conditions > 1 or conditions == “ss”,in,not等等
  • ansible Jinja2 filters
---
- hosts: commonservers
  vars:
    pre: hostname is
  tasks:
  - name: register vars
    shell: hostname
    register: info
  - name: display hostname
    debug: msg="{{pre}} {{info.stdout}}"
  - name: print true
    debug: msg="result is ubuntu14"
    when: info.stdout == "ubuntu14"
  - name: print false
    debug: msg="result is not ubuntu14"
    when: info.stdout != "ubuntu14"
  - name: print warning
    debug: msg="client OS is ubuntu"
    when: info['stdout'].startswith('u')

Ansible Playbooks


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Domain-Driven Design Distilled

Domain-Driven Design Distilled

Vaughn Vernon / Addison-Wesley Professional / 2016-6-2 / USD 36.99

Domain-Driven Design (DDD) software modeling delivers powerful results in practice, not just in theory, which is why developers worldwide are rapidly moving to adopt it. Now, for the first time, there......一起来看看 《Domain-Driven Design Distilled》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

html转js在线工具
html转js在线工具

html转js在线工具