Question

I got some hosts in my ansible inventory which the ansible server cannot connect to (there is no pubkey deployed).

  • How do I list all of them? (List unreachable hosts)
  • Maybe there is a way to generate an inventory file with all of the hosts?

(the less elegant way is to write a playbook and to copy the command line output, but is there a better way?)

Was it helpful?

Solution

To list them, you can use the ping module, and pipe the output :

ANSIBLE_NOCOWS=1 ansible -m ping all 2>&1  | grep 'FAILED => SSH' | cut -f 1 -d' '

If you want to generate an inventory, you can just redirect the output in a file :

ANSIBLE_NOCOWS=1 ansible -m ping all 2>&1  | grep 'FAILED => SSH' | cut -f 1 -d' ' > hosts_without_key

Then, you can use it later providing the -i switch to ansible commands :

ansible-playbook -i hosts_without_key deploy_keys.yml

If you can ssh using passwords, and assuming you have a key deploying playbook (e.g. deploy_keys.yml), you can issue :

ansible-playbook -i hosts_without_key deploy_keys.yml -kKu someuser

But if the point is to deploy keys on hosts that don't have them, remember Ansible is idempotent. It does no harm to execute the deploy_keys.yml playbook everywhere (it's just a bit longer).

Good luck.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top