Skip to content

Instantly share code, notes, and snippets.

@hongqn
Created July 27, 2015 02:30
Show Gist options
  • Save hongqn/62d528116eb5a4a9e4b7 to your computer and use it in GitHub Desktop.
Save hongqn/62d528116eb5a4a9e4b7 to your computer and use it in GitHub Desktop.
Ansible playbook to distribute ssh public key to hosts
---
- hosts: all
gather_facts: no
tasks:
- local_action: command grep '{{ ansible_ssh_host }} ' /root/.ssh/known_hosts
register: exists
ignore_errors: True
changed_when: False
- local_action: shell ssh-keyscan -p {{ ansible_ssh_port|default(22) }} {{ ansible_ssh_host }}
register: result
when: exists|failed
- local_action: known_hosts path='/root/.ssh/known_hosts' host={{ ansible_ssh_host }} key="{{ result.stdout }}"
when: exists|failed
- hosts: dmz-vms
tasks:
- yum: pkg=libselinux-python
- hosts: all
tasks:
- authorized_key: user=root key="{{ lookup('file', '/root/.ssh/id_rsa.pub') }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment