ProfHase85
ProfHase85

Reputation: 12233

Ansible - List available hosts

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

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

Upvotes: 3

Views: 4767

Answers (1)

leucos
leucos

Reputation: 18279

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.

Upvotes: 3

Related Questions