简体   繁体   中英

Ansible: AWS CLI not working with command or shell module

I'm trying to bring files from S3 to a directory. My Ansible code block (erring at the first task):

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

"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. The command aws s3 cp s3://ov90-golden/pgscripts.zip /ovi/scripts/ works fine manually.

EDITS:

Other notable attempts that failed (Error: "/bin/sh: aws: command not found" in both):

- 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. 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. I see you are using sudo so double check by manually trying the command with sudo on the host.

https://stackoverflow.com/a/56101308/8340742 --> This Answer worked for me

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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