我正在使用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在不告诉您的情况下进行序列化的情况)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句