【问题标题】:How to make the aws command available to sh when using the ansible shell module?使用 ansible shell 模块时如何使 aws 命令对 sh 可用?
【发布时间】:2019-09-29 16:20:10
【问题描述】:

我正在尝试使用 aws cli 运行以下任务,因为 aws_s3 模块将所有存储桶键都展平。但是,我不断收到aws: not found 错误。
aws cli 已正确安装,因为从主机运行完全相同的命令,工作正常。

我的任务:

- 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?

【问题讨论】:

  • 很难确定问题的根源,但到目前为止,我想到了两个可能的问题:1. 这个任务是在 hosts: localhost 上执行的吗(因为我假设你有本地安装的 aws cli)。 2. aws cli 是在系统范围内安装的,还是仅为您的用户安装(并添加到 $PATH),并且由于某种原因,ansible 以不同的用户身份启动并清除了环境变量。
  • 另外,虽然这不是您所要求的,但执行您想做的事情的“可靠方式”是通过 aws_s3: mode=list bucket={{s3_bucket}}
  • @MatthewLDaniel 我在帖子中提到,使用aws_s3 对我不起作用,因为这让我所有的键都是平的,而shell: aws s3 ls 只提供顶级文件夹。
  • @PiotrWicijowski 我没有在本地主机上运行。 aws cli 在 PATH 上,但显然这是一个问题:github.com/ansible/ansible/issues/25147

标签: amazon-web-services ansible sh aws-cli


【解决方案1】:

此主机上的 sh 解释器无法使用 AWS 二进制文件。

从 shell 会话运行

$ which aws

查找 awscli 所在的位置。

确保此目录包含在 PATH 环境变量中

$ echo $PATH

如果不是,您可以将服务器配置为在打开 shell 时包含它

# bash
$ echo 'export PATH=$PATH:/path/to/awscli/dir'  >> ~/.bash_profile

# KSH/sh
$ echo 'export PATH=$PATH:/path/to/awscli/dir'  >> ~/.profile

或者,您可以使用 ansible environment 属性在内存中为特定的游戏、任务等设置环境变量。

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

最后,为简洁起见,您可以使用 [defaults] 部分下的 executable 键修改 ansible 用于您的 ansible.cfg 中的 shell 模块的 shell。这将允许您将其从 sh 解释器更改为 bash 之类的其他东西。

【讨论】:

  • 奇怪的是,即使我可以使用带有 args 的 bash shell 时,我也看到了这一点:可执行文件:/bin/bash 但如果我 ssh 进入 aws 实例,则 AWS 可执行文件可用。我认为这是因为由于某种原因 /usr/local/sbin 在运行 ansible bash shell 时不在路径中。
猜你喜欢
  • 2020-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多