How to configure users/keys to allow Ansible to run against multiple hosts? - linux

I'm currently using a sandbox environment to help gain an understanding of Linux and Ansible.
I have a rhel 7.6 VM where Ansible is installed/ran from that i connect to via moba. I then have 2 test VMs that i'd like to run Ansible against.
I cannot SSH from the Ansible VM to either of the test VM's (Permission denied public key) but i can connect directly to the test VM's.
How do i set up the keys/hosts? does the private key need to be uploaded to the Ansible VM?

Try to deploy ~/.ssh/id_rsa.pub key from Ansible control machine to one of your VM's in a file ~/.ssh/authorized_keys. Copy the contents of ~/.ssh/id_rsa.pub from the Ansible control machine in ~/.ssh/authorized_keys on the target host. You may use the ssh-copy-id command to perform this for you so long as you have access to the target host via some method.

another method different from best practice id_rsa.pub deployment is configuring inventory vars for your hosts/groups by setting ansible_user, ansible_ssh_pass (with vault usage), ansible_become_user, ansible_become_pass (with vault usage)

Related

Cannot clone a repo from azure devops on Linux VM on windows 11 using ssh

I am trying to clone a repo from Azure DevOps on Linux VM on windows 11 using SSH. It is asking me for a username and password for https://XX.visualstudio.com. The steps are below. Am I missing anything?
Created a VM Ubuntu 18.04 LTS using Hyper V
Installed git
Generated a key pair
added a public key in Azure DevOps
and git clone the repo
I was not able to create and edit ~/.ssh/config, it is not present, some solutions were suggested to edit it.
Please make sure we are using SSH URL when cloning.
Enter your passphrase for SSH key.
For more details about how to generate a new SSH key and set passphrase, you could refer to this doc: https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent

Permission denied lost connection

I have created a simple VM in azure in which I will have to host a very simple server written in C.
To send the folder hosted on my computer containing the server to the virtual machine, I use the command from powershell:
scp -r <path_to_key.pem> <path_to_folder_on_my_pc> <azureuser#ip:/home/azureuser/>
The result of this command is
azureuser#ip: Permission denied (publickey).
lost connection
Would anyone who has had this problem have a solution ?
You need to copy your private key to the ~/.ssh/ directory on the host from which you want to transfer the file. Once you have done that, you can use the following command:
scp -i ~/.ssh/<name of your key>.pem <path of file to transfer> user#azureip:<target directory>
So for example you want to transfer file.txt to your Azure VM (IP of 10.10.10.10) with the private key named key.pem
scp -i ~/.ssh/key.pem file.txt user#10.10.10.10:/home/user/
To pull a file from your Azure VM to your local host, you reverse the order of the file to get and user#azureip.
scp -i ~/.ssh/key.pem user#10.10.10.10:/home/user/file.txt /home/user/
This problem may cause in your public key. please Ensure that the public key is also present in your home directory when you create the Azure Virtual machine with a public key. Meaning The public key was kept on your both local computer and virtual machine Then, with the permission accept from your local workstation, you can use ssh into your Azure Virtual Machine using the public key.
Reference: linux - Can't scp to Azure's VM - by ale93p
Suppose if you want to use the private key in the SCP then you will have to use the below command to copy files from the local system to the Azure VM
sudo scp -i ~/.ssh/id_rsa /path/cert.pem azureuser#ip.xxx.xxx.xxx:/home/file/user/local
Make sure that the Azure VM's incoming NSG rule has port 22 opened and by default VM'S page is reachable through port 80/443 over public IP address.
For more information in detail, please refer this link:
Use SSH keys to connect to Linux VMs - Azure Virtual Machines | Microsoft Docs
Use SCP to move files to and from a VM - Azure Virtual Machines | Microsoft Docs

Scp connection timed out ubuntu VM

