我正在尝试将脚本作为角色的一部分运行。该脚本位于角色文件夹内,但 ansible 抱怨当我尝试使用该角色时找不到它。
这是我的文件夹设置:
my-project/
- ansible/
- roles/
-install-docker/
- files/
- install-docker.sh
- meta/
- main.yml
- tasks/
- main.yml
- gate
- ansible
- docker-setup.yml
- ansible.cfg
这就是角色
tasks/main.yml
:
---
- name: Install docker and docker compose
ansible.builtin.script: "{{ role_path }}/files/install-docker.sh"
args:
creates: /etc/apt/keyrings/docker.asc
docker-setup.yml
中的gate
剧本正在尝试使用它:
---
- name: Install docker
hosts: some-host
vars:
root_dir: /home/foo
roles:
- install-docker
这是我的
ansible.cfg
[defaults]
INVENTORY = local_vagrant
roles_path = ./ansible/roles
[ssh_connection]
pipelining = true
这就是我运行剧本和错误输出的方式:
(.venv) foo@foo:~$ cd my-project
(.venv) foo@foo:~/my-project$ ansible-playbook gate/ansible/docker-setup.yml -i production
PLAY [Install docker] ************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************************************************************************************************************************
[WARNING]: Platform linux on host some-host is using the discovered Python interpreter at /usr/bin/python3.12, but future installation of another Python interpreter could change the meaning of that path. See
https://docs.ansible.com/ansible-core/2.17/reference_appendices/interpreter_discovery.html for more information.
ok: [some-host]
TASK [install-docker : Install docker and docker compose] ************************************************************************************************************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: NoneType: None
fatal: [some-host]: FAILED! => {"changed": true, "msg": "non-zero return code", "rc": 127, "stderr": "Shared connection to xxx.xxx.xxx.xxx closed.\r\n", "stderr_lines": ["Shared connection to xxx.xxx.xxx.xxx closed."], "stdout": "/bin/sh: 1: /home/foo/.ansible/tmp/ansible-tmp-1718466187.643503-255075-226226739385306/install-docker.sh: not found\r\n", "stdout_lines": ["/bin/sh: 1: /home/foo/.ansible/tmp/ansible-tmp-1718466187.643503-255075-226226739385306/install-docker.sh: not found"]}
PLAY RECAP ***********************************************************************************************************************************************************************************************************************************
some-host : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
我做错了什么?
该错误消息不是很有帮助。原因是 shebang 中出现错误,修复它即可解决问题:
install-docker.sh
-#!bin/bash
+#!/bin/bash