使用Ansible创建IAM用户-获取CLI凭据

使用Ansible创建IAM用户-获取CLI凭据,ansible,Ansible,我正在使用iam模块创建用户名,并且正在使用access\u key\u state:create选项 但是,我希望我的playbook为每个用户输出访问密钥和秘密访问密钥 playbook.yml: --- - name: "Starting the tasks: Creates IAM Policy, group, Role and User" hosts: localhost connection: local gather_facts: False vars_files:

我正在使用
iam
模块创建用户名,并且正在使用
access\u key\u state:create
选项

但是,我希望我的playbook为每个用户输出访问密钥秘密访问密钥

playbook.yml:

---
- name: "Starting the tasks: Creates IAM Policy, group, Role and User"
  hosts: localhost
  connection: local
  gather_facts: False
  vars_files:
    - vars/aws-credentials.yml

  tasks:

  - include: tasks/create-user.yml
    tags: user

  - include: tasks/create-group.yml
    tags: group
任务/create-user.yml:

---
  # Create the IAM users with Console and API access
- name: Create new IAM users with API keys and console access
  iam:
    iam_type: user
    name: "{{ item }}"
    state: present
    password: "{{ lookup('password', 'passwordfile chars=ascii_letters') }}"
    access_key_state: create
    update_password: on_create
  no_log: true
  register: newusers
  loop:
    - johna
    - mariab
    - carlosc

- name: test
  debug:
    msg: "{{ credentials.results }}"
调试消息
“{{credentials.results}}”
为我提供了访问密钥,但没有提供秘密访问密钥:

{
            "ansible_loop_var": "item",
            "changed": true,
            "created_keys": [],
            "failed": false,
            "groups": null,
            "invocation": {
                "module_args": {
                    "access_key_ids": null,
                    "access_key_state": "create",
                    "aws_access_key": null,
                    "aws_secret_key": null,
                    "debug_botocore_endpoint_logs": false,
                    "ec2_url": null,
                    "groups": null,
                    "iam_type": "user",
                    "key_count": 1,
                    "name": "carol.v",
                    "new_name": null,
                    "new_path": null,
                    "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
                    "path": "/",
                    "profile": null,
                    "region": null,
                    "security_token": null,
                    "state": "present",
                    "trust_policy": null,
                    "trust_policy_filepath": null,
                    "update_password": "always",
                    "validate_certs": true
                }
            },
            "item": "carlosc",
            "keys": {
                "AK_________FV": "Active"
            },
            "user_meta": {
                "access_keys": [
                    {
                        "access_key_id": "AK_________FV",
                        "status": "Active"
                    }
                ]
            },
            "user_name": "carlosc"
        }
如何为每个用户获取秘密访问密钥


2020年5月9日更新:对于。

坏消息;他们似乎有意将
密码访问密钥
扔进垃圾桶:

似乎唯一的解决方法是在
iam:
中设置
key\u count:0
,然后使用
awscli
或自定义ansible模块来创建相同的
iam。创建访问键
调用并保留结果

-name:为{{item}创建访问密钥
命令:aws iam创建访问密钥--用户名{{item}
环境:
AWS_地区:“{{u地区在这里}”
AWS_ACCESS_KEY_ID:“{{不管你叫什么{u你的{u ACCESS_KEY}”
AWS_SECRET_ACCESS_KEY:“{您的AWS_SECRET_ACCESS_KEY_name_here}”
注册:用户密钥
有以下项目:
-约翰娜
-马里亚布
-卡洛斯

尽管您可能需要针对新问题进行归档,但您可以随意归档问题,因为iam.py不再存在于
devel
分支中您可以使用
community.aws.iam

-name:使用API密钥创建IAM用户
community.aws.iam:
iam_类型:用户
姓名:某个虚拟用户
国家:现在
访问密钥状态:创建
注册:新用户
-调试:
var:新用户
您可以在以下位置获取访问权限和密钥:

  • 新建用户。用户元。访问密钥[0]。访问密钥id
  • new\u user.user\u meta.access\u key[0]。secret\u access\u key

我已经将我的文件加载到Secrets Manager中,并最终使用lambda函数进行旋转。

太糟糕了!你能举一个如何使用Ansible实现这一点的例子吗?当然,我不认为描述如此不透明,但我更新了答案,以包含这样一个任务的框架