so i'm trying to copy a file for my directory to Azure ubuntu VM , SSH works just fine ,but scp command takes a lot of time and then i had this message
connect to host 10.x.x.x port 22: Connection timed out lost connection
this is the command i used :
scp -vvv -i .ssh/id_rsa BaltimoreCyberTrustRoot.crt.pem azureuser#10.x.x.x:/var/www/html
• AFAIK, the SCP command that you are using to try to connect to your Ubuntu Azure VM might not be correct as the correct command to connect to your Ubuntu Linux VM from your local machine to copy files between them is as follows: -
scp -r ./tmp/ azureuser#10.xxx.xxx.xxx:/home/file/user/local
In the above command, the SCP connection gets established successfully after entering the private key further which files in the local system in ‘/tmp’ directory is recursively getting copied in the Azure ubuntu VM specified in ‘/home/file/user/local’ directory. Thus, the whole directory tree as specified is copied from the local system to the Azure ubuntu VM.
• Also, if you want to use the private key in the ‘SCP’ command through SSH, then you will have to use the below command to copy files from the local system to the Azure ubuntu VM: -
sudo scp -i ~/.ssh/id_rsa /path/cert.pem azureuser#10.xxx.xxx.xxx:/home/file/user/local
Using ‘sudo’ to access a ‘root’ file, while ‘SCP’ is going to look for the identity file ‘id_rsa’ in ‘/root/.ssh/’ instead of in ‘/home/user/.ssh/’. That's why you will have to specify the identity file (private key) in the SCP command to connect to the Azure ubuntu VM and transfer files from local system to the VM.
Other than this, kindly ensure that port 22 is opened in the inbound NSG rule on the Azure ubuntu VM and the VM's default page is accessible on port 80/443 over public IP address and the Azure FQDN assigned.
For more information, kindly refer to the links below: -
Can't scp to Azure's VM
https://learn.microsoft.com/en-us/azure/virtual-machines/linux/copy-files-to-linux-vm-using-scp#scp-a-directory-from-a-linux-vm

Not able to make connection from jenkins to AWS ec2 servers

I am trying to use SSH plugins in Jenkins to execute the script on the remote host server. I have added the remote user credentials on Jenkins but not able to make the connection on AWS ec2 instance.
screenshot of the error:
Am I missing any steps while doing the configurations? How can I resolve this issue?
Some things to consider
Does the Security Group on the Target 3.56.98.1 allow for SSH
access from the Jenkins IP address?
Do you have the Public SSH Key of Jenkins added to the targets authorised keys file?
Have you set the correct username that Jenkins will use in the target ubuntu, ec2-user, other?

Transferring files from my Local windows pc to my Linux VM

SO i am new to tech, and as previous posts suggests i am working with OCI. Currently i run a linux 8 VM on OCI. My goal is to run terraform scrips on the vm, and have the resources created in OCI.
Current problem:
The tf files i will be writing will be done so on my local windows 10 machine. The files will be saved in a local directory. I need a way of transferring these local files to a directory in my linux machine, in order to execute them!
Is anybody good with OCI is there capability for a sftp transfer using winscp?? I'm just not sure where to start. Anybody with good advice please aid me!
It depends of your OCI network configuration.
If your OCI compute VM is in a public subnet and you have an internet gateway, then you can use ssh to connect to it (using putty for instance). That means you can also use scp which lets move copy files over ssh. As you mentioned, WinSCP let's you connect to your OCI compute VM by using ssh and scp or sFTP. After installing it you can create a new connection using the public ip of your OCI compute VM and the private key.
My personal preference is to use MobaXterm to connect to ssh to
my OCI compute VMs. Once connected to a remote host using ssh, the
left pane directly displays a file browser for the remote host.
Drag-and-dropping a file there would initiate an sFTP transfer
automatically.
Please also note that scp is obsolete since 2019. SFTP or rsync could be used instead. Using MobaXterm, it can be done by opening a new terminal tab (which is local to your Windows machine) and type the rsync command you wish for instance rsync -v -P -e 'ssh -i "D:/my_folder/oci_api_key.pem"' /cygdrive/d/my_folder/*.tf opc#<oci_vm_ip>:/home/opc/my_folder
-v is increasing verbosity, to display more information. -P displays partial progress for each file transferred. -e lets you specify which command to use to run rsync. in this case I use ssh and pass the private key. More option are available and you can check them by typing man rsync.
If your OCI compute VM is in a private subnet, you would need to set up a bastion VM in a public subnet to first access the bastion and then the VM. Here is a blog post about how to achieve that using putty and WinSCP : https://www.ateam-oracle.com/ssh-tunnel-to-a-private-vm-using-a-bastion-host-in-oci

Resources