众所周知,亚马逊是目前最好的云服务,但也是最贵的,现在我们使用
首先应该先装好ansible,boto,
pip install ansible
pip install boto
#下载ec2.py和ec2.ini
wget https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/ec2.py
wget https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/ec2.ini
如果你是亚马逊中国的用户,需要修改ec2.ini的2个配置
regions = cn-north-1
regions_exclude =
改为以上配置 地区列表请参考 http://docs.aws.amazon.com/zh_cn/general/latest/gr/rande.html
其他地区什么也不用改,搞特殊啊
现在用我自己的账号测试下
这个是我的测试账号
生成一个访问密钥并下载
然后再ansible的机器上写入环境变量,就写这个就行了,不行搞什么boto.cfg,一堆莫名其妙的问题解决不了
export AWS_ACCESS_KEY_ID='AKIAJ4RDIXZHIQHSCGNA'
export AWS_SECRET_ACCESS_KEY='很长一串字符,你懂的,我不会告诉你,我没有那么傻'
准备发射了
root@ip-172-31-16-161:/etc/ansible# ls
ansible.cfg ec2.ini ec2.py hosts
root@ip-172-31-16-161:/etc/ansible# pwd
/etc/ansible
root@ip-172-31-16-161:/etc/ansible# chmod +x ec2.py
root@ip-172-31-16-161:/etc/ansible# ./ec2.py --list
{
"_meta": {
"hostvars": {
"52.79.136.10": {
"ansible_ssh_host": "52.79.136.10",
"ec2__in_monitoring_element": false,
"ec2_ami_launch_index": "0",
"ec2_architecture": "x86_64",
"ec2_client_token": "qmLXm1457920178723",
"ec2_dns_name": "ec2-52-79-136-10.ap-northeast-2.compute.amazonaws.com",
"ec2_ebs_optimized": false,
"ec2_eventsSet": "",
"ec2_group_name": "",
"ec2_hypervisor": "xen",
"ec2_id": "i-2bf1e88c",
"ec2_image_id": "ami-09dc1267",
"ec2_instance_profile": "",
"ec2_instance_type": "t2.micro",
"ec2_ip_address": "52.79.136.10",
"ec2_item": "",
"ec2_kernel": "",
"ec2_key_name": "myAWS",
"ec2_launch_time": "2016-03-16T09:16:43.000Z",
"ec2_monitored": false,
"ec2_monitoring": "",
"ec2_monitoring_state": "disabled",
"ec2_persistent": false,
"ec2_placement": "ap-northeast-2c",
"ec2_platform": "",
"ec2_previous_state": "",
"ec2_previous_state_code": 0,
"ec2_private_dns_name": "ip-172-31-16-161.ap-northeast-2.compute.internal",
"ec2_private_ip_address": "172.31.16.161",
"ec2_public_dns_name": "ec2-52-79-136-10.ap-northeast-2.compute.amazonaws.com",
"ec2_ramdisk": "",
"ec2_reason": "",
"ec2_region": "ap-northeast-2",
"ec2_requester_id": "",
"ec2_root_device_name": "/dev/sda1",
"ec2_root_device_type": "ebs",
"ec2_security_group_ids": "sg-d78627be",
"ec2_security_group_names": "firewall",
"ec2_sourceDestCheck": "true",
"ec2_spot_instance_request_id": "",
"ec2_state": "running",
"ec2_state_code": 16,
"ec2_state_reason": "",
"ec2_subnet_id": "subnet-2515156f",
"ec2_tag_Name": "DevOps",
"ec2_virtualization_type": "hvm",
"ec2_vpc_id": "vpc-d2894fbb"
}
}
},
"ami_09dc1267": [
"52.79.136.10"
],
"ap-northeast-2": [
"52.79.136.10"
],
"ap-northeast-2c": [
"52.79.136.10"
],
"ec2": [
"52.79.136.10"
],
"i-2bf1e88c": [
"52.79.136.10"
],
"key_myAWS": [
"52.79.136.10"
],
"security_group_firewall": [
"52.79.136.10"
],
"tag_Name_DevOps": [
"52.79.136.10"
],
"type_t2_micro": [
"52.79.136.10"
],
"vpc_id_vpc_d2894fbb": [
"52.79.136.10"
]
}
这些就是一些动态信息,当然我们还可以用代码来实现
In [2]: import boto.ec2
In [3]: conn = boto.ec2.connect_to_region('ap-northeast-2',aws_access_key_id='AKIAJ4RDIXZHIQHSCGNA',aws_secret_access_key='呵呵。。。。')
In [4]: r=conn.get_all_instances()
In [10]: for i in r:
print i.instances,i.region,i.id,i.item
....:
[Instance:i-2bf1e88c] RegionInfo:ap-northeast-2 r-488598ef
上面就取出了我们的的一些参数,是EC2的一些参数 可以对照上图
In [50]: type(r)
Out[50]: boto.resultset.ResultSet
In [51]: r
Out[51]: [Reservation:r-488598ef]
In [52]: type(r[0])
Out[52]: boto.ec2.instance.Reservation
In [53]: r[0]
Out[53]: Reservation:r-488598ef
In [54]: dir(r[0])
Out[54]:
['__class__',
'__delattr__',
'__dict__',
'__doc__',
'__format__',
'__getattribute__',
'__hash__',
'__init__',
'__module__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'__weakref__',
'connection',
'endElement',
'groups',
'id',
'instances',
'item',
'owner_id',
'region',
'startElement',
'stop_all']
In [55]: type(r[0].instances)
Out[55]: boto.resultset.ResultSet
In [56]: type(r[0].instances[0])
Out[56]: boto.ec2.instance.Instance
In [57]: dir(r[0].instances[0])
Out[57]:
['__class__',
'__delattr__',
'__dict__',
'__doc__',
'__format__',
'__getattribute__',
'__hash__',
'__init__',
'__module__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'__weakref__',
'_in_monitoring_element',
'_placement',
'_previous_state',
'_state',
'_update',
'add_tag',
'add_tags',
'ami_launch_index',
'architecture',
'block_device_mapping',
'client_token',
'confirm_product',
'connection',
'create_image',
'dns_name',
'ebs_optimized',
'endElement',
'eventsSet',
'get_attribute',
'get_console_output',
'group_name',
'groups',
'hypervisor',
'id',
'image_id',
'instance_profile',
'instance_type',
'interfaces',
'ip_address',
'item',
'kernel',
'key_name',
'launch_time',
'modify_attribute',
'monitor',
'monitored',
'monitoring',
'monitoring_state',
'persistent',
'placement',
'placement_group',
'placement_tenancy',
'platform',
'previous_state',
'previous_state_code',
'private_dns_name',
'private_ip_address',
'product_codes',
'public_dns_name',
'ramdisk',
'reason',
'reboot',
'region',
'remove_tag',
'remove_tags',
'requester_id',
'reset_attribute',
'root_device_name',
'root_device_type',
'sourceDestCheck',
'spot_instance_request_id',
'start',
'startElement',
'state',
'state_code',
'state_reason',
'stop',
'subnet_id',
'tags',
'terminate',
'unmonitor',
'update',
'use_ip',
'virtualization_type',
'vpc_id']
圆环套圆环。。。。。。。终于把皮给拨开了
In [58]: r[0].instances[0].state_code
Out[58]: 16
In [59]: r[0].instances[0].tags
Out[59]: {u'Name': u'DevOps'}
In [60]: r[0].instances[0].tags['name']
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-60-a036520982ad> in <module>()
----> 1 r[0].instances[0].tags['name']
KeyError: 'name'
In [61]: r[0].instances[0].tags['Name']
Out[61]: u'DevOps'
先写到这里,还有很多更有趣的事情,
来源:oschina
链接:https://my.oschina.net/u/1790313/blog/699248