3. 安装ssh
3.1. 说明
主机与其他节点建立ssh连接并存储秘钥。
3.2. yml脚本
---
- hosts: ssh
gather_facts: no
vars_files:
- ../vars.yml
tasks:
- name: enforce env
shell: source ~/.bashrc
run_once: true
- name: delete /root/.ssh/
file: path=/root/.ssh/ state=absent
- name: generating public/private rsa key pair #生成公钥和私钥
shell: ssh-keygen -t rsa -b 2048 -N '' -f /root/.ssh/id_rsa
- name: view id_rsa.pub #将公钥设置成变量
shell: cat /root/.ssh/id_rsa.pub
register: sshinfo
- set_fact: sshpub={{sshinfo.stdout}}
- name: add ssh record #合并各个节点公钥
local_action: shell echo {{ sshpub }} >> {{ PLAYBOOK_DIR }}/ssh/authorized_keys.j2
- name: copy authorized_keys.j2 to all #分发到各个节点上
template: src={{ PLAYBOOK_DIR }}/ssh/authorized_keys.j2 dest=/root/.ssh/authorized_keys mode=0600
tags:
- install ssh
3.3. 运行ansible-playbook
ansible-playbook -i /etc/ansible/hosts ssh/main.yml
网友评论