Ansible是用来处理大批量重复性操作的工具,只需要在一台机器上就可以远程控制所有机器,但前提是必须保证每台机器之间SSH可以相互免密登录。关于Ansible的安装和环境准备请参考Ansible环境的准备。

注: 有关Ansible的所有操作只需在第一台机器上修改和执行,其它机器只需知道IP地址即可。

免密安装机器


172.18.18.120

172.18.18.121

172.18.18.122

配置所有免密机器用户名及密码


编辑/etc/ansible/hosts 文件增加配置如下:

[ssh]
172.18.18.120 ansible_ssh_user=root ansible_ssh_pass=123456
172.18.18.121 ansible_ssh_user=root ansible_ssh_pass=123456
172.18.18.122 ansible_ssh_user=root ansible_ssh_pass=123456

编写yml执行文件

编辑/opt/ansible/sshKey.yml文件如下:

- hosts: ssh
  gather_facts: no

tasks:
    - name: enforce env   
      shell: source /etc/profile
      run_once: true
    - name: close ssh check  #关闭初次访问提示询问   
      shell: sed -i "s/^.*StrictHostKeyChecking.*$/   StrictHostKeyChecking no/g" /etc/ssh/ssh_config
    - 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: delete /tmp/ssh/ dir
      file: path=/tmp/ssh/ state=absent
      run_once: true
    - name: fetch copy  #从各宿主机将公钥拷贝到本机
      fetch: src=/root/.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' \;
      run_once: true
    - name: copy authorized_keys  #将合成的公钥进行分发
      copy: src=/tmp/ssh/authorized_keys.log dest=/root/.ssh/authorized_keys mode=0600
      tags:
       - install ssh

执行免密安装

ansible-playbook  /opt/ansible/sshKey.yml

最新文章

  1. 一鼓作气 博客--第八篇 note8
  2. 关于spring中无法将service注入到servlet中的问题
  3. Java 线程 — AbstractQueuedSynchronizer
  4. 使用Tuple来实现多个Model传送至视图
  5. NOIP2012pj文化之旅[floyd]
  6. 集成学习(Ensembling Learning)
  7. PC/UVa 题号: 110106/10033 Interpreter (解释器)题解 c语言版
  8. 深入探索C++对象模型-5
  9. Python 写网络爬虫思路分析
  10. ACM做题过程中的一些小技巧
  11. MSSQL-最佳实践-Always Encrypted
  12. Topshelf的使用
  13. day2——两数相加
  14. 微信小程序笔记
  15. 数据结构~Sqlserver索引使用的B树
  16. dubbo学习笔记1
  17. TFS2017新特性(一)
  18. 和我一起打造个简单搜索之IK分词以及拼音分词
  19. xxx is not in sudoers file 解决(转)
  20. .NET Runtime version 2.0.50727.xxx 执行引擎错误。 (Fatal Execution Engine Error)

热门文章

  1. 有哪些让人相见恨晚的Python库(一)
  2. 倍增LCA模板
  3. NETCore下IConfiguration和IOptions的用法
  4. Git高级之配置多个SSH key
  5. 匈牙利算法(Kuhn-Munkres)算法
  6. scrapy基本的流程 糗事百科
  7. kindeditor富文本编译器
  8. 1.Java和Python的选择
  9. OGG bi-directional replication for Oracle DB
  10. 基于 Lind.DDD 的 权限管理系统