Hi Surjeet,

My intent with the second message was to show you the power of the ios/nxos
facts modules.  These modules return device information in a structured way
so that you don't have to mine your output with regular expressions if it
returns the data you are looking for.  With the ios facts module you get
version, serial number, ip addresses (ipv4/6), hostname, etc..

If you take the output and paste it into http://jsoneditoronline.org/ you
can "decompose" the ansible_facts object.   Here is the first part so you
can see that ansible_facts is a dictionary and the first key is
ansible_net_all_ipv4_addresses and the value is a list with two IPs (the
two IPv4 ips this switch has configured).  The next key would hold all the
IPv6 IPs in list but as you can see the list is empty [] because I don't
have ipv6 configured on this switch.

>
>         "ansible_facts": {
>             "ansible_net_all_ipv4_addresses": [
>                 "10.1.10.25",
>                 "192.0.2.33"
>             ],
>             "ansible_net_all_ipv6_addresses": [],
>             "ansible_net_filesystems": [
>                 "flash:"
>             ],
>

In this  playbook I use the ios_facts module to get the version of code in
the ansible_net_version key value pair that is part of the ansible_facts
"dictionary".

  tasks:
>     - name: Gather IOS Facts
>       ios_facts:



so the last line

debug: var=facts_output.ansible_facts.ansible_net_version


is just printing the value of the ansible_net_version key from the
ansible_facts dictionary returned by the ios_facts module that I stuffed in
a varialbe called "facts_output"

Having said all of that, there are many scenarios where you will need to
parse your output for data that has not been nicely packaged up for us in
these ansible modules so it is a valuable skill with many approaches.  You
want to use the one you are most comfortable with but you also don't want
to work any harder than you have to!

Here are some of the approaches I'm aware of and maybe others can chime in
with what works for them.

*1.  embed regexp in the command you send with the ios_command module.*
Here is sample output from the attached playbook.  I'm just sending the
regexp as part of the command as you would if you were in the CLI.
Ethan Banks at Packet Pushers does a nice little summary
<http://packetpushers.net/rocking-your-show-commands-with-regex/>and I'm
sure you can Google a bunch more

root@e8d7daa45b5b:/ansible/ansible2_4_base# ansible-playbook -i hosts
> get_ios_cmd_filter.yml
> PLAY [cisco]
> ********************************************************************************************************************************************************
> TASK [Show command with embedded regexp inc connected for all Conneced
> interfaces]
> **********************************************************************************
> ok: [arctic-3650] => (item=show int status | inc connected)
> TASK [debug]
> ********************************************************************************************************************************************************
> ok: [arctic-3650] => {
>     "output.results[0].stdout_lines": [
>         [
>             "Gi1/0/4                      connected    1          a-full
> a-1000 10/100/1000BaseTX"
>         ]
>     ]
> }
> TASK [Show command with embedded regexp for all IPs]
> ****************************************************************************************************************
> ok: [arctic-3650] => (item=show ip interface brief | inc \.[0-9]+[ ]+YES)
> TASK [debug]
> ********************************************************************************************************************************************************
> ok: [arctic-3650] => {
>     "output.results[0].stdout_lines": [
>         [
>             "Vlan1                  192.0.2.33      YES manual up
>           up      ",
>             "GigabitEthernet0/0     10.1.10.25      YES DHCP   up
>           up"
>         ]
>     ]
> }
> PLAY RECAP
> **********************************************************************************************************************************************************
> arctic-3650                : ok=4    changed=0    unreachable=0    failed=0


2.  Use the newish jinja2 filters regex_findall and regex_search

Ivan Pepelnjak has a nice summary of 1 and 2 here.
<http://automation.ipspace.net/Example:Parsing_Text_Printouts_within_Ansible_Playbooks>

3.  Look at the textfsm modules and filters

4.  look at napalm getters which return data for a variety of network
hardware in a structured way so that you can abstract out your actions in
your playbooks across many device types.

Kirk Byer has a very good Ansible series and he covers jinja filters and
using the textfsm parsing modules.  That is my favorite and I've moved most
of my parsing scripts to TextFSM these days.  Check out the ntc modules
from Network to Code (Jason Edelman).

http://docs.networktocode.com/en/latest/ntc-ansible%20Modules%20(multi-vendor)/modules_list.html

You can use the jinja2 based filters to do all kinds of things in Ansible
but they make my head hurt!  (which means I don't understand them well
enough yet) :D

Good Luck!

Claudia



On Mon, Jan 22, 2018 at 8:52 PM, Surjeet Singh <surjeet.singh0...@gmail.com>
wrote:

> Hi Claudia de Luna,
>
> thank you for your response.
>
> *following your **instruction i tried to collect the host name using **regular
> **expression used by peter and i can collect ios version.*
>
> *another question is triggered here after looking on your playbook**:-*
>
> register: facts_output
>
>
>     - debug: var=facts_output
>     - debug: var=facts_output.ansible_facts.ansible_net_version
>
> question is regarding last line here, in your last line you add
> ansible_facts, can you please let me know what is purpose of this and where
> it came. rest i know about ansible_net_version.
>
> also can you please refer me any document how to use regular expression
> ansible playbook.i have used these in python so i have small understanding
> in python.
>
> Regards,
> *Surjeet Singh*
> Technical Specialist – Networks DATA
> CCNA, CCNP(R&S)
> Cell : +917838707047 <+91%2078387%2007047>
>
> To become bigger person,need to walk with bigger Vision !!!!
>
> On Sun, Jan 21, 2018 at 11:59 PM, Claudia de Luna <delu...@gmail.com>
> wrote:
>
>> Using Ansible ios_facts module
>>
>> Here is a simple playbook to gather ios facts.
>>
>> I took the output and pasted it into one of the may JSON lint/editors on
>> line to easily see the structure and then added a debug statement for the
>> key:value pair I wanted.  Version in this case.
>>
>>
>> ---
>> - hosts: cisco
>>   connection: local
>>   gather_facts: False
>>   ignore_errors: yes
>>
>>
>>   vars:
>>     cli:
>>         host: "{{ host }}"
>>         username: "{{ username }}"
>>         password: "{{ password }}"
>>
>>
>>   tasks:
>>     - name: Gather NX-OS Facts
>>       ios_facts:
>>         provider: "{{ cli }}"
>>
>>
>>       register: facts_output
>>
>>
>>     - debug: var=facts_output
>>     - debug: var=facts_output.ansible_facts.ansible_net_version
>>
>>
>>     - local_action: copy content="{{ facts_output }}" dest="./{{
>> inventory_hostname }}.txt"
>>
>>
>>
>>
>>
>>
>>
>> TASK [debug] ************************************************************
>> ************************************************************
>> ********************************
>> ok: [arctic-3650] => {
>>     "facts_output.ansible_facts.ansible_net_version": "03.06.06E"
>> }
>>
>>
>>
>>
>>
>>
>> On Saturday, January 20, 2018 at 8:57:38 AM UTC-8, Surjeet Singh wrote:
>>>
>>> Hi Peter,
>>>
>>> I am facing another issue with same i am trying to debug version only
>>> from the results of show version using below method but it gives me
>>> variable error. however when i run playbook -vvv i can see the show version
>>> command is excuted and they are mapped to stdout and stdout_lines as you
>>> mention in document. can you please help me with that or redirect me
>>> towards of link.
>>>
>>>       register: version
>>>     - debug: var = version.stdout[0].Version
>>>
>>> Regards/surjeet
>>>
>>> On Saturday, September 3, 2016 at 8:58:39 AM UTC+5:30, Peter Sprygada
>>> wrote:
>>>>
>>>> Hi Valerie,
>>>>
>>>> Since network devices such as IOS do not provide a shell environment
>>>> nor the ability to download and run arbitrary executables, we are fairly
>>>> constrained from using the current connection plugin module implemented in
>>>> core.  So in order to build modules that work with network devices, we
>>>> build an integration that effectively treats SSH or more appropriately
>>>> said, CLI over SSH like an API.  During module execute, we build an SSH
>>>> session to the remote device for the purposes of sending and receiving
>>>> commands and output.  That is way we must specify connection=local.
>>>>
>>>> Peter
>>>>
>>>> On Fri, Sep 2, 2016 at 7:55 AM, Valérie P <valeri...@gmail.com> wrote:
>>>>
>>>>> Hello John,
>>>>>
>>>>> I've had a little bit of trouble with the ios_* modules and thanks to
>>>>> the source found in this sample it is now functional, anyhow I do not
>>>>> understand every line of it, and particularly the "connection: local" one.
>>>>> What is it used for? The ansible documentation refer to the connection:
>>>>> local as a way to make the playbook play locally.
>>>>>
>>>>> "It may be useful to use a playbook locally, rather than by connecting
>>>>> over SSH. This can be useful for assuring the configuration of a system by
>>>>> putting a playbook in a crontab. This may also be used to run a playbook
>>>>> inside an OS installer, such as an Anaconda kickstart.
>>>>>
>>>>> To run an entire playbook locally, just set the “hosts:” line to
>>>>> “hosts: 127.0.0.1” and then run the playbook like so:
>>>>>
>>>>> ansible-playbook playbook.yml --connection=local
>>>>>
>>>>> Alternatively, a local connection can be used in a single playbook
>>>>> play, even if other plays in the playbook use the default remote 
>>>>> connection
>>>>> type:
>>>>>
>>>>> - hosts: 127.0.0.1
>>>>>   connection: local
>>>>>
>>>>> "
>>>>> Why is the connection: local parameters a must for it to work?
>>>>>
>>>>> Thanks in advance!
>>>>> Valerie
>>>>>
>>>>>
>>>>> Le mercredi 24 août 2016 09:20:15 UTC+2, John Barker a écrit :
>>>>>>
>>>>>> I've added a comment with the the corrected playbook sample
>>>>>>
>>>>>> https://gist.github.com/privateip/11b042e569585ee9248a
>>>>>>
>>>>>> Regards,
>>>>>> John Barker
>>>>>>
>>>>>> On Wednesday, 10 August 2016 15:32:23 UTC+1, Bharath Bharadwaj wrote:
>>>>>>>
>>>>>>> Hi All,
>>>>>>>
>>>>>>> I'm new to Ansible and yml, my goal is to automate a part of network
>>>>>>> operation, I just want to start with a very simple output, copied below 
>>>>>>> is
>>>>>>> my playbook, I'm trying to run a show version, but i'm getting error 
>>>>>>> when
>>>>>>> executing the output, yet when i try the same yml script through yml
>>>>>>> validator, there is no errors.
>>>>>>>
>>>>>>> My Playbook
>>>>>>>
>>>>>>> vars:
>>>>>>>     cli:
>>>>>>>     host: "{{ network }}"
>>>>>>>     username: admin
>>>>>>>     password: test@123
>>>>>>>     transport: cli
>>>>>>>
>>>>>>> tasks:
>>>>>>> - name: run multiple commands on remote nodes
>>>>>>>   ios_command:
>>>>>>>   - commands: show version
>>>>>>>   - provider: "{{ cli }}"
>>>>>>>   - transport: cli
>>>>>>>
>>>>>>> Error:
>>>>>>> "ERROR! playbooks must be a list of plays
>>>>>>>
>>>>>>> The error appears to have been in 
>>>>>>> '/etc/ansible/playbooks/cisco_ios.yml':
>>>>>>> line 1, column 1, but may
>>>>>>> be elsewhere in the file depending on the exact syntax problem.
>>>>>>>
>>>>>>> The offending line appears to be:
>>>>>>>
>>>>>>>
>>>>>>> vars:
>>>>>>> ^ here"
>>>>>>>
>>>>>> --
>>>>> 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-proje...@googlegroups.com.
>>>>> To post to this group, send email to ansible...@googlegroups.com.
>>>>> To view this discussion on the web visit
>>>>> https://groups.google.com/d/msgid/ansible-project/6383da04-b
>>>>> 68b-4a33-85e9-87318eb0a5b8%40googlegroups.com
>>>>> <https://groups.google.com/d/msgid/ansible-project/6383da04-b68b-4a33-85e9-87318eb0a5b8%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>>> .
>>>>>
>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>
>>>>
>>>> --
>> You received this message because you are subscribed to a topic in the
>> Google Groups "Ansible Project" group.
>> To unsubscribe from this topic, visit https://groups.google.com/d/to
>> pic/ansible-project/Ul5D-gAzRrg/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to
>> ansible-project+unsubscr...@googlegroups.com.
>> To post to this group, send email to ansible-project@googlegroups.com.
>> To view this discussion on the web visit https://groups.google.com/d/ms
>> gid/ansible-project/826a69bb-d730-48f6-a7ce-103808c1f0f0%
>> 40googlegroups.com
>> <https://groups.google.com/d/msgid/ansible-project/826a69bb-d730-48f6-a7ce-103808c1f0f0%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>> For more options, visit https://groups.google.com/d/optout.
>>
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Ansible Project" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/
> topic/ansible-project/Ul5D-gAzRrg/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> ansible-project+unsubscr...@googlegroups.com.
> To post to this group, send email to ansible-project@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/ansible-project/CA%2BpLPDTaTE0o3NWOp00je5ZiRTMveL
> 4z3MTTPuiU6%2Bemv6v6%3DQ%40mail.gmail.com
> <https://groups.google.com/d/msgid/ansible-project/CA%2BpLPDTaTE0o3NWOp00je5ZiRTMveL4z3MTTPuiU6%2Bemv6v6%3DQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAENRZgbhS%2Bu3PBLns78nBHo-C86p%2BoVQoWS96czJGfT80PJBbA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Attachment: get_ios_cmd_filter.yml
Description: Binary data

Reply via email to