Bookmark this page

Chapter 2. Running Commands and Plays

Abstract

Goal Run automated tasks on devices using plays and ad hoc commands.
Objectives
  • Run ad hoc commands to execute single, one-time tasks.

  • Write playbooks, run plays with ansible-playbook, and interpret the resulting output.

  • Build more complex plays that include multiple tasks.

  • Write playbooks that include multiple plays.

Sections
  • Executing Ad Hoc Commands (and Guided Exercise)

  • Preparing Ansible Playbooks (and Guided Exercise)

  • Building a Play with Multiple Tasks (and Guided Exercise)

  • Composing Playbooks with Multiple Plays (and Guided Exercise)

Lab

Running Commands and Plays

Executing Ad Hoc Commands

Objectives

After completing this section, you should be able to run ad hoc commands to execute single, one-time tasks.

Introducing Ad Hoc Commands

An ad hoc command is a single, manually run Ansible task that you want to perform quickly and do not need to save to run again later.

This is the form of an ad hoc command:

ansible host-pattern -m module [-a 'module arguments'] [-i inventory]

When module arguments are enclosed within single or double quote marks, the space that follows the -a option and the arguments is optional.

Running Ad Hoc Commands

When are ad hoc commands useful? It depends on the module.

To enable the netconf service on a platform that supports it, run the following command:

[user@host ~]$ ansible -m junos_netconf host-identifier

To test Ansible connectivity and authentication to a managed resource (not ICMP), run the following command:

[user@host ~]$ ansible -m ping host-identifier

To determine if you can reach an IP address from a managed resource (using ICMP), run the following command:

[user@host ~]$ ansible -m ios_ping host-identifier -a "dest=ip-address"

You can pass arguments to modules with the -a option.

[user@host ~]$ ansible -m vyos_command -a "commands='command'" host-identifier

More Examples of Ad Hoc Commands

To gather facts about an IOS network device, run the following command:

[user@host ~]$ ansible -m ios_facts ios-device-host-identifier

To gather facts about a JunOS device, run the following command:

[user@host ~]$ ansible -m junos_facts junos-device-host-identifier
Revision: do457-2.5-4693601