2

我对使用 saltstack 相当陌生,并且正在尝试让 salt-cloud 在配置期间标记我的 EC2 实例。我认为这是需要在 cloud.profiles 中完成的事情。我一直在寻找在配置 EC2 实例时尝试为 EC2 实例创建标签的文档和具体示例。我发现创建实例后标记实例可以从命令行完成:
salt-cloud -a set_tags mymachine tag1=somestuff tag2='Other stuff' 但是我希望这些操作在创建实例时自动发生。

附带说明一下,我还没有发现 salt 文档是最有帮助的。如果有教程或演练可以帮助自己更加熟悉 saltstack,我将不胜感激。

谢谢,

4

2 回答 2

2

ec2.py 云模块在配置文件中查找“标签”。

以下示例摘自上述文档。

mysql_profile:
  provider: ec2
  size: 1024MB
  tags:
    tag1: somestuff
    tag2: "others stuff" 
  [...]
于 2014-01-13T21:55:39.473 回答
1

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)
于 2015-06-30T14:48:42.717 回答