导航

Linux 使用ansible配置集群间互信

Posted on 2019-07-12 13:15  许爱琪  阅读(636)  评论(0编辑  收藏  举报
安装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