zoukankan      html  css  js  c++  java
  • ansible 开源批量管理服务器工具

    Ansible 是一款基于 Python 开发的自动化运维工具,可以进行配置管理、批量部署等功能。对于机器较多的场景,可以使用 Ansible 来免去重复敲命令的烦恼。

    1. 安装ansible
      yum -y install ansible
      配置文件路径/etc/ansible/ansible.cfg
      修改配置参数无需重启ansible
      修改参数:host_key_checking = False (不修改可能出现无法连接服务器的情况)
      提示:Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this.  Please add this host's fingerprint to your known_hosts file to manage this host.
    2. 创建需要管理的服务器列表iplist
      默认的iplist为:/etc/ansible/hosts
      [group] 是组的别名,可以通过该组名对其下的所有机器进行控制
      每一行内容分别是:机器别名、机器 IP、ssh 访问时使用的用户名、ssh 访问时使用的端口、ssh 访问时使用的密码
      格式如下:
      [web]
      web_Server_0001 ansible_ssh_host=127.0.0.1 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass='passwd'
      web_Server_0002 ansible_ssh_host=127.0.0.1 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass='passwd'
      [db]
      db_Server_0001 ansible_ssh_host=127.0.0.1 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass='passwd'
      分别创建1组web服务器,1组db服务器,若密码有特殊字符,一定要使用单引号引起来,否则可能出现无法登陆服务器
    3. 日常脚本使用介绍
      eg:显示列表下所有服务器的hostname
      ansible all -i iplist -m shell -a "hostname"
      all 代表所有组,也可指定组名
      -i 指定iplist文件
      -m 指定运行模块名称,如shell
      -a 模块下可调用的命令
      eg:给列表下服务器安装程序
      ansible all -i iplist -m yum -a "name=vim"
      命令可嵌套,可使用管道符
      eg:

      使用ansible分发文件
      ansible all -i iplist -m copy -a "src=/home/file dest=/home/"

  • 相关阅读:
    我们的回忆

    出差
    恍惚
    七夕
    K8S命令(一)——Node相关
    K8S命令(二)——查询相关
    [转载]为什么你应该(从现在开始就)写博客
    替代JDK日期的开源项目:jodatime
    使用getElementById获取xml中的指定元素
  • 原文地址:https://www.cnblogs.com/Mrhuangrui/p/6698795.html
Copyright © 2011-2022 走看看