-1

We are trying to pass return output (JSON) from Firewall Facts (containing 1000+ Policies) to a Python script, then Python Script will process that output (like getting right firewall policy to modify) and pass the output as variable to Ansible playbook back again for next task execution.

We have tested this method to convert CSV files to Excel. But not sure on in terms of variables.

UME
  • 323
  • 1
  • 2
  • 7
  • Is this question a follow up to [How to read and output JSON response using Ansible](https://stackoverflow.com/questions/75208052/)? – U880D Jan 25 '23 at 12:12

2 Answers2

0

I think one way to achieve this will be taking the output with "register" and then storing it into a local file. After that make the python script read the file and take the information from there storing it into a variable.

 - name: task that will create the output
   ...............
   register: foo_output

 - name: take the output into a file
   copy: 
    content: "{{ foo_output }}"
    dest: /path/to/destination/file

I hope this works for you, let me know how it goes.

iker lasaga
  • 330
  • 1
  • 3
  • 18
0

Is there any way to call a Python script from an Ansible playbook and pass the output as variable to an Ansible playbook back again for the next task?

Yes, of course.

Since there is almost no information, further description about what you try to achieve, nothing about "the script" at all, etc., the recommended approach is Developing a module and following the example of Creating a module.

By doing this you can simply and fast come to the step of Verifying your module code in a playbook

- name: Test Python script
  hosts: localhost

  vars:

    input: "TEST"

  tasks:

  - name: Run Python script
    the_script:
      name: "{{ input }}"
    register: output

  - name: Show output
    debug:
      msg: '{{ output }}'

Further Documentation

Similar Q&A

U880D
  • 8,601
  • 6
  • 24
  • 40
  • The information has not increased. Maybe you can have a look into [How to ask](https://stackoverflow.com/help/how-to-ask), [Minimal Example](https://stackoverflow.com/help/minimal-reproducible-example), and even if it is not about firewalls [How to make a great reproducible example](https://stackoverflow.com/questions/5963269/), since I can't even guess what kind of firewall you are using, how the facts and policies are looks like, how they are structured, how big they are, which kind of Ansible modules you are using, where the data is coming from and going too, etc. ... – U880D Jan 25 '23 at 07:37