1

I am trying execute an ansible playbook inside a Flask Python project using ansible runner but upon execution, I get the following error: The command was not found or was not executable: ansible-playbook.

The app runs in a docker container inside directory /app.

Code:

        r = ansible_runner.run(private_data_dir='/app/flask/ansible', playbook='project/playbook.yml')
        app.logger.info("{}: {}".format(r.status, r.rc))
        # successful: 0
        for each_host_event in r.events:
            app.logger.info(each_host_event['event'])
        app.logger.info("Final status:")
        app.logger.info(r.stats)

This is the project tree:

.
├── README.md
├── ansible.cfg
├── docker-compose.yml
├── flask
│   ├── Dockerfile
│   ├── ansible
│   │   ├── env
│   │   │   ├── cmdline
│   │   │   ├── envvars
│   │   │   ├── extravars
│   │   │   ├── passwords
│   │   │   ├── settings
│   │   │   └── ssh-key
│   │   ├── inventory
│   │   │   └── hosts
│   │   └── project
│   │       └── playbook.yml
│   ├── app.ini
│   ├── main.py
│   ├── run.py
│   ├── static
│   │   ├── app.js
│   │   ├── bulma.min.css
│   │   ├── highlight.min.css
│   │   ├── highlight.min.js
│   │   └── styles.css
│   └── templates
│       ├── 404.html
│       ├── base.html
│       ├── create_user.html
│       └── login.html
├── nginx
│   ├── Dockerfile
│   └── nginx.conf

Flask DockerFile:

FROM python:3.7.2-stretch
WORKDIR /app
ADD . /app
RUN pip install --upgrade pip && pip install flask uwsgi requests ansible_runner
CMD ["uwsgi","app.ini"]
Omar Abdelrazik
  • 683
  • 2
  • 9
  • 30
  • Don't know if it is the problem, but shouldn't `private_data_dir` be `/app/flask/ansible`? There is no directory `/app/ansible` that I can see. – Jack Hughes Oct 27 '20 at 14:20
  • true, but it still shows me an error: ValueError('private_data_dir path is either invalid or does not exist') – Omar Abdelrazik Oct 28 '20 at 10:29

1 Answers1

0

The ansible_runner Python package is just an interface to the ansible executable. You need to install Ansible itself within your Docker container. Add RUN apt-get update && apt-get install -y ansible to your Dockerfile

FROM python:3.7.2-stretch

RUN apt-get update && \
    apt-get install -y ansible && \
    rm -rf /var/lib/apt/lists/*

WORKDIR /app
ADD . /app
RUN pip install --upgrade pip && pip install flask uwsgi requests ansible_runner
CMD ["uwsgi","app.ini"]
djsumdog
  • 2,560
  • 1
  • 29
  • 55
  • that helped! but now I am getting an error that the private_data_dir path is invalid or doesn't exist. – Omar Abdelrazik Oct 28 '20 at 10:28
  • 1
    You're adding `.` to `/app`. Depending on where you build this container from, that could be including a different directory. You can also do `docker build -t somename .` followed by `docker run -it --rm somename /bin/sh` to enter the container and see the file structure you've created. – djsumdog Oct 28 '20 at 22:00