简体   繁体   English

Ansible:AWS CLI 不适用于命令或 shell 模块

[英]Ansible: AWS CLI not working with command or shell module

I'm trying to bring files from S3 to a directory.我正在尝试将文件从 S3 带到一个目录。 My Ansible code block (erring at the first task):我的 Ansible 代码块(第一个任务出错):

- name: Transfer scripts to {{ instance_directory_vars.scripts_ovi }} from s3 bucket
  block:         
    - name: Transfer from s3 to local
      shell: "aws s3 cp {{ pg_scripts.s3_loc }}/{{ pg_scripts.fname }} {{ instance_directory_vars.scripts_ovi }}/"
      vars:
        ansible_python_interpreter: /usr/bin/python3
    - name: Unzip the transferred file
      shell: "unzip {{ instance_directory_vars.scripts_ovi }}/{{ pg_scripts.fname }} -d {{ instance_directory_vars.scripts_ovi }}/"
  become: true
  become_method: sudo
  become_user: "{{ admin_group_name }}"

The error I'm getting:我得到的错误:

fatal: [10.0.7.9]: FAILED! => {
    "changed": true,
    "cmd": "aws s3 cp s3://ov90-golden/pgscripts.zip /ovi/scripts/",
    "delta": "0:00:00.002329",
    "end": "2021-05-25 11:27:18.390103",
    "invocation": {
        "module_args": {
            "_raw_params": "aws s3 cp s3://ov90-golden/pgscripts.zip /ovi/scripts/",
            "_uses_shell": true,
            "argv": null,
            "chdir": null,
            "creates": null,
            "executable": null,
            "removes": null,
            "stdin": null,
            "stdin_add_newline": true,
            "strip_empty_ends": true,
            "warn": true
        }
    },
    "msg": "non-zero return code",
    "rc": 127,
    "start": "2021-05-25 11:27:18.387774",
    "stderr": "/bin/sh: aws: command not found",
    "stderr_lines": [
        "/bin/sh: aws: command not found"
    ],
    "stdout": "",
    "stdout_lines": []
}

I've tried replacing shell with the command module, but that gives me:我试过用command模块替换shell ,但这给了我:

"cmd": "aws s3 cp s3://ov90-golden/pgscripts.zip /ovi/scripts/",
"invocation": {
    "module_args": {
        "_raw_params": "aws s3 cp s3://ov90-golden/pgscripts.zip /ovi/scripts/",
        "_uses_shell": false,
        "argv": null,
        "chdir": null,
        "creates": null,
        "executable": null,
        "removes": null,
        "stdin": null,
        "stdin_add_newline": true,
        "strip_empty_ends": true,
        "warn": true
    }
},
"msg": "[Errno 2] No such file or directory: b'aws': b'aws'",
"rc": 2

I have also tried removing the double quotes (from shell/command: "aws..." ) and using the folded block scalar ( shell: > \n aws... ) without luck.我还尝试删除双引号(来自shell/command: "aws..." )并使用折叠块标量( shell: > \n aws... ),但没有运气。 The command aws s3 cp s3://ov90-golden/pgscripts.zip /ovi/scripts/ works fine manually.命令aws s3 cp s3://ov90-golden/pgscripts.zip /ovi/scripts/手动工作正常。

EDITS:编辑:

Other notable attempts that failed (Error: "/bin/sh: aws: command not found" in both):其他值得注意的失败尝试(错误: "/bin/sh: aws: command not found"在两者中):

- name: Transfer from s3 to local
  shell: 
    cmd: "aws s3 cp {{ pg_scripts.s3_loc }}/{{ pg_scripts.fname }} {{ instance_directory_vars.scripts_ovi }}/"
    chdir: "/home/{{ admin_group_name }}/"

And

- name: Transfer from s3 to local
  shell: 
    cmd: >
     aws s3 cp {{ pg_scripts.s3_loc }}/{{ pg_scripts.fname }} {{ instance_directory_vars.scripts_ovi }}/
    chdir: "/home/{{ admin_group_name }}/"

Side question: In the second case (with folded scalar '>'), Ansible adds a newline automatically after the raw command.附带问题:在第二种情况下(使用折叠标量“>”),Ansible 在原始命令之后自动添加换行符。 Why is that?这是为什么? Here's the detailed error for the second case:这是第二种情况的详细错误:

"invocation": {
    "module_args": {
        "_raw_params": "aws s3 cp s3://ov90-golden/pgscripts.zip /ovi/scripts/\n",
        "_uses_shell": true,
        "argv": null,
        "chdir": "/home/ovadmin/",
        ...
    }

Appreciate any help!感谢任何帮助!

The error "stderr": "/bin/sh: aws: command not found" looks like aws cli is not found on the host.错误"stderr": "/bin/sh: aws: command not found"看起来好像在主机上找不到 aws cli。 I see you are using sudo so double check by manually trying the command with sudo on the host.我看到您正在使用 sudo,因此通过在主机上手动尝试使用 sudo 命令进行仔细检查。

https://stackoverflow.com/a/56101308/8340742 --> This Answer worked for me https://stackoverflow.com/a/56101308/8340742 --> 这个答案对我有用

Fix - Added:修复 - 添加:

  environment:
    PATH: "{{ lookup('env', 'PATH') }}:/usr/local/bin/aws"

Solved, but I still don't understand why I needed this considering /usr/local/bin was already in my PATH and /usr/local/bin/aws is just a subset of that directory.解决了,但我仍然不明白为什么我需要这个考虑到/usr/local/bin已经在我的 PATH 中,而/usr/local/bin/aws只是该目录的一个子集。

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

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