从 Ansible 的输出中提取变量

Extract variable from output in Ansible

当我运行以下剧本时:

- name: Token
  hashivault_token_create:
    display_name: "{{ vm_name }}"
    policies: ["secret/{{ vm_name }}"]
    renewable: True
    token: "{{ root_token }}"
  register: "vault_token_admin"

- name: Print token
  debug:
    msg: "My token is: {{ vault_token_admin }}"

我得到以下输出:

ok: [localhost] => {
"msg": "ES: {'changed': True, 'rc': 0, 'token': {'request_id': '84645008-2d04-4f09-84aa-7a546ca29f73', 'lease_id': '', 'renewable': False, 'lease_duration': 0, 'data': None, 'wrap_info': None, 'warnings': ['Policy \"secret/test\" does not exist'], 'auth': {'client_token': 's.q3a4s4trStht30gJ12kDibjl', 'accessor': 'jqMmGMbMls9poRlx48nSruSO', 'policies': ['default', 'secret/test'], 'token_policies': ['default', 'secret/test'], 'metadata': None, 'lease_duration': 2764800, 'renewable': True, 'entity_id': '', 'token_type': 'service', 'orphan': False}}, 'failed': False}"}

我的objective是获取“client_token”。我怎样才能只得到“s.q3a4s4trStht30gJ12kDibjl”?

谢谢

试试这个

  - debug:
      var: vault_token_admin.ES.token.auth.client_token

应该给

  vault_token_admin.ES.token.auth.client_token: s.q3a4s4trStht30gJ12kDibjl

要查看结构,请使用过滤器 to_nice_yaml。例如

  - debug:
      msg: "My token is: {{ vault_token_admin|to_nice_yaml }}"

给予

  msg: |-
    My token is: ES:
        changed: true
        failed: false
        rc: 0
        token:
            auth:
                accessor: jqMmGMbMls9poRlx48nSruSO
                client_token: s.q3a4s4trStht30gJ12kDibjl
                entity_id: ''
                lease_duration: 2764800
                metadata: None
                orphan: false
                policies:
                - default
                - secret/test
                renewable: true
                token_policies:
                - default
                - secret/test
                token_type: service
            data: None
            lease_duration: 0
            lease_id: ''
            renewable: false
            request_id: 84645008-2d04-4f09-84aa-7a546ca29f73
            warnings:
            - Policy \"secret/test\" does not exist
            wrap_info: None