![](/img/trans.png)
[英]How to make the aws command available to sh when using the ansible shell module?
[英]Ansible: AWS CLI not working with command or shell module
我正在嘗試將文件從 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": []
}
我試過用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
我還嘗試刪除雙引號(來自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/",
...
}
感謝任何幫助!
錯誤"stderr": "/bin/sh: aws: command not found"
看起來好像在主機上找不到 aws cli。 我看到您正在使用 sudo,因此通過在主機上手動嘗試使用 sudo 命令進行仔細檢查。
https://stackoverflow.com/a/56101308/8340742 --> 這個答案對我有用
修復 - 添加:
environment:
PATH: "{{ lookup('env', 'PATH') }}:/usr/local/bin/aws"
解決了,但我仍然不明白為什么我需要這個考慮到/usr/local/bin
已經在我的 PATH 中,而/usr/local/bin/aws
只是該目錄的一個子集。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.