【问题标题】:Openstack Heat & Ansible. VM spinup and App deploymentOpenstack Heat 和 Ansible。虚拟机启动和应用部署
【发布时间】:2015-06-17 14:33:52
【问题描述】:

我正在使用 openstack heat 模板启动新的虚拟机,并获取新启动的虚拟机的 IP 列表。我也在使用 Ansible 脚本。

我能够从热中获取新的 IP 列表,并且能够使用 with_items 以顺序方式部署应用程序。

如何使用 Ansible 脚本进行并行部署,以使“n”台服务器上的总部署时间与一台服务器上的总部署时间相同。

【问题讨论】:

    标签: ansible openstack


    【解决方案1】:

    一种选择是创建一个dynamic inventory script,它将从 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 playbook:

    - 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
    

    如您所见,该命令在所有三个上并行执行 主机,这是您正在寻找的行为(但请注意 this thread, 它描述了 Ansible 将序列化事物的情况 不告诉你)。

    【讨论】:

    • 感谢您的建议。这是最简单和最好的方法之一。我们设法让它在没有 python 脚本的情况下工作。我们定义了角色并构建了编排,以便在新启动的虚拟机上并行部署多个包。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 2015-10-31
    • 2019-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-12
    • 1970-01-01
    • 2019-08-13
    相关资源
    最近更新 更多