Playbooks against Remote Hosts

Connect to remote Linux and Windows hosts using Ansible playbooks.

Let’s look at how you can codify the ad-hoc command to connect to the Linux instance or virtual machine.

Connect to remote Linux host#

You will learn how to convert the following ad-hoc command to an Ansible playbook:

Linux ad-hoc ping

Create the playbook#

Create the playbook in these steps:

  1. Create a ping.yml file.
  2. Add the hosts line to use the host pattern of all.
Hosts
  1. Create a vars list and define the connection variables. Replace <Password> with your password.
Variables List

Defining the variables in the playbooks prevents you from defining them at run time.

At the moment, the password is in cleartext. This is obviously bad. Ansible has a feature that allows you to encrypt variables, which will be covered later.

  1. Add a tasks list.
Tasks

Run the playbook#

The final form of the ping.yml file looks like the one below. You can execute the playbook by clicking on the Run button.

/
ping.yml
Execute ping.yml

Once the environment is set up, execute the following command in the terminal:

Execute ping.yml

Replace <Public IP Address> with the address of the Linux remote host. Ensure that there is a , at the end of the IP address to indicate that this is a host list.

Add a prompt#

The ping.yml playbook can be used to ping any Linux host by providing a different host list. However, since the username and password are hardcoded in the playbook, that user must exist on every host with the same password.

To increase the repeatability of the playbook, you can use vars_prompt to prompt for the username and password.

  1. Create a playbook called ping_prompts.yml.
  2. Add a vars_prompt for the ssh user and ssh password.
vars_prompt
  1. Update vars with the variables defined by the prompts.
Update vars

Ansible variables are enclosed in double-quotes and curly braces "{{ var }}".

  1. Review and execute the final form below by clicking on the Run button.
/
ping_prompts.yml
Execute ping_prompts.yml

Once the environment is set up, execute the following command in the terminal:

Execute ping_prompts.yml
  1. When prompted, enter the username and password.

Default username
The username is ansible by default.

An added benefit of using a vars_prompt for the password is that you are far less likely to commit a password to source control, which would be bad.

Try it now#

Practice converting Windows ad-hoc ping command to a playbook.

Convert to a Windows ping playbook#

The command to connect to the remote Windows host is given below. Write your code in the win_ping.yml file.

Windows ad-hoc ping command

Solution
The solution to this challenge is provided in the Solution directory. Try it yourself first and compare your solution with the provided solution.

/
Solution
win_ping.yml
win_ping.yml

In this lesson, we introduced vars and vars_prompt and converted the ad-hoc commands to playbooks to make them repeatable.

Ad-hoc Commands against Remote Hosts
Configure an Nginx Web Server
Mark as Completed
Report an Issue