Ansible Tower – Running Python Script in Background via Playbook

ansibleansible-playbookansible-towerpython

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.

Best Answer

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