http://blog.keshi.org/hogememo/2015/12/07/exploiting-ansible-jinja2

http://blog.keshi.org/hogememo/2014/10/12/ansible-credential-management

目录结构

group_vars组变量,host_vars主机变量

tvmhls/
├── channels.list.j2
├── channel.yml
├── group_vars
│   └── all.yml
├── hosts.j2
├── host_vars
│   └── 10.20.10.234.yml
├── inventory
└── readme
all.yml
---
links:
proxy.crc.01:
ip: 192.168.4.248
port: 80 proxy.ct.01:
ip: 10.10.111.31
port: 80 proxy.ct.02:
ip: 10.20.20.111
port: 80 proxy.cu.01:
ip: 10.10.111.32
port: 80 proxy.cu.02:
ip: 10.20.20.112
port: 80

10.20.10.234.yml

---
channels:
- name: BTV1HD
link: proxy.ct.01
status: disable
picture: iphone
type:
- iphone - name: ShenZhenHD
link: proxy.cu.02
status: disable
picture: ipad
type:
- iphone - name: CCTV3HD
link: proxy.ct.01
status: enable
picture: ipad
type:
- ipad

channel.yml

---
- hosts: '{{ hosts }}'
gather_facts: no
vars:
host_links: |
{% set l = [] %}
{% for channel in channels|sort(case_sensitive=True, attribute='link') %}
{% set _ = l.append(links[channel.link].ip+' '+channel.link) %}
{% endfor %}
{{ l | unique }} host_channels: |
{% set c = [] %}
{% for channel in channels|sort(case_sensitive=True, attribute='name') %}
{% set s = '#' if channel.status == 'disable' else '' %}
{% for type in channel.type|sort %}
{% set p = '|1' if channel.picture == type else '' %}
{% set _ = c.append(s+channel.link+':'+links[channel.link].port|string()+'|wxcenter|'+channel.name+'|'+type+'|60'+p) %}
{% endfor %}
{% endfor %}
{{ c }} tasks:
- name: update /etc/hosts
template: src=hosts.j2 dest=/etc/hosts owner=root group=root mode=0644
notify:
- restart svscan - name: update /opt/script/channels.list
template: src=channels.list.j2 dest=/opt/script/channels.list owner=root group=root mode=0644
notify:
- delete channel file
- restart svscan handlers:
- name: delete channel file
shell: /bin/find /opt/online01/{m3u8,muxer} -name "{{ item.name }}"* | /usr/bin/xargs /bin/rm -rf
when: item.status == 'disable'
with_items: "{{ channels }}" - name: restart svscan
command: /sbin/initctl restart svscan

hosts.j2

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 {% for link in host_links -%}
{{ link }}
{% endfor %}

channels.list.j2

[/opt/download:/opt/tvmcap]
{% for channel in host_channels -%}
{{ channel }}
{% endfor %}

inventory

[wx:children]
local
backup
beijing
shanghai [local]
127.0.0.1 [backup]
10.20.10.234 [beijing]
10.20.10.225
10.20.10.232
10.20.10.240
10.20.10.241
10.20.10.242 [shanghai]
10.20.10.230
10.20.10.233
10.20.10.237

run playbook

ansible-playbook -i inventory channel.yml --user=root --ask-pass --connection=ssh --extra-vars 'hosts=10.20.10.234'

/etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 10.10.111.31 proxy.ct.01
10.20.20.112 proxy.cu.02

/opt/script/channels.list

[/opt/download:/opt/tvmcap]
#proxy.ct.01:80|wxcenter|BTV1HD|iphone|60|1
proxy.ct.01:80|wxcenter|CCTV3HD|ipad|60|1
#proxy.cu.02:80|wxcenter|ShenZhenHD|iphone|60

