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:
ansible all -i <Public Ip Address>, -m ping \-e "ansible_user=ansible ansible_password=<Password> ansible_ssh_common_args='-o StrictHostKeyChecking=no'"
Create the playbook
Create the playbook in these steps:
- Create a
ping.yml
file. - Add the
hosts
line to use the host pattern ofall
.
---- hosts: all
- Create a
vars
list and define the connection variables. Replace<Password>
with your password.
vars:ansible_user: ansibleansible_password: <Password>ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
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.
- Add a
tasks
list.
tasks:- name: run pingping:
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.
--- - hosts: all gather_facts: false vars: ansible_user: ansible ansible_password: <Password> ansible_ssh_common_args: '-o StrictHostKeyChecking=no' tasks: - name: run ping ping:
Once the environment is set up, execute the following command in the terminal:
# Replace the password with the <Public IP Address># with the Linux Instance or VM IP Addressansible-playbook ping.yml -i <Public IP Address>,
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.
- Create a playbook called
ping_prompts.yml
. - Add a
vars_prompt
for the ssh user and ssh password.
---- hosts: allvars_prompt:- name: userprompt: "Enter ssh user"- name: passwordprompt: "Enter password for ssh user"
- Update
vars
with the variables defined by the prompts.
vars:ansible_user: "{{ user }}"ansible_password: "{{ password }}"ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
Ansible variables are enclosed in double-quotes and curly braces "{{ var }}"
.
- Review and execute the final form below by clicking on the
Run
button.
--- - hosts: all gather_facts: false vars_prompt: - name: user prompt: "Enter ssh user" - name: password prompt: "Enter password for ssh user" vars: ansible_user: "{{ user }}" ansible_password: "{{ password }}" ansible_ssh_common_args: '-o StrictHostKeyChecking=no' tasks: - name: run ping ping:
Once the environment is set up, execute the following command in the terminal:
ansible-playbook ping_prompts.yml -i <Public Ip Address>,
- When prompted, enter the username and password.
Default username
The username isansible
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.
ansible all -i <Public Ip Address>, -m win_ping-e "ansible_user=ansible ansible_password=<Password> ansible_winrm_server_cert_validation=ignore ansible_connection=winrm"
Solution
The solution to this challenge is provided in theSolution
directory. Try it yourself first and compare your solution with the provided solution.
--- - hosts: all gather_facts: false vars_prompt: - name: username prompt: "Enter local username" private: no - name: password prompt: "Enter password" vars: ansible_user: "{{ username }}" ansible_password: "{{ password }}" ansible_connection: winrm ansible_winrm_transport: ntlm ansible_winrm_server_cert_validation: ignore tasks: - name: run win_ping win_ping:
In this lesson, we introduced vars
and vars_prompt
and converted the ad-hoc commands to playbooks to make them repeatable.
Get hands-on with 1300+ tech skills courses.