amazon-web-services - 从ansible中注册的变量中检索键的值

标签 amazon-web-services ansible ansible-2.x

我正在编写各种剧本,用于在 AWS 中配置用户、组、策略等。

目前,我正在尝试编写一个任务,从给定的 AWS IAM 账户中删除所有访问 key 。要使用 ansible 中的 iam 模块正确执行此操作,您必须指定要禁用的 AWS 访问 key 。

此脚本还会预先创建一个用户(删除访问 key 是为了确保如果用户已经创建,则他们不会拥有之前留下的任何访问 key )。

用户创建的输出被注册到一个变量中,如下所示

- name: Create new user and add to IAM group for console
  vars:
    use_key: "{{ enable_access_keys }}"
  iam:
    iam_type: user
    name: "{{ item }}"
    state: present
    aws_access_key: "{{ account_vars.aws_access_key }}"
    aws_secret_key: "{{ account_vars.aws_secret_key }}"
    password: "{{ iam_password }}"
    update_password: on_create
  with_items:
    - "{{ iam_user_name_list }}"
  when: not use_key
  register: console_user

console_user 变量的输出是:

ok: [127.0.0.1] => {
    "changed": false,
    "msg": {
        "changed": false,
        "msg": "All items completed",
        "results": [
            {
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "_ansible_parsed": true,
                "changed": false,
                "groups": null,
                "invocation": {
                    "module_args": {
                        "access_key_ids": null,
                        "access_key_state": null,
                        "aws_access_key": "removed",
                        "aws_secret_key": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
                        "ec2_url": null,
                        "groups": null,
                        "iam_type": "user",
                        "key_count": 1,
                        "name": "other-guy",
                        "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": "on_create",
                        "validate_certs": true
                    }
                },
                "item": "other-guy",
                "keys": {
                    "Access key is here": "Active"
                },
                "user_name": "other-guy"
            }
        ]
    }
}

我的问题是,如何获取“keys”字典下提供的访问 key ?由于我正在寻找的是键,而不是值,所以我不确定如何获取该访问 key ,以便我可以在下一个任务中使用它来表示我想删除它。

预先感谢您的帮助。

最佳答案

每个字典都有.keys()方法。例如打印每个用户的 key :

- debug:
    msg: "User {{ item.user_name }} has keys {{ item.keys.keys() }}"
  with_items: "{{ console_user.results }}"

或者使用 JMESPath 迭代每个用户的每个 key :

- debug:
    msg: "{{ item }}"
  with_items: "{{ console_user.results | json_query('[].keys.keys(@)') }}"

关于amazon-web-services - 从ansible中注册的变量中检索键的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44788277/

相关文章:

amazon-web-services - 除了 ROWTIME 之外,AWS Kinesis Analytics 在其他字段上是否有水印和滑动窗口?

java - 如何设置从当前日期起 1 年内的 Amazon S3 存储桶预签名 URL 过期时间

amazon-web-services - 如何在 CloudWatch Insights 查询中获取额外的上下文行?

amazon-web-services - 云信息 : Autoscaling persist volume after instance stop and add tags

ansible - 用 ansible 替换字符串中的/by\

Ansible:如何使用 yaml 指定数组或列表元素事实?

Ansible:更改=0

docker - 如何使用docker_host_info过滤处于退出状态的docker容器?

Ansible 如何使用用户输入将内容复制到文件

amazon-ec2 - 我可以只使用 ssh key 文件在 ec2 上运行 ansible playbook 没有 aws key /对吗?