cannot run ansible to AWS EC2 created by terraform - amazon-ec2

I have EC2 created by terraform, and I can login the ec2 using:
ssh -vvvv -i /home/ec2-user/.ssh/mykey.pub ec2-user#XX.XX.XX.XX without password,(XX.XX.XX.XX) is the IP of the EC2 created by terraform.
but when I try to run ansible file in terraform when ec2 is created, ansible cannot run and error message is:
aws_instance.dev (local-exec): TASK [Gathering Facts]
*********************************************************
The authenticity of host 'XX.XX.XX.XX (XX.XX.XX.XX)' can't be
established.
...
Are you sure you want to continue connecting (yes/no)?
aws_instance.dev: Still creating... (6m40s elapsed)
note the ansible yml is started after I manually force the terraform to sleep for 6m and at that time, the EC2 already started (I can login it myself, although it showed "aws_instance.dev: Still creating...") i.e.
resource "aws_instance" "dev" {
...
provisioner "local-exec" {
command = "sleep 6m && ansible-playbook -i hosts myansible.yml"
}
...
}
I run the terraform as ec2-user, I set ansible yml as:
remote_user: ec2-user
become_user: ec2-user
what is the reason the ansible cannot ssh to the EC2?

There is a message for you:
The authenticity of host 'XX.XX.XX.XX (XX.XX.XX.XX)' can't be
established.
...
Are you sure you want to continue connecting (yes/no)?
Either execute ssh-keyscan XX.XX.XX.XX before executing ansible-playbook, or disable host key checking in ansible.

Related

SSH permission denied for ec2 using Ansible

I am new to Ansible and was tring to figure out how to establish a ssh connection via ansible.
I have created an ec2 instance(Linux) and i have controlling node as my local machine(Ubuntu).
Now, I have generated and added key from my local to remote instance. I am also able to ssh by running ssh ec2-user#.
But, when doing the same using ansible command
ansible -i hosts all -m ping -u ec2-user
I am getting error as:
ec2-user#13.127.222.56 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: ec2-user#13.127.222.56: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).",
"unreachable": true
}
My hosts file is:
[test]
13.127.222.56
Now, I am unable to figure out how to work with this further. I am missing some authentication required by ansible because this usually comes only when ssh is unable to get the key.

SSH Authenticity issue when using Ansible as Terraform provisioner

I am trying to use an Ansible playbook as provisioner for my Terraform project, but I get a SSH authenticity message and it hangs forever.
The authenticity of host 'xxxx' can't be established.
ECDSA key fingerprint is SHA256:xxxx.
Are you sure you want to continue connecting (yes/no/[fingerprint])?
I turned it off on my ansible.cfg file, but it doesn't seem to help.
host_key_checking = False
Any ideas on how to fix it?

/ect/ansible file is not available in Mac OS

