Playbooks basics in ansible

A playbook (script file) is a file that describes the actions to be performed on a group of hosts.

Inside the playbook:

play is a set of tasks to be performed for a group of hosts

task is a specific task. The task contains at least: – description (task name can be omitted, but it is highly recommended) – module and command (action in module)


Playbook syntax

---
- name: Run show commands on routers
  hosts: cisco-routers
  gather_facts: false
  tasks:
    - name: run sh ip int br
      raw: sh ip int br

    - name: run sh ip route
      raw: sh ip route

- name: Run show commands on switches
  hosts: cisco-switches
  gather_facts: false
  tasks:

    - name: run sh int status  
      raw: sh int status
    
    - name: run sh vlan
      raw: show vlan

The playbook has two scenarios (play):

  • name: Run show commands on routers – script name (play). This parameter must be in any scenario
  • hosts: cisco-routers – script will be applied to devices in the cisco-routers group
  • Several groups can be specified here, for example, in this way: hosts: cisco-routers:cisco-switches
  • Usually, you need to specify the remote_user parameter in play. But, since we specified it in the Ansible configuration file, you can not specify it in play.
  • gather_facts: false – disable gathering facts about the device, since separate modules must be used for gathering facts for network equipment.
  • tasks: – followed by a list of tasks
    • each task has a name (optional) and an action. There can only be one action.
    • the action specifies which module to use and the module parameters.

Run playbook:

$ ansible-playbook show_commands_with_raw.yml

Leave a Reply

Your email address will not be published. Required fields are marked *