1

When executing a playbook with the following command I get a failed with a Could not find or access '/opt/ansible-automation-platform-setup-bundle-2.4-1.4-x86_64/setup.sh' on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote_src option

ansible-playbook playbooks/install_aap_platform.yml -f 10 -vvv --private-key $ID_RSA -u ec2-user -i installer_node

This is the ansible playbook:

- name: install ansible
  hosts: installnode
  become: yes
  become_user: ansible
  gather_facts: yes
  tasks:
  - name: populate /etc/hosts file
    ansible.builtin.shell: |
      cat {{ lookup('env', 'ANSIBLE_INSTALLER_LOCATION') }}/inventory | grep ".med.ds.osd.mil*" | awk '{print $1}' > test.txt
      for i in $(cat test.txt); do
        echo $i
        if grep -q "$(echo "$i")" /etc/hosts ; then
          echo "hostname $host_name already in hosts file"
        else
          echo $(echo $i |  cut -d'.' -f1 | sed 's/ip-//' | sed 's/-/./g')" $i" >> /etc/hosts
        fi
      done
      cat /etc/hosts
    become: yes
    become_user: root
  - name: Run installer script
    become: yes
    become_user: ansible
    ansible.builtin.script:
      cmd: /opt/ansible-automation-platform-setup-bundle-2.4-1.4-x86_64/setup.sh
    environment:
      ANSIBLE_BECOME_METHOD: 'sudo'
      ANSIBLE_BECOME: True
      ANSIBLE_HOST_KEY_CHECKING: False
    register: out
    ignore_errors: yes
  - name: print output
    debug:
      msg: "{{ out.stdout }}"

populate /etc/hosts file works without any issue, it is the task Run installer script that fails.

The task fails because it is not able to reach the file /opt/ansible-automation-platform-setup-bundle-2.4-1.4-x86_64/setup.sh, but if I ssh into the host and paste /opt/ansible-automation-platform-setup-bundle-2.4-1.4-x86_64/setup.sh and press enter, it works right away.

The script does not seem to be the issue, the folder /opt is owned by root, but the folder ansible-automation-platform-setup-bundle-2.4-1.4-x86_64/ is owned by the user ansible. I know that the become_user works because I used it in a previous task to upload files to the host system without any issues.

1
  • You are able to run the script from the ansible user, right ? Commented Nov 28, 2023 at 18:15

1 Answer 1

1

Remember that the script module copies a local script to the remote machine. When you have:

    ansible.builtin.script:
      cmd: /opt/ansible-automation-platform-setup-bundle-2.4-1.4-x86_64/setup.sh

That means it's going to look for /opt/ansible-automation-platform-setup-bundle-2.4-1.4-x86_64/setup.sh on the local (control) host, not on the host that is the target of the play. If the script already exists in the remote location, you should be using the command module instead:

    ansible.builtin.command:
      cmd: /opt/ansible-automation-platform-setup-bundle-2.4-1.4-x86_64/setup.sh

Unrelated to your question, but in populate /etc/hosts file you don't need either cat or grep; you can write:

awk '/.med.ds.osd.mil/ {print $1}' {{ lookup('env', 'ANSIBLE_INSTALLER_LOCATION') }}/inventory > test.txt

...and honestly, you could probably replace the entire script with an Ansible loop and the lineinfile module.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you very much, that helped, but I had to scrap the installation of aap via ansible because of a "double" assume role that was not usable by ansible. I ended up using sshpass and run an ssh command to install aap. I will put the changes in the cat/grep command as per suggestion.Thank you !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.