Re: [ansible-project] Sending ansible output to python function

2020-06-15 Thread Brian Coca
You can use a callback plugin, they are the ones that process 'output'
from a play/task.



-- 
--
Brian Coca

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CACVha7c5DcmCUKDotMvnHbto0soX6tKVN2FkkRF0YfZDe0gdfA%40mail.gmail.com.


Re: [ansible-project] Sending ansible output to python function

2020-06-11 Thread Abhijeet Kasurde
You can write a custom module and send the "output_cardTable" to it. Have
your business logic in that custom module. You can read more about module
development here -
https://docs.ansible.com/ansible/latest/dev_guide/developing_modules_general.html#developing-modules-general

On Thu, Jun 11, 2020 at 9:42 PM Srinivas Naram 
wrote:

> Did you try to initiate Ansible playbook from Python code and capture
> Ansible output directly in the Python code ?
>
> You can use os module for it. Try using os.system('ansible-playbook
> 
> in the playbook add another play
>
> debug:
>var: output_cardTable
>
> HTH
>
> On Thu, Jun 11, 2020 at 9:22 PM 'Douglas R Christensen' via Ansible
> Project  wrote:
>
>> Hello,
>>
>> I have posted the following at
>> https://stackoverflow.com/questions/62309274/how-to-have-ansible-send-output-to-a-python-function-in-a-custom-library,
>> but I wanted to share here to get some more insight.
>>
>> I am a newbie at working with Ansible and Python and I am attempting to
>> develop an Ansible playbook that will query several Cisco ASR5500's running
>> current iOS software for CLI output of various show commands. I would like
>> to take that output and send it to a python function in a custom library
>> which will apply logic to see if the response from the ASR shows that it is
>> healthy or not. Currently, I am sending the Ansible output to a file with
>> the thought of having Python read the file and analyze each line. Here is a
>> snippet from my playbook:
>>
>> - name: show card table # run "show card table" on node
>>   ios_command:
>> commands:
>>   - show card table
>>   register: output_cardTable- name: send output_cardTable to file # 
>> send "show card table" to file
>>   lineinfile:
>> dest: /home/**my_user**/testoutput/{{ inventory_hostname }}_{{ my_time }}
>> line: "{{ output_cardTable.stdout_lines }}"
>> insertafter: EOF
>>
>> This snippet takes the output of "show card table" and sends it to a
>> timestamped file as a multi-dimensional array (not sure why it's formatted
>> like that, but that's another question for later). My first thought was to
>> have Ansible run the checks and then have Python read the generated files
>> for each node and apply its logic to determine the node's health.
>>
>>
>> However, I would like to know if there is a way for Ansible to send the
>> output of the task directly to a function in a imported Python library,
>> similar to how RobotFramework can invoke a Python function from a library
>> that has been declared. I do not know if such syntax exists, but Ansible is
>> better at maintaining connections to multiple nodes concurrently, and
>> RobotFramework is not the tool of choice for my organization.
>>
>>
>> So, how would I write the task that sends the "output_cardTable" content
>> to a python function, and how would I declare that function library in the
>> header of the playbook so Ansible can see and use it?
>>
>>
>> Any insight would be appreciated. Thank you.
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Ansible Project" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to ansible-project+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/ansible-project/6df3edc0-ac47-4c4d-92de-1ca1eadcd722o%40googlegroups.com
>> 
>> .
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-project+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CAG-N3P7tCdat2ordierVDh6DOzbRdUPZUEPnNKVMXMbrPGUkYw%40mail.gmail.com
> 
> .
>


-- 
Thanks,
Abhijeet Kasurde

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAFwWkHr_jSXWhUHgc5SBk30j7YJ%3D4yHkPq27GEuxTEdX5gUw3w%40mail.gmail.com.


Re: [ansible-project] Sending ansible output to python function

2020-06-11 Thread Srinivas Naram
Did you try to initiate Ansible playbook from Python code and capture
Ansible output directly in the Python code ?

You can use os module for it. Try using os.system('ansible-playbook
 wrote:

> Hello,
>
> I have posted the following at
> https://stackoverflow.com/questions/62309274/how-to-have-ansible-send-output-to-a-python-function-in-a-custom-library,
> but I wanted to share here to get some more insight.
>
> I am a newbie at working with Ansible and Python and I am attempting to
> develop an Ansible playbook that will query several Cisco ASR5500's running
> current iOS software for CLI output of various show commands. I would like
> to take that output and send it to a python function in a custom library
> which will apply logic to see if the response from the ASR shows that it is
> healthy or not. Currently, I am sending the Ansible output to a file with
> the thought of having Python read the file and analyze each line. Here is a
> snippet from my playbook:
>
> - name: show card table # run "show card table" on node
>   ios_command:
> commands:
>   - show card table
>   register: output_cardTable- name: send output_cardTable to file # 
> send "show card table" to file
>   lineinfile:
> dest: /home/**my_user**/testoutput/{{ inventory_hostname }}_{{ my_time }}
> line: "{{ output_cardTable.stdout_lines }}"
> insertafter: EOF
>
> This snippet takes the output of "show card table" and sends it to a
> timestamped file as a multi-dimensional array (not sure why it's formatted
> like that, but that's another question for later). My first thought was to
> have Ansible run the checks and then have Python read the generated files
> for each node and apply its logic to determine the node's health.
>
>
> However, I would like to know if there is a way for Ansible to send the
> output of the task directly to a function in a imported Python library,
> similar to how RobotFramework can invoke a Python function from a library
> that has been declared. I do not know if such syntax exists, but Ansible is
> better at maintaining connections to multiple nodes concurrently, and
> RobotFramework is not the tool of choice for my organization.
>
>
> So, how would I write the task that sends the "output_cardTable" content
> to a python function, and how would I declare that function library in the
> header of the playbook so Ansible can see and use it?
>
>
> Any insight would be appreciated. Thank you.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-project+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/6df3edc0-ac47-4c4d-92de-1ca1eadcd722o%40googlegroups.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAG-N3P7tCdat2ordierVDh6DOzbRdUPZUEPnNKVMXMbrPGUkYw%40mail.gmail.com.


[ansible-project] Sending ansible output to python function

2020-06-11 Thread 'Douglas R Christensen' via Ansible Project
Hello,

I have posted the following at 
https://stackoverflow.com/questions/62309274/how-to-have-ansible-send-output-to-a-python-function-in-a-custom-library,
 
but I wanted to share here to get some more insight.

I am a newbie at working with Ansible and Python and I am attempting to 
develop an Ansible playbook that will query several Cisco ASR5500's running 
current iOS software for CLI output of various show commands. I would like 
to take that output and send it to a python function in a custom library 
which will apply logic to see if the response from the ASR shows that it is 
healthy or not. Currently, I am sending the Ansible output to a file with 
the thought of having Python read the file and analyze each line. Here is a 
snippet from my playbook:

- name: show card table # run "show card table" on node
  ios_command:
commands:
  - show card table
  register: output_cardTable- name: send output_cardTable to file # 
send "show card table" to file
  lineinfile:
dest: /home/**my_user**/testoutput/{{ inventory_hostname }}_{{ my_time }}
line: "{{ output_cardTable.stdout_lines }}"
insertafter: EOF

This snippet takes the output of "show card table" and sends it to a 
timestamped file as a multi-dimensional array (not sure why it's formatted 
like that, but that's another question for later). My first thought was to 
have Ansible run the checks and then have Python read the generated files 
for each node and apply its logic to determine the node's health.


However, I would like to know if there is a way for Ansible to send the 
output of the task directly to a function in a imported Python library, 
similar to how RobotFramework can invoke a Python function from a library 
that has been declared. I do not know if such syntax exists, but Ansible is 
better at maintaining connections to multiple nodes concurrently, and 
RobotFramework is not the tool of choice for my organization.


So, how would I write the task that sends the "output_cardTable" content to 
a python function, and how would I declare that function library in the 
header of the playbook so Ansible can see and use it?


Any insight would be appreciated. Thank you.

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/6df3edc0-ac47-4c4d-92de-1ca1eadcd722o%40googlegroups.com.