ansible

Cannot get ansible.builtin.script to find the script


I am trying to run a script as part of a role. The script is located within the role folder, but ansible complains that it cannot find it when I try to use the role.

This is my folder setup:

my-project/
  - ansible/
    - roles/
      -install-docker/
        - files/
          - install-docker.sh
        - meta/
          - main.yml
        - tasks/
          - main.yml
  - gate
    - ansible
      - docker-setup.yml
  - ansible.cfg

This is the role 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

And the docker-setup.yml playbook in gate is trying to use it:

---
- name: Install docker
  hosts: some-host
  vars:
    root_dir: /home/foo

  roles:
      - install-docker

Here is my ansible.cfg

[defaults]
INVENTORY = local_vagrant
roles_path = ./ansible/roles

[ssh_connection]
pipelining = true

This is how I run the playbook and the error output:

(.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

What am I doing wrong?


Solution

  • That error message is not very helpful. The cause was an error in the shebang and fixing it resolves it:

    install-docker.sh

    -#!bin/bash
    +#!/bin/bash