前提是安装ansible
配置源
需要执行一遍ssh,破除 需要输入yes的问题
修改文件: /etc/ssh/ssh_config
在文件中添加如下信息:StrictHostKeyChecking no
2. 添加 hadoop用户
首先 test用户,对远程服务器可以进行控制
准备hosts文件
hosts
[hadoop_test_hosts]
0.0.0.0 ansible_ssh_user=test ansible_ssh_pass=000
0.0.0.0 ansible_ssh_user=test ansible_ssh_pass=000
0.0.0.0 ansible_ssh_user=test ansible_ssh_pass=000
0.0.0.0 ansible_ssh_user=test ansible_ssh_pass=000
[hadoop_hosts]
0.0.0.0 ansible_ssh_user=hadoop ansible_ssh_pass=000
0.0.0.0 ansible_ssh_user=hadoop ansible_ssh_pass=000
0.0.0.0 ansible_ssh_user=hadoop ansible_ssh_pass=000
0.0.0.0 ansible_ssh_user=hadoop ansible_ssh_pass=000
准备添加hadoop playbook
useradd.yml
---
- hosts: hadoop_test_hosts
remote_user: test
sudo: yes
vars:
user: hadoop
tasks:
- name: add user
action: user name={{ user }} home=/home/{{ user }}
tags:
- user
ansible-playbook -i hosts useradd.yml
准备修改hadoop密码
change_pass_hadoop.yml
---
- hosts: hadoop_test_hosts
gather_facts: False
remote_user: test
sudo: yes
tasks:
- name: change user
user: name={{ item.name }} password={{ item.new_pass | password_hash('sha512') }} update_password=always
with_items:
- { name: 'hadoop',new_pass: '000' }
hadoop用户下 执行 ssh-keygen 生成 ip_rsa.pub文件(每台hadoop上都需要)
ansible-playbook -i hosts change_pass_hadoop.yml
准备 hadoop ssh无秘钥 playbook
rsync_key.yml
- hosts: hadoop_hosts
remote_user: hadoop
tasks:
- name: copy ssh key
authorized_key:
user: hadoop
key: "{{ lookup('file', '/home/hadoop/.ssh/id_rsa.pub') }}"
ansible-playbook -i hosts rsync_key.yml