ansible playbook 示例

本文介绍了一个使用Ansible进行自动化部署的实例,包括配置管理、模板渲染和任务执行等核心功能。通过定义主机清单、变量文件及Playbook,实现了远程服务器上的hosts文件更新和频道列表的动态配置。

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
posted on 2016-12-14 18:16 北京涛子 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/liujitao79/p/6180399.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值