Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the access key with iam_module of Ansible?

I am using Ansible to create AWS users. One of the features of Ansible is to create a user with access key. I am wondering how could I get the access key after the user was successfully created.

http://docs.ansible.com/ansible/iam_module.html

tasks:
- name: Create two new IAM users with API keys
  iam:
    iam_type: user
    name: "{{ item }}"
    state: present
    password: "{{ temp_pass }}"
    access_key_state: create
  with_items:
    - user
like image 727
Istvan Avatar asked Apr 07 '16 13:04

Istvan


2 Answers

I tried in 2.0.1.0. Should work in 2.0.0.2.

  tasks:
  - iam:
      iam_type: user
      name: foo
      state: present
      access_key_state: create
    register: credentials
  - debug: var=credentials

Output

[debug] *******************************************************************
ok: [127.0.0.1] => {
    "credentials": {
        "changed": false,
        "groups": null,
        "keys": {
            "AKIAXXXXXXXXXXTTGFXX": "Active"
        },
        "user_name": "foo"
    }
}

It is not possible to get the secret as of Ansible 2.0.1.0. It is a bug. See iam module not very useful for managing access keys

like image 142
helloV Avatar answered Oct 26 '22 05:10

helloV


In the meantime (I am using Ansible 2.3.2.0) that issue was successfully fixed:

- name: Create restricted bot user to access S3
  iam:
    iam_type: user
    name: blubaa  
    state: present
    access_key_state: create
  connection: local
  register: credentials

- debug: var=credentials

Output:

ok: [XXXXXXXXXX] => {
    "credentials": {
        "changed": true, 
        "groups": null, 
        "keys": [
            {
                "access_key_id": "AKIAJXXXXXXXXXXZX6GQ", 
                "create_date": "2017-08-26T01:04:05Z", 
                "status": "Active", 
                "user_name": "blubaa"
            }
        ], 
        "user_meta": {
            "access_keys": [
                {
                    "access_key_id": "AKIAJXXXXXXXXXXZX6GQ", 
                    "access_key_selector": "XXXX", 
                    "create_date": "2017-08-26T01:04:05.720Z", 
                    "secret_access_key": "wPwd2H0XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXkHB08Elo", 
                    "status": "Active", 
                    "user_name": "blubaa"
                }
            ], 
            "created_user": {
                "arn": "arn:aws:iam::30XXXXXXXXXX:user/blubaa", 
                "create_date": "2017-08-26T01:04:05.557Z", 
                "path": "/", 
                "user_id": "AIDAXXXXXXXXXXOYT7M", 
                "user_name": "blubaa"
            }, 
            "password": null
        }
    }
}
like image 20
SoJeN Avatar answered Oct 26 '22 04:10

SoJeN