简体   繁体   English

Openstack热与Ansible。 虚拟机启动和应用程序部署

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

I am spinning up new VM's using openstack heat template and get the IP list of the newly spun up VM's. 我正在使用openstack heat模板拆分新的VM,并获取新拆分的VM的IP列表。 I am using Ansible scripts for the same. 我正在使用Ansible脚本。

I am able to get the new list of IP's from the heat and I am able to deploy an app using with_items in a sequential manner. 我可以从热中获取新的IP列表,并且可以使用with_items依次部署应用程序。

How can I do the deployments in parallel using Ansible scripts so that the total deployment time on "n" servers is same as that of One server. 如何使用Ansible脚本并行进行部署,以使“ n”台服务器上的总部署时间与一台服务器上的总部署时间相同。

One options is to create a dynamic inventory script that will fetch the instance ips from Heat and make them available to Ansible. 一种选择是创建一个动态清单脚本 ,该脚本将从Heat中获取实例ip,并将其提供给Ansible。 Consider a Heat template that looks like this: 考虑如下的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]}

This will define three nova instances, where each instance is defined as: 这将定义三个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]}

After deploying this stack, we can get a list of public ips like this: 部署此堆栈后,我们可以获得以下公共ip列表:

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

We can write a simple Python script to implement the dynamic inventory interface: 我们可以编写一个简单的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()

We can test that out to see that it works: 我们可以测试一下,看看它是否有效:

$ 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"
}

Assume that we have the following Ansible playbook: 假设我们有以下Ansible剧本:

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

This will run the sleep 60 command on each host. 这将在每个主机上运行sleep 60命令。 That should take around one minute to run things in parallel, and around three minutes if things are serialized. 并行运行内容大约需要一分钟,而序列化则大约需要三分钟。

Testing things out: 测试一下:

$ 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

As you can see, the command is executing in parallel on all three hosts, which is the behavior you were looking for (but be aware of this thread , which describes situations in which Ansible will serialize things without telling you). 如您所见,该命令正在所有三台主机上并行执行,这就是您要查找的行为(但是请注意此线程该线程描述了Ansible在不告诉您的情况下序列化事物的情况)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM