How do I get Ansible to ping other AWS servers? - linux

I am using RHEL 7.x as my control server. I have installed Ansible 2.2.2.0. The managed nodes are running CentOS 6. I cannot upgrade Ansible because of an incompatibility.
Without Ansible, I can ping the managed servers from the control server. From the control server I can SSH to the managed nodes without password authentication. With Ansible from the control server, I cannot ping the managed servers. Why cannot I use basic Ansible operations (e.g., ansible -m ping all)?
Here are some details. As root, I run this:
ansible -m ping all -vvvv
I saw this:
| UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: OpenSSH_6.6.1, OpenSSL 1.0.1e-fips 11 Feb 2013\r\ndebug1: Reading configuration data
/etc/ssh/ssh_config\r\ndebug1: /etc/ssh/ssh_config line 56: Applying
options for *\r\npercent_expand: unknown key %C\r\n",
"unreachable": true
So I rebooted.
I tried it again. I saw this:
[WARNING]: scp transfer mechanism failed on [x.y.z.z]. Use
ANSIBLE_DEBUG=1 to see detailed information
x.y.z.z | FAILED! => {
"failed": true,
"msg": "failed to transfer file to Please login as the user \"centos\" rather than the user \"root\"./ping.py:\n\nExecuting:
program /usr/bin/ssh host x.y.z.z, user (unspecified), command scp -v
-t 'Please login as the user \"centos\" rather than the user \"root\"./ping.py'\nOpenSSH_6.6.1, OpenSSL 1.0.1e-fips 11 Feb
2013\r\ndebug1: Reading configuration data
/etc/ssh/ssh_config\r\ndebug1: /etc/ssh/ssh_config line 56: Applying
options for *\r\ndebug1: auto-mux: Trying existing master\r\ndebug2:
fd 3 setting O_NONBLOCK\r\ndebug2: mux_client_hello_exchange: master
version 4\r\ndebug3: mux_client_forwards: request forwardings: 0
local, 0 remote\r\ndebug3: mux_client_request_session:
entering\r\ndebug3: mux_client_request_alive: entering\r\ndebug3:
mux_client_request_alive: done pid = 10256\r\ndebug3:
mux_client_request_session: session request sent\r\ndebug1:
mux_client_request_session: master session id: 4\r\nPlease login as
the user \"centos\" rather than the user \"root\".\n" } [WARNING]:
scp transfer mechanism failed on [z.x.y.w]. Use ANSIBLE_DEBUG=1 to see
detailed information
z.x.y.w | FAILED! => {
"failed": true,
"msg": "failed to transfer file to Please login as the user \"centos\" rather than the user \"root\"./ping.py:\n\nExecuting:
program /usr/bin/ssh host z.x.y.w, user (unspecified), command scp -v
-t 'Please login as the user \"centos\" rather than the user \"root\"./ping.py'\nOpenSSH_6.6.1, OpenSSL 1.0.1e-fips 11 Feb
2013\r\ndebug1: Reading configuration data
/etc/ssh/ssh_config\r\ndebug1: /etc/ssh/ssh_config line 56: Applying
options for *\r\ndebug1: auto-mux: Trying existing master\r\ndebug2:
fd 3 setting O_NONBLOCK\r\ndebug2: mux_client_hello_exchange: master
version 4\r\ndebug3: mux_client_forwards: request forwardings: 0
local, 0 remote\r\ndebug3: mux_client_request_session:
entering\r\ndebug3: mux_client_request_alive: entering\r\ndebug3:
mux_client_request_alive: done pid = 10259\r\ndebug3:
mux_client_request_session: session request sent\r\ndebug1:
mux_client_request_session: master session id: 4\r\nPlease login as
the user \"centos\" rather than the user \"root\".\n" }
I then assumed the Linux user "centos" (su centos). I then tried the ansible commands again. I ran this command:
ansible -m ping all -vvvv
I saw this:
x.y.z.z | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: OpenSSH_6.6.1, OpenSSL 1.0.1e
-fips 11 Feb 2013\r\ndebug1: Reading configuration data /etc/ssh/ssh_config\r\nd
ebug1: /etc/ssh/ssh_config line 56: Applying options for *\r\ndebug1:
auto-mux:
Trying existing master\r\ndebug1: Control socket
\"/home/centos/.ansible/cp/ansi
ble-ssh-x.y.z.z-22-centos\" does not exist\r\ndebug2: ssh_connect:
needpri
v 0\r\ndebug1: Connecting to x.y.z.z [x.y.z.z] port 22.\r\ndebug2: f
d 3 setting O_NONBLOCK\r\ndebug1: fd 3 clearing O_NONBLOCK\r\ndebug1:
... partially removed because it "looked like spam"
est\r\ndebug2: we sent a publickey packet, wait for reply\r\ndebug1:
Authenticat
ions that can continue:
publickey,gssapi-keyex,gssapi-with-mic\r\ndebug1: Trying
private key: /home/centos/.ssh/id_dsa\r\ndebug3: no such identity:
/home/centos
/.ssh/id_dsa: No such file or directory\r\ndebug1: Trying private key:
/home/cen
tos/.ssh/id_ecdsa\r\ndebug3: no such identity:
/home/centos/.ssh/id_ecdsa: No su
ch file or directory\r\ndebug1: Trying private key:
/home/centos/.ssh/id_ed25519
\r\ndebug3: no such identity: /home/centos/.ssh/id_ed25519: No such
file or dire
ctory\r\ndebug2: we did not send a packet, disable method\r\ndebug1:
No more aut
hentication methods to try.\r\nPermission denied
(publickey,gssapi-keyex,gssapi-
with-mic).\r\n",
"unreachable": true }
My ansible.cfg file looks like this:
[defaults]
host_key_checking = False
library = ../extra_modules
roles_path = ../roles
pipelining = True
remote_user = centos
forks = 20
log_path = ./ansible.log
[ssh_connection]
control_path = ~/.ssh/ansible-ssh-%%C
What is wrong? Why cannot I ping Ansible managed nodes?

