Ansible:AWS CLI 无法使用命令或 shell 模块

时间:2021-05-25 11:41:34

标签: amazon-web-services shell ansible

我正在尝试将文件从 S3 带到一个目录。我的 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 }}"

我得到的错误:

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

我尝试用 shell 模块替换 command,但这给了我:

"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

我也试过删除双引号(从 shell/command: "aws ..." 中)并使用折叠块标量(shell: > \n aws ... ),但没有运气。命令 aws s3 cp s3://ov90-golden/pgscripts.zip /ovi/scripts/ 手动工作正常。

编辑:

其他值得注意的失败尝试(错误:"/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 }}/"

- 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 }}/"

附带问题:在第二种情况下(使用折叠标量“>”),Ansible 会在原始命令后自动添加换行符。这是为什么?这是第二种情况的详细错误:

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

感谢任何帮助!

2 个答案:

答案 0 :(得分:0)

错误 "stderr": "/bin/sh: aws: command not found" 看起来像在主机上找不到 aws cli。我看到您正在使用 sudo,因此请通过在主机上手动尝试使用 sudo 执行命令来仔细检查。

答案 1 :(得分:0)

https://stackoverflow.com/a/56101308/8340742 --> 这个答案对我有用

修复 - 添加:

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

已解决,但我仍然不明白为什么我需要这个,因为 /usr/local/bin 已经在我的 PATH 中,而 /usr/local/bin/aws 只是该目录的一个子集。