msouilhi
Mission Specialist
Mission Specialist
  • 2,306 Views

Understanding Machine Credentials and Creating SSH Connections in Ansible Automation

In the Ansible automation platform, I would like to understand how machine credentials work.

By utilizing the execution environment, playbook execution will not be processed on the control machine but rather in the execution environment. Therefore, my question is: what machine credentials should I use to enable SSH connections to the target hosts from the automation platform?

In my case, I have 2 target machines where SSH connections were already enabled and keys are shared. How can I create these machine credentials?

Additionally, I have already created an inventory with those 2 hosts.

8 Replies
Karim_Expert
Mission Specialist
Mission Specialist
  • 2,149 Views

you can create machine credentials in ansible tower to run playbooks after you distribute the SSH Key Pair you go to ansible tower web console and open credetials tab and press new credential you give it a name and in the SSH PRIVATE KEY field you paste the contents of the private key file in your host you can find it in /home/user_home_dir/.ssh/ after this you can also choose more option and press save to finish and start using the new machine credential to launch your playbooks .

Mostafa1
Flight Engineer
Flight Engineer
  • 2,149 Views

In Ansible, machine credentials are used to establish SSH connections between the Ansible control node and the target hosts. These credentials typically consist of SSH keys or usernames/passwords, depending on your authentication method.

To create machine credentials in Ansible Tower or AWX (the automation platform), follow these steps:

  1. Navigate to the "Credentials" page: In the Ansible Tower or AWX web interface, go to the "Credentials" section.

  2. Click on "Add" to create a new credential: On the "Credentials" page, click on the "Add" button to create a new credential.

  3. Select the credential type: Choose the appropriate credential type based on how you want to authenticate with the target hosts. For SSH connections, you'll typically choose "Machine".

  4. Enter the credential details: Fill in the required fields with the necessary information. This includes the username, SSH private key (or password), and any additional options if needed.

  5. Save the credential: Once you've entered all the required information, save the credential.

  6. Assign the credential to your job template or inventory: After creating the credential, you'll need to assign it to either your job template (if you're running ad-hoc commands or playbooks) or your inventory (if you're using the Tower or AWX inventory).

  7. Test the connection: Before running any playbooks or jobs, it's a good idea to test the connection to ensure that the credential is working properly. You can do this by running a simple ping command or a playbook that connects to the target hosts.

After following these steps, your Ansible Tower or AWX instance will have the necessary machine credentials to establish SSH connections to your target hosts. These credentials can then be used in your job templates or inventories to automate tasks on those hosts.

 

Mostafa1
Flight Engineer
Flight Engineer
  • 2,149 Views

In Ansible Controller, the machine credentials will be used normally from the execution environment container to the targetted hosts in the inventory

PeterSzabo
Mission Specialist
Mission Specialist
  • 2,110 Views

You have not specified your AAP version, but generally you need to create a credential, assignt it to a host or job template and it should work.

It does not matter if you have keys already deployed on the host running AAP, because when you use execution environment, technically a container is started and ansible is running inside, having no access to host resources.

ericbos
Flight Engineer
Flight Engineer
  • 2,082 Views

I agree with the above

Best regards, Eric

napapornt
Mission Specialist
Mission Specialist
  • 2,039 Views

Certainly! Let’s dive into how machine credentials work in Ansible Automation and how you can create them to enable SSH connections to your target hosts.

Understanding Machine Credentials
Machine credentials are essential for Ansible Tower (or Ansible Automation Platform) to authenticate and establish SSH connections to remote hosts. These credentials allow Tower to securely communicate with the target machines during playbook execution. Here’s how they work:
SSH Authentication:
Ansible Tower uses SSH to connect to remote hosts (or the Windows equivalent).
To pass the SSH key from Tower to the SSH client, the key must be decrypted before it can be sent.
Tower then uses a named pipe to send the key to SSH, ensuring that it is never written to disk.
If passwords are used, Tower handles them by responding directly to the password prompt and decrypting the password before writing it to the prompt.
Creating Machine Credentials:
Machine credentials are created within Ansible Tower.
They include information such as the SSH username, password (if applicable), and SSH private key.
You can create machine credentials for different users or teams.
Associating with Job Templates:
Once you’ve created machine credentials, you can associate them with job templates.
Job templates define the tasks to be executed on the target hosts.
When launching a job using a template, Tower uses the specified machine credentials to authenticate with the hosts.
Creating Machine Credentials
Here’s how you can create machine credentials in Ansible Tower:

Log in to the Ansible Automation Platform console.
Navigate to the Credentials tab.
Click the Add button and select Machine.
Provide the following details:
Name: A descriptive name for the credential.
Username: The SSH username for authentication.
Password: If using password-based authentication.
SSH Private Key: If using key-based authentication, provide the private key.
Optionally, associate the credential with an organization.
Click Save.
Using Machine Credentials
Once you’ve created the machine credentials, you can use them in your job templates. When defining a job template, select the appropriate machine credential to be used during playbook execution.

Remember that machine credentials are specific to Tower and are separate from environment variables. Tower handles the authentication process, ensuring secure communication with the target hosts.

  • 2,022 Views

Since you already have your SSH Keypair: Just navigate to Resources--> Credentials, add a new one (type: Machine) and then enter your username & paste your SSH Private Key. If you set a passphrase enter that below in the appropriate field and choose your privilege escalation method.

Then you can use this credential in any template either by setting it on your template or choosing to prompt for it upon launch. Also ideally set the Organization associated with the credential and adding Access to other teams/users as needed.

Join the discussion
You must log in to join this conversation.