Can you please share your Ansible hosts/inventory file and.ssh folder (ls ~/.ssh)?
Also please try do to something like that and passing the ssh private key and the user name variables via cli:
ansiblie.cfg
[ssh_connection]
pipelining = True
ssh_args = -o ControlMaster=auto -o ControlPersist=30m -o StrictHostKeyChecking=no
control_path = /tmp/ansible-ssh-%%h-%%p-%%r
command:
ansible -m ping all -i <inventory_file> --private-key=~/.ssh/<your pem key.pem> -u <login user ubuntu/centos>

Related

why I get unreachable host with ansible?

I have two machine, local host ubuntu, and client centOS 7.
I have added those lines into: /etc/ansible/hosts
[linux]
192.168.122.1
[linux:vars]
ansible_user=user
ansible_password=123456
and changed uncommented 1 line in ansible.cfg:
# uncomment this to disable SSH key host checking
host_key_checking = False
Still when I do:
ansible all -m ping
192.168.122.1 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: ssh: connect to host 192.168.122.1 port 22: Connection timed out",
unreachable": true
}
what should I do ?
the password and user in hosts file is what i use to log into the other machine
Check your firewalls and selinux. Port 22 is not accepting the connection. It's not a password issue, it's a networking issue.

ImportError: No module named influxdb. Failed to import the required Python library (influxdb)

