【问题标题】:AWS API re-deployment using ansible使用 ansible 重新部署 AWS API
【发布时间】:2022-06-23 12:04:33
【问题描述】:

我的 AWS 账户中有一个现有的 API。现在我尝试在引入任何资源策略更改后使用 ansible 重新部署 api。 根据 AWS,我需要使用以下 CLI 命令重新部署 api:

- name: deploy API
 command: >
   aws apigateway update-stage --region us-east-1 \
       --rest-api-id <rest-api-id> \
       --stage-name 'stage'\
       --patch-operations op='replace',path='/deploymentId',value='<deployment-id>'

在上面,“deploymentId”与之前的部署在每次部署后都会有所不同,这就是为什么尝试将其创建为变量以便可以自动执行重新部署步骤的原因。 我可以使用以下 CLI 获取以前的部署信息:

- name: Get deployment information
  command: >
   aws apigateway get-deployments \
      --rest-api-id 123454ne \
      --region us-east-1
  register: deployment_info

输出如下所示:

deployment_info.stdout_lines:
  - '{'
  - '    "items": ['
  - '        {'
  - '            "id": "abcd",'
  - '            "createdDate": 1228509116'
  - '        }'
  - '    ]'
  - '}'

我使用 deployment_info.items.id 作为 deploymentId 并且无法完成这项工作。现在坚持使用 Ansible CLI 命令从输出中获取 id 并在部署命令中将此 id 用作 deploymentId。 如何在部署命令中将此 id 用于 deploymentId

【问题讨论】:

标签: json amazon-web-services ansible


【解决方案1】:

我创建了一个小型 ansible 模块,您可能会觉得它很有用

#!/usr/bin/python

# Creates a new deployment for an API GW stage
# See https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-deployments.html
# Based on https://github.com/ansible-collections/community.aws/blob/main/plugins/modules/aws_api_gateway.py

# TODO needed?
# from __future__ import absolute_import, division, print_function
# __metaclass__ = type

import json
import traceback

try:
  import botocore
except ImportError:
  pass  # Handled by AnsibleAWSModule

from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict

from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry


def main():
  argument_spec = dict(
    api_id=dict(type='str', required=True),
    stage=dict(type='str', required=True),
    deploy_desc=dict(type='str', required=False, default='')
  )

  module = AnsibleAWSModule(
    argument_spec=argument_spec,
    supports_check_mode=True
  )

  api_id = module.params.get('api_id')
  stage = module.params.get('stage')

  client = module.client('apigateway')

  # Update stage if not in check_mode
  deploy_response = None
  changed = False
  if not module.check_mode:
    try:
      deploy_response = create_deployment(client, api_id, **module.params)
      changed = True
    except (botocore.exceptions.ClientError, botocore.exceptions.EndpointConnectionError) as e:
      msg = "Updating api {0}, stage {1}".format(api_id, stage)
      module.fail_json_aws(e, msg)

  exit_args = {"changed": changed, "api_deployment_response": deploy_response}
  module.exit_json(**exit_args)


retry_params = {"retries": 10, "delay": 10, "catch_extra_error_codes": ['TooManyRequestsException']}

@AWSRetry.jittered_backoff(**retry_params)
def create_deployment(client, rest_api_id, **params):
  result = client.create_deployment(
    restApiId=rest_api_id,
    stageName=params.get('stage'),
    description=params.get('deploy_desc')
  )

  return result

if __name__ == '__main__':
  main()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多