簡體   English   中英

Openstack熱與Ansible。 虛擬機啟動和應用程序部署

[英]Openstack Heat & Ansible. VM spinup and App deployment

我正在使用openstack heat模板拆分新的VM,並獲取新拆分的VM的IP列表。 我正在使用Ansible腳本。

我可以從熱中獲取新的IP列表,並且可以使用with_items依次部署應用程序。

如何使用Ansible腳本並行進行部署,以使“ n”台服務器上的總部署時間與一台服務器上的總部署時間相同。

一種選擇是創建一個動態清單腳本 ,該腳本將從Heat中獲取實例ip,並將其提供給Ansible。 考慮如下的Heat模板:

heat_template_version: 2014-10-16

resources:

  nodes:
    type: OS::Heat::ResourceGroup
    properties:
      count: 3
      resource_def:
        type: node.yaml

outputs:

  nodes:
    value: {get_attr: [nodes, public_ip]}

這將定義三個nova實例,每個實例定義為:

heat_template_version: 2014-10-16

resources:

  node:
    type: OS::Nova::Server
    properties:
      image: rhel-atomic-20150615
      flavor: m1.small
      key_name: lars
      networks:
        - port: {get_resource: node_eth0}

  node_eth0:
    type: OS::Neutron::Port
    properties:
      network: net0
      security_groups:
        - default
      fixed_ips:
        - subnet: 82d04267-635f-4eec-8211-10e40fcecef0

  node_floating:
    type: OS::Neutron::FloatingIP
    properties:
      floating_network: public
      port_id: {get_resource: node_eth0}

outputs:

  public_ip:
    value: {get_attr: [node_floating, floating_ip_address]}

部署此堆棧后,我們可以獲得以下公共ip列表:

$ heat output-show mystack nodes
[
  "172.24.4.234", 
  "172.24.4.233", 
  "172.24.4.238"
]

我們可以編寫一個簡單的Python腳本來實現動態廣告資源接口:

#!/usr/bin/python

import os
import argparse
import json
import subprocess

def parse_args():
    p = argparse.ArgumentParser()
    p.add_argument('--list',
                   action='store_true')
    p.add_argument('--host')
    return p.parse_args()

def get_hosts():
    hosts = subprocess.check_output([
        'heat', 'output-show', 'foo', 'nodes'])

    hosts = json.loads(hosts)
    return hosts

def main():
    args = parse_args()

    hosts = get_hosts()

    if args.list:
        print json.dumps(dict(all=hosts))
    elif args.host:
        print json.dumps({})
    else:
        print 'Use --host or --list'
        print hosts

if __name__ == '__main__':
    main()

我們可以測試一下,看看它是否有效:

$ ansible all -i inventory.py -m ping
172.24.4.238 | success >> {
    "changed": false,
    "ping": "pong"
}

172.24.4.234 | success >> {
    "changed": false,
    "ping": "pong"
}

172.24.4.233 | success >> {
    "changed": false,
    "ping": "pong"
}

假設我們有以下Ansible劇本:

- hosts: all
  gather_facts: false
  tasks:
  - command: sleep 60

這將在每個主機上運行sleep 60命令。 並行運行內容大約需要一分鍾,而序列化則大約需要三分鍾。

測試一下:

$ time ansible-playbook -i inventory.py playbook.yaml
PLAY [all] ******************************************************************** 

TASK: [command sleep 60] ****************************************************** 
changed: [172.24.4.233]
changed: [172.24.4.234]
changed: [172.24.4.238]

PLAY RECAP ******************************************************************** 
172.24.4.233               : ok=1    changed=1    unreachable=0    failed=0   
172.24.4.234               : ok=1    changed=1    unreachable=0    failed=0   
172.24.4.238               : ok=1    changed=1    unreachable=0    failed=0   


real    1m5.141s
user    0m1.771s
sys 0m0.302s

如您所見,該命令正在所有三台主機上並行執行,這就是您要查找的行為(但是請注意此線程該線程描述了Ansible在不告訴您的情況下序列化事物的情況)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM