Ansible – Basic Playbook

I’m going to create a basic playbook just to ping 8.8.8.8

Host file contents under /etc/ansible/hosts. I have my NX-OS devices already included and configured the username for all devices (‘admin’)

[LAB_NXOS]
SPINE-1 ansible_host=192.168.255.1
SPINE-2 ansible_host=192.168.255.2
LEAF-1 ansible_host=192.168.255.3
LEAF-2 ansible_host=192.168.255.4

[LAB_NXOS:vars]
ansible_user=admin

Playbooks use YAML (YAML Ain’t Markup Language) which takes some getting used to. Below is the contents of my ‘playbook.yml’ file.
– ‘hosts’ maps to the ‘[LAB_NXOS]’ group within my /etc/ansible/hosts file.
– ‘connection: local’ (from what i understand) tells Ansible not to send a python script to the remote host for remote execution which NX-OS doesn’t support.
– ‘gather_facts: false’ isn’t supported by the Ansible module for NXOS. There are other modules which I will use later that can grab facts (serial number, hostname, configuration, etc)
– ‘nxos_command’ just tells Ansible to use the module specific for nxos. IOS uses ‘ios_command’.
– ‘commands’ is what you are actually running on the box

“playbook.yml”

 - name: Pinging from NXOS
   hosts: LAB_NXOS
   connection: local
   gather_facts: false
   tasks:
           - name: Pinging from NXOS
             nxos_command:
                     commands: "ping 8.8.8.8"

Now to run the playbook.

# First check to make sure the syntax is good to go. In this case there were no errors
BOX$ ansible-playbook playbook.yml --syntax-check

playbook: playbook.yml

# Running with --ask-pass because I do not have the password stored
BOX$ ansible-playbook playbook.yml --ask-pass
SSH password:

PLAY [Pinging from NXOS] **************************************************************************************************************

TASK [Pinging from NXOS] **************************************************************************************************************
ok: [SPINE-2]
ok: [SPINE-1]
ok: [LEAF-1]
ok: [LEAF-2]

PLAY RECAP ****************************************************************************************************************************
LEAF-1                     : ok=1    changed=0    unreachable=0    failed=0
LEAF-2                     : ok=1    changed=0    unreachable=0    failed=0
SPINE-1                    : ok=1    changed=0    unreachable=0    failed=0
SPINE-2                    : ok=1    changed=0    unreachable=0    failed=0

Just to see what a failure looks like, I configured a static route on LEAF-2 for 8.8.8.8 pointing to null0 and ran it again

BOX$ ansible-playbook playbook.yml --ask-pass
SSH password:

PLAY [Pinging from NXOS] **************************************************************************************************************************************

TASK [Pinging from NXOS] **************************************************************************************************************************************
ok: [SPINE-2]
ok: [SPINE-1]
ok: [LEAF-1]
fatal: [LEAF-2]: FAILED! => {"changed": false, "failed": true, "msg": "timeout trying to send command: ping 8.8.8.8"}
        to retry, use: --limit @/home/jason/ANS-TESTING-1/1.BASIC_PLAYBOOK/playbook.retry

PLAY RECAP ****************************************************************************************************************************************************
LEAF-1                     : ok=1    changed=0    unreachable=0    failed=0
LEAF-2                     : ok=0    changed=0    unreachable=0    failed=1
SPINE-1                    : ok=1    changed=0    unreachable=0    failed=0
SPINE-2                    : ok=1    changed=0    unreachable=0    failed=0

Ansible Playbook Documenation: http://docs.ansible.com/ansible/latest/playbooks.html

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.