Jason Edelman's Blog
  • Home
  • About
  • Contact

Network Automation with Python, Ansible, and Twitter Integration

9/17/2014

0 Comments

 
Last month I wrote about using the Cisco Nexus NX-API to extract stats from a Nexus switch while using Ansible.  For some reason, last night I finally went on to tackle how to integrate with the Twitter API and then integrated the two together.  Integrating with Twitter has always been top of mind, but just put it on the back burner.  Funny enough though, it was a pretty quick integration thanks to the great people at Google.
What am I talking about?

In the code I pushed last month, I created an Ansible playbook that pulls interface stats from a Nexus 9000 (or any other Nexus device supporting NX-API) and then creates a template report for those stats.  It was pretty vanilla, nothing fancy about it.

There have been integrations with other social platforms, but to be honest, the one that has been stuck in my brain is Hubot that is used at GitHub.  Several months back I remember hearing about Hubot for the second or third time while listening to the Cloudcast podcast with Mark Imbriaco (just before he went to Digital Ocean).  It is amazing to see what can be done with the right team, culture, and tools.

Anyway, last night I figured I would check out using the Twitter API and integrate with Python assuming that it would be complicated, require “hard core” integration, and lots of time. It was totally the opposite – thankfully I found the Google python-twitter library on my first Google search!  It took just a few minutes.

Following the integration, the same playbook from last month now tweets 3 times.  It tweets when the playbook starts, after data is pulled from the Nexus 9000, and right before it ends.  Having tweets mid-way through longer playbooks could be good too.  You can tweet to certain people (or secure handles) or include whatever hashtag you’d like for easy tracking on changes.  Right now, the user of this playbook should modify the key_id variable for each execution – Twitter doesn’t like it when you tweet the same message repeatedly :)

The GitHub repo has been updated with the new playbook that has “with_twitter” in the file name.  The repo also has the ‘tweet’ module that I created.  Note: I am only using the twitter API to send tweets right now, but the library has dozens of methods built-in for gaining status messages, mentions, timelines, etc., so it is in fact a pretty robust library.

The Twitter Integration

The steps here are straightforward (also here), but you will have to create an official “Twitter app” first and then they’ll give you the proper tokens and IDs to use while writing your code.

If you look at the playbook I’m using, you’ll notice I’m passing ‘creds’ to the tweet module.  ‘creds’ is just a variable I defined in another file so you can’t see it.  ‘creds’ in my case is a string that contains the 4 variables needed to use the twitter API.

This will be the flow to follow to test:

import twitter
api = twitter.Api(consumer_key='consumer_key', consumer_secret='consumer_secret', access_token_key='access_token', access_token_secret='access_token_secret')

print api.VerifyCredentials()
status = api.PostUpdate(‘this message will post to twitter')

If you have any questions, feel free to reach out!

Thanks,
Jason

Twitter: @jedelman8

0 Comments



Leave a Reply.

    Author

    Jason Edelman, Founder & CTO of Network to Code. 


    Enter your email address:

    Delivered by FeedBurner


    RSS Feed


    Categories

    All
    1cloudroad
    2011
    2960
    40gbe
    7000
    Arista
    Aruba
    Big Switch
    Brocade
    Capwap
    Christmas
    Cisco
    Controller
    Data Center
    Dell Force10
    Embrane
    Extreme
    Fex
    Hadoop
    Hp
    Ibm
    Isr G2
    Juniper
    Limited Lifetime Warranty
    Meraki
    Multicast
    N7k
    Nexus
    Nicira
    Ons
    Opendaylight
    Openflow
    Openstack
    Presidio
    Qsfp
    Quick Facts
    Routeflow
    Sdn
    Sdn Ecosystem
    Security
    Ucs


    Archives

    May 2015
    April 2015
    February 2015
    January 2015
    December 2014
    November 2014
    October 2014
    September 2014
    August 2014
    June 2014
    May 2014
    March 2014
    February 2014
    January 2014
    December 2013
    November 2013
    October 2013
    September 2013
    August 2013
    July 2013
    June 2013
    May 2013
    April 2013
    March 2013
    February 2013
    January 2013
    December 2012
    November 2012
    October 2012
    June 2012
    May 2012
    April 2012
    March 2012
    February 2012
    January 2012
    December 2011
    November 2011


    View my profile on LinkedIn
Photo used under Creative Commons from NASA Goddard Photo and Video