ansible playbook 示例的更多相关文章

  1. ansible笔记(11):初识ansible playbook(二)

    ansible笔记():初识ansible playbook(二) 有前文作为基础,如下示例是非常容易理解的: --- - hosts: test211 remote_user: root tasks ...

  2. ansible笔记(10):初识ansible playbook

    ansible笔记():初识ansible playbook 假设,我们想要在test70主机上安装nginx并启动,我们可以在ansible主机中执行如下3条命令 ansible test70 -m ...

  3. Ansible playbook 批量修改服务器密码 先普通后root用户

    fsckzy   Ansible playbook 批量修改服务器密码 客户的需求:修改所有服务器密码,密码规则为Rfv5%+主机名后3位 背景:服务器有CentOS6.7,SuSE9.10.11,r ...

  4. Ansible playbook基础组件介绍

    本节内容: ansible playbook介绍 ansible playbook基础组件 playbook中使用变量 一.ansible playbook介绍 playbook是由一个或多个“pla ...

  5. ansible入门四(Ansible playbook基础组件介绍)

    本节内容: ansible playbook介绍 ansible playbook基础组件 playbook中使用变量 一.ansible playbook介绍 playbook是由一个或多个“pla ...

  6. ansible playbook模式及语法

    一.什么是playbook及其组成 什么是playbook playbook 翻译过来就是"剧本" playbook的组成 play:定义的是主机的角色 task:定义的是具体执行 ...

  7. ansible - playbook(剧组)

    目录 ansible - playbook(剧组) 常用命令 五种传参方式 常用元素详解 tags handlers template when 循环 嵌套循环 ansible - playbook( ...

  8. Ansible--02 ansible playbook的应用

    目录 Ansible playbook的应用 什么是playbook playbook的组成 playbook和Ad-Hoc对比 YAML语法 安装httpd练习 rsyncd实战 实战1: 实战2: ...

  9. ansible playbook详解

    ansible playbook是由yml语法书写,结构清晰,可读性强,所以必须掌握yml基础语法 语法 描述 缩进 YAML使用固定的缩进风格表示层级结构,每个缩进由两个空格组成,不能使用tabs键 ...

随机推荐

  1. 酷炫ILOVEU

    assume cs:code code segment main: ;显示背景22*80 ;dh中放行号 ;dl中放列号 bibi: push cx ;显示光标 ;第0页 int 10h ;在光标处显 ...

  2. IIs安装&发布&解决遇到的问题

    IIS安装: IIS发布: 1.添加网站: 2.将发布的文件,copy到该网站的目录下 3. 刷新,文件显示出来,将其"转换为应用程序” => 4.在应用程序池中找到该网站相应的程序池 ...

  3. javaIO-学习笔记

    package IOTest; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream ...

  4. 流编辑器-sed

    sed 参数: 1.'s' 替换 sed 's/search-word/replace-word/' file-name 替换file-name文件中的search-word为replace-word ...

  5. JS关于时间的计算

    用javascript返回两个时间差,精确到秒:     Date.diff = function(a,b){        if(a instanceof Date && b.ins ...

  6. Nginx-Lua模块的执行顺序

    一.nginx执行步骤 nginx在处理每一个用户请求时,都是按照若干个不同的阶段依次处理的,与配置文件上的顺序没有关系,详细内容可以阅读<深入理解nginx:模块开发与架构解析>这本书, ...

  7. linux下查看系统信息

    lspci:   这个工具用来查看所有连接到pci总线上的所有设备信息. 如果有些系统没有lspci,安装后即可使用,有些老死的版本中需要root权限才可使用. 一些用法: lspci    列出所有 ...

  8. SpringMVC 拦截器不拦截静态资源的三种处理方式

    SpringMVC提供<mvc:resources>来设置静态资源,但是增加该设置如果采用通配符的方式增加拦截器的话仍然会被拦截器拦截,可采用如下方案进行解决: 方案一.拦截器中增加针对静 ...

  9. Rails :.nil? , .empty?, .blank? .present? 的区别

    .nil? , .empty?, .blank? .present? 的区别 首先这三个都是判空的. 而 .nil? 和 .empty? 是ruby的方法. .blank? 是rails的方法 .ni ...

  10. 01 Linux档案与目录管理

      1. 目录与路径 1.1绝对路径和相对路径         绝对路径:一定有根目录/写起,例如:/usr/share/doc         相对路径:不是由根目录/写起,例如:由/usr/sha ...