salt.states.cloud 文档讨论了使用“cloud.tagged”盐状态,但它似乎没有被实现。
http://docs.saltstack.com/en/latest/ref/states/all/salt.states.cloud.html#using-states-instead-of-maps-to-deploy-clouds
创建实例时,您似乎可以像这样使用 tag 属性:
my-server-name:
cloud.present:
- name: 'my-server-name'
#...other properties
- tag:
'Env': 'auto-test'
这会在创建时应用标签,但如果实例已经存在,则不会更新它们。另外,我不知道如何在 cloud.present 中标记 EBS 卷。
您可以使用 Python boto 库重新标记 SaltStack 创建的实例并标记 EBS 卷。下面的示例代码 - 适用于实例和 EBS 卷。
def find_instance(instanceName, region):
boto_ec2 = boto.ec2.connect_to_region(region)
instances = boto_ec2.get_only_instances()
for instance in instances:
if instance.tags.get("Name", None) == instanceName:
return instance
return None
def ensure_instance_tags(instance, region, tags):
newTags = {}
for tagName in tags:
if instance.tags.get(tagName, None) != tags[tagName]:
newTags[tagName] = tags[tagName]
if bool(newTags):
sys.stdout.write("Updating tags for instance " + instance.id + "\n")
boto_ec2 = boto.ec2.connect_to_region(region)
boto_ec2.create_tags(instance.id, newTags)