SSH into remote machines

SSH into remote machines using Ansible

Deepankar Jain
English

About

Inventories in Red Hat Ansible Automation Platform are a fundamental component for organizing and managing hosts or groups of hosts. They provide a structured way to define and categorize the systems Ansible interacts with during automation tasks. 

Ansible inventories let you group hosts based on shared characteristics, such as environment, role, or location, allowing for targeted and efficient configuration management. Defining inventories gives administrators more control over their infrastructure and enables seamless orchestration and deployment of Ansible playbooks across the desired hosts. 

Ansible uses SSH protocol to connect to servers and run tasks, providing flexible and secure options for remote connectivity. This cheat sheet outlines two different methods to manage and configure remote hosts:

  • Using machine credentials as variables
  • Setting up private and public key credentials

With Red Hat Developer cheat sheets, you get essential information right at your fingertips so you can work faster and smarter. Easily learn new technologies and coding concepts and quickly find the answers you need.

Excerpt

Set up private key credentials

Create the credentials to SSH into the remote machine:

  1. Log in to the Ansible Automation Platform console.
  2. Navigate to the Credentials tab; under Add Button, select Machine.

Add your username, password, and SSH private key in the corresponding fields and click Save.

SSH into remote machines using Ansible

Inventories in Red Hat Ansible Automation Platform serve as a fundamental component for organizing and managing hosts or groups of hosts. They provide a structured way to define and categorize the systems Ansible interacts with during automation tasks. 

Ansible inventories let you group hosts based on shared characteristics, such as environment, role, or location, allowing for targeted and efficient configuration management. Defining inventories gives administrators more control over their infrastructure and enables seamless orchestration and deployment of Ansible playbooks across the desired hosts. Ansible uses SSH protocol to connect to servers and run tasks, providing flexible and secure options for remote connectivity. 

In this cheat sheet, you’ll learn how to manage and configure remote hosts. This cheat sheet covers two different approaches:

  • Using machine credentials as variables
  • Setting up private and public key credentials

Use machine credentials as variables

To specify the host variables:

  1. Log in to the Ansible Automation Platform console.
  2. Select Inventories from the left menu (Figure 1).
  3. Click Add.
  4. Select Add inventory.
    Adding a new inventory from the Ansible Automation Platform Inventory page
    Figure 1: Adding a new inventory from the Ansible Automation Platform Inventory page.
  5. Give the inventory a name and save it (Figure 2).
    Creating a new inventory page
    Figure 2: Creating a new inventory page.
  6. Select the groups from inventories and click Add group.
  7. Give the group a name and save it.
  8. Next, click HostsAdd a new host.
  9. Give the targeted remote machine IP a name and save it.
  10. In the Variables section, add the given fields, as shown in Figure 3.
    ansible_user : <username to ssh into>
    ansible_ssh_pass : <password for the username>
    ansible_become_pass: <password for becoming the root>
    Adding the host variables in Ansible Automation Platform
    Figure 3: Adding the host variables in Ansible Automation Platform.
  11. Run the job templates by defining the inventory, as shown in Figure 4. 
    Creating a new job template
    Figure 4: Creating a new job template.

Set up private key credentials

Create the credentials to SSH into the remote machine:

  1. Log in to the Ansible Automation Platform console.
  2. Navigate to the Credentials tab; under Add Button, select Machine.
  3. Add your username, password, and SSH private key in the corresponding fields and click Save (Figure 5).
    The Credential details page
    Figure 5: The Credential details page.

Set up the inventory:

  1. Select the inventory from the left menu.
  2. Click Add.
  3. Select Add inventory.
  4. Give a name to the inventory and save it.
  5. Select the groups from inventories and click Add group.
  6. Give a name to the group and save it.
  7. Next, click Hosts and click Add a new host.
  8. Give the targeted remote machine IP and save it.
  9. While configuring the job template, select the machine credentials and run your job template (Figure 6).
    Select the machine credentials to run the job template
    Figure 6: Select the machine credentials to run the job template.

Related Cheat sheets