I used pip to install Ansible in MacOS. But I cannot find the /etc/ansible folder. Neither the inventory file.
I want to run my playbook in minikube environment. But the playbook returns,
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
[WARNING]: Could not match supplied host pattern, ignoring: 192.168.99.105
How to solve this issue?
I looked into this matter and using Ansible for managing minikube is not an easy topic. Let me elaborate on that:
The main issue is cited below:
Most Ansible modules that execute under a POSIX environment require a Python interpreter on the target host. Unless configured otherwise, Ansible will attempt to discover a suitable Python interpreter on each target host the first time a Python module is executed for that host.
-- Ansible Docs
What that means is that most of the modules will be unusable. Even ping
Steps to reproduce:
Install Ansible
Install Virtualbox
Install minikube
Start minikube
SSH into minikube
Configure Ansible
Test
Install Ansible
As the original poster said it can be installed through pip.
For example:
$ pip3 install ansible
Install VirtualBox
Please download and install appropriate version for your system.
Install minikube
Please follow this site: Kubernetes.io
Start minikube
You can start minikube by invoking command:
$ minikube start --vm-driver=virtualbox
Parameter --vm-driver=virtualbox is important because it will be useful later for connecting to the minikube.
Please wait for minikube to successfully deploy on the Virtualbox.
SSH into minikube
It is necessary to know the IP address of minikube inside the Virtualbox.
One way of getting this IP is:
Open Virtualbox
Click on the minikube virtual machine for it to show
Enter root for account name. It should not ask for password
Execute command: $ ip a | less and find the address of network interface. It should be in format of 192.168.99.XX
From terminal that was used to start minikube please run below command:
$ minikube ssh
Command above will ssh to newly created minikube environment and it will store a private key in location:
HOME_DIRECTORY .minikube/machines/minikube/id_rsa
id_rsa will be needed to connect to the minikube
Try to login to minikube by invoking command:
ssh -i PATH_TO/id_rsa docker#IP_ADDRESS
If login has happened correctly there should be no issues with Ansible
Configure Ansible
For using ansible-playbook 2 files will be needed:
Hosts file with information about hosts
Playbook file with statements what you require from Ansible to do
Example hosts file:
[minikube_env]
minikube ansible_host=IP_ADDRESS ansible_ssh_private_key_file=./id_rsa
[minikube_env:vars]
ansible_user=docker
ansible_port=22
The ansible_ssh_private_key_file=./id_rsa will tell Ansible to use ssh key from file with correct key to this minikube instance.
Note that this declaration will need to have id_rsa file in the same location as rest of the files.
Example playbook:
- name: Playbook for checking connection between hosts
hosts: all
gather_facts: no
tasks:
- name: Task to check the connection
ping:
You can test the connection by invoking command:
$ ansible-playbook -i hosts_file ping.yaml
Above command should fail because there is no Python interpreter installed.
fatal: [minikube]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "module_stderr": "Shared connection to 192.168.99.101 closed.\r\n", "module_stdout": "/bin/sh: /usr/bin/python: No such file or directory\r\n", "msg": "The module failed to execute correctly, you probably need to set the interpreter.\nSee stdout/stderr for the exact error", "rc": 127}
There is a successful connection between Ansible and minikube but there is no Python interpreter to back it up.
There is a way to use Ansible without Python interpreter.
This Ansible documentation is explaining the use of raw module.

Ansible "Failed to connect to host via ssh: Permission denied" when trying -m ping all

I've been at this all morning, and I've seen multiple posts about this error in other places, but none of the solutions seem to be working for me at this point.
I'm playing around with Ansible and trying to setup my inventory + hosts for this project.
For my hosts file I have (/etc/ansible/hosts):
[test-servers]
x.x.x.x
For my inventory I have (/etc/ansible/inventory.txt):
ansible-target ansible_ssh_host=x.x.x.x ansible_user=user ansible_ssh_pass=password
But when I try to run ansible -m ping all I get:
x.x.x.x | UNREACHABLE! => {
"changed": false,
"msg": Failed to connect to the host via ssh: user#x.x.x.x: Permission denied (publickey,password,keyboard-interacgive.\r\n"
"unreachable":true
}
However, I get a pong response successfully when I run:
ansible x.x.x.x -m ping -i /etc/ansible/inventory.txt
Anyone have any idea what this might be?
Thanks in advance.
Found the solution. Needed to have it prompt me for a password to setup the initial Ansible SSH configuration.
Ansible: Permission denied (publickey, password)

Unable to connect to AWS instance even after manually adding in public key to authorized_keys

I am unable to run an ansible-playbook or use ansible ping on a AWS instance. However, I can ssh into the instance with no problem. My hosts file is this:
[instance]
xx.xx.xxx.xxx ansible_ssh_user=ubuntu ansible_ssh_private_key_file=/home/josh/Ansible/Amazon/AWS.pem
Should I not use a direct path. I am trying to use ansible to install apache onto the server. In my security group in the AWS console, I allowed all incoming ssh traffic in port 22, and ansi
service: name=apache2 state=started`ble tries to ssh through port 22 so that should not be the problem. Is there some crucial idea behind sshing into instances that I didn't catch onto to. I tried following this post: Ansible AWS: Unable to connect to EC2 instance but to no avail.
make sure inside ansible.cfg ***
private_key_file = path of private key(server-private-key)
and in host machine don't change default authorized_keys file ,better way is create one user, for that user create .ssh directory and then inside create a file called authorized_keys & paste your server-public key
$~/.ssh/authorized_keys
try: ansible-playbook yourplaybookname.yml --connection=local
ansible defaults to ssh

Resources