简体   繁体   English

如何在 Ansible Tower 中运行 AWS CLI 命令任务

[英]How to run AWS CLI command tasks in Ansible Tower

The AWS CLI command tasks in Ansible playbooks work fine form command line if AWS credentials are specified as environment variables as per boto requirements.如果根据 boto 要求将 AWS 凭证指定为环境变量,Ansible playbooks 中的 AWS CLI 命令任务可以在命令行中正常工作。 More info can be found here Environment Variables .更多信息可以在这里找到环境变量 But they fail to run in Tower because it exports another set of env.但是它们无法在 Tower 中运行,因为它导出了另一组环境。 vars:变量:

AWS_ACCESS_KEY
AWS_SECRET_KEY

In order to make them work in Tower just add the below in task definition:为了使它们在 Tower 中工作,只需在任务定义中添加以下内容:

environment:
  AWS_ACCESS_KEY_ID: "{{ lookup('env','AWS_ACCESS_KEY') }}"
  AWS_SECRET_ACCESS_KEY: "{{ lookup('env','AWS_SECRET_KEY') }}"

eg this task:例如这个任务:

- name: Describe instances
  command: aws ec2 describe-instances --region us-east-1

will transform to:将转变为:

- name: Describe instances
  command: aws ec2 describe-instances --region us-east-1
  environment:
    AWS_ACCESS_KEY_ID: "{{ lookup('env','AWS_ACCESS_KEY') }}"
    AWS_SECRET_ACCESS_KEY: "{{ lookup('env','AWS_SECRET_KEY') }}"

NOTE: This only injects env.var.注意:这只会注入 env.var。 for the specific task - not the whole playbook!针对特定任务 - 不是整个剧本! So you have to amend this way every AWS CLI task.因此,您必须以这种方式修改每个 AWS CLI 任务。

Put your environment variable in a file:将您的环境变量放在一个文件中:

export AWS_ACCESS_KEY=
export AWS_SECRET_KEY=

save the file in ~/.vars in the remote host and then in your playbook.将文件保存在远程主机的 ~/.vars 中,然后保存在您的剧本中。

- name: Describe instances
  command: source ~/.vars && aws ec2 describe-instances --region us-east-2

for security you could delete the file after run and copy again in the next play.为了安全起见,您可以在运行后删除文件并在下一次播放时再次复制。

While this may not be applicable to tower we use the opensource version.虽然这可能不适用于 Tower,但我们使用开源版本。 Setup your .aws and/or .boto files.设置您的 .aws 和/或 .boto 文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM