ansible playbook 示例

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
上一篇:16-支持向量机SVM


下一篇:Asp.net mvc 5 CRUD代码自动生成工具- vs.net 2013 Saffolding功能扩展