1

Here I am trying to invoke a python script through ansible playbook. When I run the playbook containing below mentioned code, it is successfully invoking the script. But ansible script continues to run till the Python script finishes all its Task.

- hosts: localhost
  gather_facts: false
  vars:
    username: 'User'
    password: '1234@345'
  tasks:

    - name: Invoking Python script
      script: data_pull_push.py 
      args:
        executable: python

The python script "data_pull_push.py" is getting data from a Url and posting it to another Url. It has too many data. So it takes really long time to retrieve and post all the data. So I want the Ansible playbook to trigger the Python script in it and close the playbook. Python script should be running in Background.

And this Ansible playbook and python script is kept in git and will be running the playbook in Ansible tower. I am not getting any idea how to use that in Ansible. If anyone knows Please let me know.

saffron
  • 143
  • 1
  • 3
  • 12

2 Answers2

2

You should use Asynchronous Actions and Polling Ansible feature.

So your task should looks like:

- name: Long async task
  command: python data_pull_push.py
  async: <timeout value>
  poll: 0
Alexander Tolkachev
  • 4,608
  • 3
  • 14
  • 23
0

Create a background job using your choice of job manager utilities like atd, cron, supervisord, or systemd. Merely examples, many other service managers exist depending on operating system.

Of these, at least cron, supervisord, and systemd have wrapper Ansible modules. Even if one doesn't exist, you can call systemd-run or whatever with a generic command.

John Mahowald
  • 32,050
  • 2
  • 19
  • 34
  • If you wish to drive this thing from within Tower, please edit your question to say so. Also, it could be both. Scheduling playbook execution and backgrounding a job immediately are separate things, which could be implemented with a combination of Tower schedules and atd. – John Mahowald Apr 17 '20 at 16:27
  • How is it possible? I am not aware of this concept. Could you please tell me in detail so that I could understand and get an idea – saffron Apr 20 '20 at 09:04