I am trying to create new IAM users with Console and CLI access via Ansible. Because of the limitation of access_key_state
, I cannot retrieve the Secret Access Key.
So instead, I am creating the API access and secret access keys via command using the AWS CLI.
# Creates the API Access and Secret keys
- name: create access key for {{ item }}
command: aws iam create-access-key --user-name {{ item }}
register: user_keys
with_items:
- "testuser"
- "testuser2"
However, every time that I run my playbook, it will try to create the keys. So I have to limit it to only run that task when there is no key already created for that specific user.
I've been trying to achieve that by doing:
- name: list access key for {{ item }}
command: aws iam list-access-keys --user-name {{ item }}
register: list_user_keys
with_items:
- "testuser1"
- "testuser2"
# Creates the API Access and Secret keys
- name: create access key for {{ item }}
command: aws iam create-access-key --user-name {{ item }}
register: user_keys
with_items:
- "testuser1"
- "testuser2"
when: list_user_keys.stdout = ""
Error:
TASK [create access key for {{ item }}] ****************************************
fatal: [localhost]: FAILED! => {"msg": "The conditional check 'list_user_keys.stdout = \"\"' failed. The error was: template error while templating string: expected token 'end of statement block', got '='. String: {% if list_user_keys.stdout = \"\" %} True {% else %} False {% endif %}\n\nThe error appears to be in '/tasks/create-user.yml': line 39, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n# Creates the API Access and Secret keys\n- name: create access key for {{ item }}\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with_items:\n - {{ foo }}\n\nShould be written as:\n\n with_items:\n - \"{{ foo }}\"\n"}
aws iam list-access-keys --user-name 'testuser1'
output:
{
"AccessKeyMetadata": []
}
What am I missing here?
Edit 1: Adding the {{ list_user_keys.results }}
output (Thanks @larsks):
ok: [localhost] => {
"msg": [
{
"ansible_loop_var": "item",
"changed": true,
"cmd": [
"aws",
"iam",
"list-access-keys",
"--user-name",
"testuser1"
],
"delta": "0:00:01.213801",
"end": "2020-05-08 12:58:36.338219",
"failed": false,
"invocation": {
"module_args": {
"_raw_params": "aws iam list-access-keys --user-name testuser1",
"_uses_shell": false,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": true
}
},
"item": "testuser1",
"rc": 0,
"start": "2020-05-08 12:58:35.124418",
"stderr": "",
"stderr_lines": [],
"stdout": "{\n \"AccessKeyMetadata\": []\n}",
"stdout_lines": [
"{",
" \"AccessKeyMetadata\": []",
"}"
]
},
So it looks like that somehow I need to get to the stdout_lines.AccessKeyMetadata
output.
Edit 2: Adding the output of the response from @miwa
ok: [localhost] => {
"res0": {
"AccessKeyMetadata": [
{
"AccessKeyId": "AK___O3",
"CreateDate": "2020-05-06T22:07:42Z",
"Status": "Active",
"UserName": "testuser1"
},
{
"AccessKeyId": "AK___GB",
"CreateDate": "2020-05-06T22:16:11Z",
"Status": "Active",
"UserName": "testuser1"
}
]
}
}
Edit 3: Still troubleshooting with @miwa
TASK [Optionally store results for further usage] ******************************
fatal: [localhost]: FAILED! => {"msg": "The conditional check 'list_user_keys.results[6].stdout != \"\"' failed. The error was: error while evaluating conditional (list_user_keys.results[6].stdout != \"\"): list object has no element 6\n\nThe error appears to be in 'tasks/create-user.yml': line 39, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Optionally store results for further usage\n ^ here\n"}
Also, note that the Debug shows me the following for testuser2
who doesn't have any Key created: "stdout": "{\n \"AccessKeyMetadata\": []\n}",
So you see that stdout
isn't empty, therefore list_user_keys.results[1].stdout != ""
won't work?