AWX 模板未获取 AWS 凭证

AWX 模板未获取 AWS 凭证

我有一个新的 Ansible AWX 服务器正在运行,我想用它做的事情之一是创建 AWS 实例的 AMI,然后为这些实例安装常规更新。

在安装更新方面,我的剧本运行良好,但我无法让 AWX UI 通过模板正确传递 AWS 凭证。

我正在运行一个仅执行 AMI 创建的测试模板,但始终失败并显示一条有关未找到凭证的消息。

AWX 有一个凭证部分。我添加了具有正确权限的 IAM 用户的访问密钥和密钥。尽管将该凭证添加到作业模板,但我得到的输出如下:

{
    "_ansible_parsed": true,
    "exception": "Traceback (most recent call last):\n  File \"/tmp/ansible_fj8G6M/ansible_module_ec2_ami.py\", line 433, in create_image\n    image_id = connection.create_image(**params).get('ImageId')\n  File \"/usr/lib/python2.7/site-packages/botocore/client.py\", line 320, in _api_call\n    return self._make_api_call(operation_name, kwargs)\n  File \"/usr/lib/python2.7/site-packages/botocore/client.py\", line 610, in _make_api_call\n    operation_model, request_dict)\n  File \"/usr/lib/python2.7/site-packages/botocore/endpoint.py\", line 102, in make_request\n    return self._send_request(request_dict, operation_model)\n  File \"/usr/lib/python2.7/site-packages/botocore/endpoint.py\", line 132, in _send_request\n    request = self.create_request(request_dict, operation_model)\n  File \"/usr/lib/python2.7/site-packages/botocore/endpoint.py\", line 116, in create_request\n    operation_name=operation_model.name)\n  File \"/usr/lib/python2.7/site-packages/botocore/hooks.py\", line 356, in emit\n    return self._emitter.emit(aliased_event_name, **kwargs)\n  File \"/usr/lib/python2.7/site-packages/botocore/hooks.py\", line 228, in emit\n    return self._emit(event_name, kwargs)\n  File \"/usr/lib/python2.7/site-packages/botocore/hooks.py\", line 211, in _emit\n    response = handler(**kwargs)\n  File \"/usr/lib/python2.7/site-packages/botocore/signers.py\", line 90, in handler\n    return self.sign(operation_name, request)\n  File \"/usr/lib/python2.7/site-packages/botocore/signers.py\", line 157, in sign\n    auth.add_auth(request)\n  File \"/usr/lib/python2.7/site-packages/botocore/auth.py\", line 356, in add_auth\n    raise NoCredentialsError\nNoCredentialsError: Unable to locate credentials\n",
    "_ansible_no_log": false,
    "botocore_version": "1.12.16",
    "changed": false,
    "invocation": {
        "module_args": {
            "enhanced_networking": null,
            "purge_tags": false,
            "launch_permissions": null,
            "ramdisk_id": null,
            "no_reboot": false,
            "ec2_url": null,
            "aws_secret_key": null,
            "billing_products": null,
            "state": "present",
            "virtualization_type": "hvm",
            "sriov_net_support": null,
            "architecture": "x86_64",
            "profile": null,
            "image_location": null,
            "description": "",
            "tags": {
                "Name": "i-sdfsdf987987"
            },
            "kernel_id": null,
            "image_id": null,
            "wait_timeout": 900,
            "wait": false,
            "aws_access_key": null,
            "name": "i-sdfsdf987987",
            "security_token": null,
            "delete_snapshot": false,
            "region": "eu-west-2",
            "instance_id": "i-sdfsdf987987",
            "root_device_name": null,
            "validate_certs": true,
            "device_mapping": null
        }
    },
    "msg": "Error registering image: Unable to locate credentials",
    "boto3_version": "1.9.16"

这是我的剧本:

---
- hosts: all
  remote_user: "{{ remote_user }}"
  become: yes
  tasks:
    - name: create an ami
      ec2_ami:
        instance_id: "{{ instance_id }}"
        name: "{{ instance_id }}"
        region: "{{ aws_region }}"
        tags:
          Name: "{{ instance_id }}"

如果我在 AWX UI 中将凭据手动添加为主机上的变量,则添加以下行:

aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"

那么一切都很好。但是这似乎忽略了使用 AWX 的凭证存储支持的明显优势。

环境详情:

  • ansible 2.6.4
  • awx 版本 1.0.7.2
  • Ubuntu 18.04.1

有人能看到我做错什么了吗?

答案1

好的,所以最后我在每台机器上配置了 AWS 凭证,并按照以下方式完成。似乎应该可以按照我最初尝试的方式完成,但我需要这样做。

在目标机器上,确保已awscli tools安装,然后运行aws configure以添加凭据、默认区域以及(如果您喜欢)默认输出选项。

您还需要确保pipbotobotocore已安装。

然后我通过剧本进行了如下更新:

---
- hosts: all
  remote_user: "{{ remote_user }}"
  tasks:
    - name: create an ami
      ec2_ami:
        instance_id: "{{ instance_id }}"
        name: "{{ instance_id }}-{{ ansible_date_time.iso8601_basic }}"
        tags:
          Name: "{{ instance_id }}-{{ ansible_date_time.iso8601_basic }}"

如您所见,我删除了region每台机器上指定的 AMI,并调整了 AMI 的命名方式。我还删除了becomeAWS 配置不是在 root 用户下完成的,因此这也会导致失败。

相关内容