简体   繁体   English

使用ansible shell模块时,如何使aws命令可用于sh?

[英]How to make the aws command available to sh when using the ansible shell module?

I'm trying to run the following task by using aws cli, because of the aws_s3 module flats out all the bucket keys. 我正在尝试使用aws cli运行以下任务,因为aws_s3模块将所有存储桶键放平。 However, I keep getting aws: not found error. 但是,我不断收到aws: not found错误。
aws cli is correctly installed because running the exact same command from the host, works fine. 正确安装了aws cli因为从主机运行完全相同的命令可以正常工作。

My task: 我的任务:

- name: Try list
  shell: aws s3 ls "{{ s3_bucket }}"

The full error: 完整错误:

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": []
}

Why can't I run the aws cli from the Ansible task? 为什么我不能从Ansible任务运行aws cli?

The AWS binary is not available to the sh interpreter on this host. AWS二进制文件不适用于此主机上的sh解释器。

From a shell session run 从Shell会话运行

$ which aws

to find where the awscli is located. 查找awscli的位置。

Make sure this directory is included in the PATH environment variable 确保此目录包含在PATH环境变量中

$ 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

Alternately you can use the ansible environment property to set environment variables in memory for a particular play, task, etc. 或者,您可以使用ansible 环境属性为特定的播放,任务等在内存中设置环境变量。

environment:
  PATH: "{{ lookup('env', 'PATH) }}:/path/to/awscli/dir"

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. 最后,为简便起见,您可以使用[defaults]部分下的可执行密钥在ansible.cfg中修改用于ansible的shell This would allow you to change it from the sh interpreter to something else like bash. 这样您就可以将其从sh解释器更改为bash之类的东西。

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

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