简体   繁体   中英

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?

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM