如何在Shell命令中使用本地注册变量

时间:2019-03-28 19:57:14

标签: ansible

我有一个下面的任务,它注册了一个变量:

- name: Get the existing access keys for the functional backup ID
  shell: "aws iam list-access-keys --user-name {{backup_functional_id}}"
  environment:
    AWS_ACCESS_KEY_ID: '{{aws_access_key_id}}'
    AWS_SECRET_ACCESS_KEY: '{{aws_secret_access_key}}'
  register: existing_access_keys

我希望此注册变量中的某些值可在shell命令中使用。

我能够记录所需的值:

- name: Mark the old one as inactive for now
  debug:
    var: (existing_access_keys.stdout |from_json).AccessKeyMetadata[0].AccessKeyId

如何在shell中访问它:

- name: Get list of available access keys to make sure new one is generated
  shell: "aws iam update-access-key --access-key-id (existing_access_keys.stdout |from_json).AccessKeyMetadata[0].AccessKeyId --status Inactive"
  environment:
    AWS_ACCESS_KEY_ID: '{{aws_access_key_id}}'
    AWS_SECRET_ACCESS_KEY: '{{aws_secret_access_key}}'

我得到:

fatal: [localhost]: FAILED! => {"changed": true, "cmd": "aws iam update-access-key --access-key-id (existing_access_keys.stdout |from_json).AccessKeyMetadata[0].AccessKeyId --status Inactive", "delta": "0:00:00.003190", "end": "2019-03-28 12:53:42.283188", "msg": "non-zero return code", "rc": 2, "start": "2019-03-28 12:53:42.279998", "stderr": "/bin/sh: 1: Syntax error: \"(\" unexpected", "stderr_lines": ["/bin/sh: 1: Syntax error: \"(\" unexpected"], "stdout": "", "stdout_lines": []}

我尝试使用{{ }}效果不佳。

我尝试使用简单的调试进行调试:

- name: Set old_api_key
  set_fact:
    old_api_key: {{ (existing_access_keys.stdout |from_json).AccessKeyMetadata[0].AccessKeyId }}

- name: Print old_api_key
  debug:
    var: old_api_key

但是我收到一个新的错误[。尝试使用\来保留它,但这也不起作用。

违规行似乎是:

      set_fact:
        old_api_key: {{ (existing_access_keys.stdout |from_json).AccessKeyMetadata[0].AccessKeyId }}
                                                                                  ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes.  Always quote template expression brackets when they
start a value. For instance:

1 个答案:

答案 0 :(得分:1)

您需要将变量表达式括在{{}}中。

#!/usr/bin/env ansible-playbook
---
- hosts: localhost
  become: false
  gather_facts: false
  tasks:
  - vars:
      my_text: "World"
    shell: "echo 'Hello {{ my_text }}'"

$ ./playbook.yml -v
PLAY [localhost] *********************************************************************************************************************************************

TASK [shell] *************************************************************************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "echo 'Hello World'", "delta": "0:00:00.002978", "end": "2019-03-28 15:06:58.623747", "rc": 0, "start": "2019-03-28 15:06:58.620769", "stderr": "", "stderr_lines": [], "stdout": "Hello World", "stdout_lines": ["Hello World"]}

PLAY RECAP ***************************************************************************************************************************************************
localhost                  : ok=1    changed=1    unreachable=0    failed=0   

在您的情况下:

- name: Get list of available access keys to make sure new one is generated
  environment:
    AWS_ACCESS_KEY_ID: "{{ aws_access_key_id }}"
    AWS_SECRET_ACCESS_KEY: "{{ aws_secret_access_key }}"
  shell: "aws iam update-access-key 
          --access-key-id {{ (existing_access_keys.stdout | from_json).AccessKeyMetadata[0].AccessKeyId }} 
          --status Inactive"