使用 ansible shell 模块时如何使 aws 命令可用于 sh? [英] How to make the aws command available to sh when using the ansible shell module?
问题描述
我正在尝试使用 aws cli 运行以下任务,因为 aws_s3
模块会显示所有存储桶键.但是,我不断收到 aws: not found
错误.aws cli
已正确安装,因为从主机运行完全相同的命令,工作正常.
I'm trying to run the following task by using aws cli, because of the aws_s3
module flats out all the bucket keys. However, I keep getting aws: not found
error.
aws cli
is correctly installed because running the exact same command from the host, works fine.
我的任务:
- name: Try list
shell: aws s3 ls "{{ s3_bucket }}"
完整错误:
fatal: [cassandra-node-1]: FAILED! => {
"changed": true,
"cmd": "aws s3 ls \"cassandra-snapshotter-test2\"",
"delta": "0:00:00.002900",
"end": "2019-05-12 13:48:25.705324",
"invocation": {
"module_args": {
"_raw_params": "aws s3 ls \"cassandra-snapshotter-test2\"",
"_uses_shell": true,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"warn": true
}
},
"msg": "non-zero return code",
"rc": 127,
"start": "2019-05-12 13:48:25.702424",
"stderr": "/bin/sh: 1: aws: not found",
"stderr_lines": [
"/bin/sh: 1: aws: not found"
],
"stdout": "",
"stdout_lines": []
}
为什么我不能从 Ansible 任务运行 aws cli?
Why can't I run the aws cli from the Ansible task?
推荐答案
AWS 二进制文件不可用于此主机上的 sh 解释器.
The AWS binary is not available to the sh interpreter on this host.
从 shell 会话运行
From a shell session run
$ which aws
找到 awscli 所在的位置.
to find where the awscli is located.
确保此目录包含在 PATH 环境变量中
Make sure this directory is included in the PATH environment variable
$ echo $PATH
如果不是,您可以配置您的服务器以在打开外壳时包含它
If it is not you can either configure your server to include it when a shell is opened
# bash
$ echo 'export PATH=$PATH:/path/to/awscli/dir' >> ~/.bash_profile
# KSH/sh
$ echo 'export PATH=$PATH:/path/to/awscli/dir' >> ~/.profile
或者,您可以使用 ansible environment 属性来设置环境内存中特定游戏、任务等的变量.
Alternately you can use the ansible environment property to set environment variables in memory for a particular play, task, etc.
environment:
PATH: "{{ lookup('env', 'PATH) }}:/path/to/awscli/dir"
最后,为简洁起见,您可以使用 executable 键在 [defaults] 部分下.这将允许您将其从 sh 解释器更改为 bash 之类的其他内容.
Finally, for brevity, you can modify the shell ansible is using for the shell
module in your ansible.cfg using the executable key under the [defaults] section. This would allow you to change it from the sh interpreter to something else like bash.
这篇关于使用 ansible shell 模块时如何使 aws 命令可用于 sh?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!