Linux 使用ansible配置集羣間互信

安裝pip
$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py   # 下載安裝腳本
$ sudo python get-pip.py    # 運行安裝腳本


wget https://files.pythonhosted.org/packages/d8/b5/5c5eb96463427da7b7206ff22a3470aae374656de2317eaac41dfb69a64a/ansible-2.8.2.tar.gz

cd /usr/local/src

tar -xzvf ansible-2.8.2.tar.gz

cd ansible-2.8.2

python setup.py install

 

ansible --version ansible-2.8.2



 

[mysql@ansible-server ansible]$ cat hosts 
[root-test]
10.200.22.203
10.200.22.15
10.200.22.128
10.200.22.222

[mysql-test]
10.200.22.15 ansible_ssh_user=mysql ansible_ssh_pass='abc123'
10.200.22.128 ansible_ssh_user=mysql ansible_ssh_pass='abc123'
10.200.22.222 ansible_ssh_user=mysql ansible_ssh_pass='abc123'

[all:vars]
username = mysql


[mysql@ansible-server ansible]$ cat ssh_root.yml 
- hosts: root-test
tasks:
- name: enforce env
shell: source /etc/profile
run_once: true
- name: create user
user: name=mysql password="{{'abc123'|password_hash('sha512')}}" shell=/bin/bash createhome=yes
- name: close ssh check #關閉初次訪問提示詢問
shell: sed -i "s/^.*StrictHostKeyChecking.*$/ StrictHostKeyChecking no/g" /etc/ssh/ssh_config

[mysql@ansible-server ansible]$ cat ssh_mysql.yml 
- hosts: mysql-test
gather_facts: no
tasks:
- name: delete /home/mysql/.ssh/
file: path=/home/mysql/.ssh/ state=absent
- name: create /home/mysql/.ssh/ dir
file: path=/home/mysql/.ssh/ mode=755 state=directory owner=mysql group=mysql
- name: generating public/private rsa key pair #生成公鑰和私鑰
shell: ssh-keygen -t rsa -b 2048 -N '' -f /home/mysql/.ssh/id_rsa
- name: delete /tmp/ssh/ dir
file: path=/tmp/ssh/ state=absent
connection: local
run_once: true
- name: create /tmp/ssh/ dir
file: path=/tmp/ssh/ state=directory
run_once: true
- name: fetch copy #從各宿主機將公鑰拷貝到本機
fetch: src=/home/mysql/.ssh/id_rsa.pub dest=/tmp/ssh/
- name: append file authorized_keys.log #將各個公鑰合併成一個文件
shell: find /tmp/ssh/* -type f -exec sh -c 'cat {}>>/tmp/ssh/authorized_keys.log' \;
connection: local
run_once: true
- name: copy authorized_keys #將合成的公鑰進行分發
copy: src=/tmp/ssh/authorized_keys.log dest=/home/mysql/.ssh/authorized_keys mode=644
tags:
- install ssh

 
相關文章
相關標籤/搜索