I have problem to manage influxdb through ansible using "influxdb_database" module. Even though it printouts error about python dependency, it is failing only when container where ansible playbook runs is run on diff VM from one where influxdb is hosted. I run ansible playbook from docker container, and if I run container on the host where influxdb is installed, it works fine - it is managing to create db. But, when same container (created from same image as one mentioned before) runs on different VM from one where influxdb is hosted, it is failing with error pasted below. So I am confused now with the error about python dependency and do not understand where the problem is.
Ansible playbook:
hosts: "tag_deployment_sysmiromis:&tag_service_tick_yes"
user: centos
become: yes
tasks:
- name: Install InfluxDB package
yum: name="influxdb-{{ frame_tick_influxdb_package_version }}" state=present disable_gpg_check=yes
register: frame_yum_run
retries: 10
until: frame_yum_run is success
- name: Restrict InfluxDB user login
user:
name: "influxdb"
group: "influxdb"
shell: /sbin/nologin
- name: Enable InfluxDB service
systemd:
name: influxdb
enabled: yes
state: started
- name: Create InfluxDB data directory
file:
path: "{{ frame_tick_influxdb_data_directory }}"
owner: influxdb
group: influxdb
state: directory
mode: 0750
- name: Create database
influxdb_database:
hostname: localhost
database_name: miroslav
Ansible log on failed task
TASK [Create database] ***********************************************************************************************************************************************************
task path: /app/lib/ansible/playbooks/influx.yml:6
Using module file /usr/lib/python3.8/site-packages/ansible/modules/database/influxdb/influxdb_database.py
Pipelining is enabled.
<10.246.44.196> ESTABLISH SSH CONNECTION FOR USER: centos
<10.246.44.196> SSH: EXEC ssh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="centos"' -o ConnectTimeout=30 -o ControlPath=/root/.ansible/cp/be4c96d801 10.246.44.196 '/bin/sh -c '"'"'sudo -H -S -n -u root /bin/sh -c '"'"'"'"'"'"'"'"'echo BECOME-SUCCESS-brzvkupumuacfsjirccgazqszuzzfwwx ; /usr/bin/python'"'"'"'"'"'"'"'"' && sleep 0'"'"''
Escalation succeeded
<10.246.44.196> (1, b'\n{"msg": "Failed to import the required Python library (influxdb) on frame-tick10-246-44-196\'s Python /usr/bin/python. Please read module documentation and install in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter", "failed": true, "exception": "Traceback (most recent call last):\\n File \\"/tmp/ansible_influxdb_database_payload_IrdxhN/ansible_influxdb_database_payload.zip/ansible/module_utils/influxdb.py\\", line 23, in <module>\\n from influxdb import InfluxDBClient\\nImportError: No module named influxdb\\n", "invocation": {"module_args": {"username": "root", "retries": 3, "use_udp": true, "proxies": {}, "database_name": "miroslav", "hostname": "localhost", "udp_port": 4444, "ssl": false, "state": "present", "timeout": null, "password": "root", "validate_certs": true, "port": 8086}}}\n', b'OpenSSH_8.1p1, OpenSSL 1.1.1g 21 Apr 2020\r\ndebug1: Reading configuration data /etc/ssh/ssh_config\r\ndebug2: resolve_canonicalize: hostname 10.246.44.196 is address\r\ndebug1: auto-mux: Trying existing master\r\ndebug2: fd 3 setting O_NONBLOCK\r\ndebug2: mux_client_hello_exchange: master version 4\r\ndebug3: mux_client_forwards: request forwardings: 0 local, 0 remote\r\ndebug3: mux_client_request_session: entering\r\ndebug3: mux_client_request_alive: entering\r\ndebug3: mux_client_request_alive: done pid = 2147\r\ndebug3: mux_client_request_session: session request sent\r\ndebug3: mux_client_read_packet: read header failed: Broken pipe\r\ndebug2: Received exit status from master 1\r\n')
<10.246.44.196> Failed to connect to the host via ssh: OpenSSH_8.1p1, OpenSSL 1.1.1g 21 Apr 2020
debug1: Reading configuration data /etc/ssh/ssh_config
debug2: resolve_canonicalize: hostname 10.246.44.196 is address
debug1: auto-mux: Trying existing master
debug2: fd 3 setting O_NONBLOCK
debug2: mux_client_hello_exchange: master version 4
debug3: mux_client_forwards: request forwardings: 0 local, 0 remote
debug3: mux_client_request_session: entering
debug3: mux_client_request_alive: entering
debug3: mux_client_request_alive: done pid = 2147
debug3: mux_client_request_session: session request sent
debug3: mux_client_read_packet: read header failed: Broken pipe
debug2: Received exit status from master 1
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_influxdb_database_payload_IrdxhN/ansible_influxdb_database_payload.zip/ansible/module_utils/influxdb.py", line 23, in <module>
from influxdb import InfluxDBClient
ImportError: No module named influxdb
fatal: [10.246.44.196]: FAILED! => {
"changed": false,
"invocation": {
"module_args": {
"database_name": "miroslav",
"hostname": "localhost",
"password": "root",
"port": 8086,
"proxies": {},
"retries": 3,
"ssl": false,
"state": "present",
"timeout": null,
"udp_port": 4444,
"use_udp": true,
"username": "root",
"validate_certs": true
}
}
}
MSG:
Failed to import the required Python library (influxdb) on frame-tick10-246-44-196's Python /usr/bin/python. Please read module documentation and install in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter
sounds like you're dealing with the same issue as me. I was struggling to find out what was wrong then I read the requirements and saw this. I'm using a newer version of influxdb than this module supports so I get the same error as you
Requirements
The below requirements are needed on the host that executes this module.
python >= 2.6
influxdb >= 0.9 & <= 1.2.4
requests
https://docs.ansible.com/ansible/latest/modules/influxdb_database_module.html
UPDATE:
I have been able to find a way to interact with influx DB using the api instead of the modules since they don't work. This involves editing the influxdb.conf to enable using the API
# modifying the influxdb.conf is required to be able to use the influxDB API
- name: Enable http
lineinfile:
path: /etc/influxdb/influxdb.conf
regexp: 'Determines whether HTTP endpoint is enabled.'
line: ' enabled = true'
- name: Enable bind address :8086
lineinfile:
path: /etc/influxdb/influxdb.conf
regexp: '# bind-address = ":8086"'
line: ' bind-address = ":8086"'
- name: Restart influxdb,
systemd:
name: influxdb
state: restarted
- name: Create influxDB database via api
uri:
url: "http://localhost:8086/query"
method: POST
body: 'q=CREATE DATABASE "grafanadb"'
body_format: form-urlencoded
- name: create root user in influxdb
uri:
url: "http://localhost:8086/query"
method: POST
body: "q=CREATE USER user WITH PASSWORD 'pass' WITH ALL PRIVILEGES"
- name: create grafana user in influxdb
uri:
url: "http://localhost:8086/query"
method: POST
body: "q=CREATE USER grafana WITH PASSWORD 'grafana'"
- name: Grant all privileges to grafana user on grafanadb
uri:
url: "http://localhost:8086/query"
method: POST
body: "q=GRANT ALL ON grafanadb TO grafana"
body_format: form-urlencoded
In order to get influxdb_database module working you have to make sure you have influxdb-python installed on your machine. It is also recommended to have influxdb installed as you might want to directly access the database from the command line.
For CentOS7/RHEL7 installations this can be done as follows:
yum install python-pip
pip install influxdb
CentOS8/RHEL8:
dnf install python3-pip
pip3 install influxdb
Note: You have to use a different python version as the default python interpreter is different for CentOS7 and 8. Therefore influxdb_database python interpreter will be different too.
Therefore, the playbook would look something like this:
- name: Install applications for CentOS 7
yum:
name:
- influxdb
- python-pip
- name: Install applications for CentOS 8
yum:
name:
- influxdb
- python3-pip
- name: Install required pip packages
pip:
name:
- influxdb
For debian/ubuntu setups you might do the following:
apt-get install python-influxdb
or
- name: Install applications for CentOS
apt:
name:
- python-influxdb
If you are trying to connect to a remote InfluxDB you should ensure that you are authenticating over SSL. You have to manually enable this as it is not enabled by default.
This is what a remote influx database creation would thus look like:
- name: Create database using custom credentials
influxdb_database:
hostname: "{{influxurl}}"
username: "{{influxusername}}"
password: "{{influxpassword}}"
database_name: "{{influxdbv7}}"
port: "{{influxport}}"
ssl: yes
validate_certs: yes
Note: I have tested this setup with CentOS7/8. It possibly works fine with Ubuntu/Debian setups too. For some reason CentOS7 required me to disable validate_certs, otherwise it fails. Possibly its a bug.
Tested version:
Database: InfluxDB version 1.8.3
Ansible: version 2.9
What I was missing is influxdb installed in targeted host.Once it is installed, influxdb ansible module start working fine.
Also struggled with this issue. Downgrading the version of the requests python package helped me.
pip install requests==2.6.0
(2.25.1 did not work for me)

