Variables

We discussed variables a bit in the previous section. Because our managed nodes are not exactly alike, we need to accommodate the differences via variables. Variable names should be letters, numbers, and underscores, and should always start with a letter. Variables are commonly defined in three locations:

  • The playbook
  • The inventory file
  • Separate files to be included in files and roles

Let's look at an example of defining variables in a playbook, cisco_1.yml:

---- name: Configure SNMP Contacthosts: "nexus"gather_facts: falseconnection: localvars:cli:host: "{{ inventory_hostname }}"username: ciscopassword: ciscotransport: clitasks:- name: configure snmp contactnxos_snmp_contact:contact: TEST_1state: presentprovider: "{{ cli }}" ...

Get Mastering Python Networking - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.