ssh command is not working while connecting linux to solaris

I am trying to connect to remote solaris machine from a linux server using ssh but not able to connect to the solaris machine. I am using below ssd command to connect to the solaris machine
ssh <host_name>
After giving this command, I am not getting any prompt for username and password. Is it the limitation for linux to solaris connection ??
The output is:
root#host> ssh -v user#solaris_host
OpenSSH_5.3p1, OpenSSL 1.0.0-fips 29 Mar 2010
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug1: Connecting to solaris_host [solaris_host] port 22.
debug1: connect to address solaris_host port 22: Connection timed out
ssh: connect to host solaris_host port 22: Connection timed out
Go over following steps
Check the network connectivity with your target, e.g. ping.
Check if the port 22 is open on your remote host e.g. nmap -A 192.168.0.5/32 -p 22
Check if ssh daemon is running on your target svcs ssh
Come back, when the problem still exists.

Getting Connection refused error when using scp on VM

I have a virtual linux build running on qemu (It runs drop bear as ssh client.) and I am trying to copy some modules I wrote to it using scp using the following command:
scp -vvv -p 2222 wd/day10/int_mod.ko root#localhost:/lib/modules/3.13.5/int_mod.ko
And I get Connection refused error more specifically (I forwarded 2222 to 22 of virtual machine.):
Executing: program /usr/bin/ssh host localhost, user root, command scp -v -p -d -t /lib/modules/3.13.5/int_mod.ko
OpenSSH_6.6.1, OpenSSL 1.0.1f 6 Jan 2014
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug2: ssh_connect: needpriv 0
debug1: Connecting to localhost [127.0.0.1] port 22.
debug1: connect to address 127.0.0.1 port 22: Connection refused
ssh: connect to host localhost port 22: Connection refused
lost connection
What I don't understand is I can easily connect to ssh using
ssh -p 2222 root#localhost
I can connect without any problem.
ssh and scp use different options for specifying the port. From the ssh man page:
[-p port]
From the scp man page:
[-P port]
scp uses capital P. Notice how your debug output says port 22 connection refused when you are trying to connect to port 2222.
Problem is that , ssh package is not installed completely!
you must install ssh and openssh-client so on... with Synaptic Package Manager!
in Synaptic Package Manager , first type ssh in search filter and mark ssh. then click on Apply Button to install ssh services for server and client.
by this way your problem will fixed 100 Percentage!!!
see bellow Please:

Cannot connect to Azure Ubuntu VM - Public Key Denied

We have been using Ubuntu VM's on Azure for some time now and rarely had a any problems. However, one of the VMs has gone bonkers lately. Out of the blue, the Ubuntu VM starts rejecting the public key -
ssh -i ~/azure.key abc#xyz.cloudapp.net
Permission denied (publickey).
Verbose gives me even more confusing signs -
~$ ssh -i -v -v -v ~/azure.key abc#xyz.cloudapp.net
Warning: Identity file -v not accessible: No such file or directory.
OpenSSH_5.9p1 Debian-5ubuntu1.1, OpenSSL 1.0.1 14 Mar 2012
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug2: ssh_connect: needpriv 0
ssh: Could not resolve hostname /home/abc/azure.key: Name or service not known
Wondering if anyone saw this problem or can suggest ideas/solutions?
How about the following?
$ ssh -i ~/azure.key -v -v -v abc#xyz.cloudapp.net

Resources