ansible中文站点:ansible中文网站

一、ansible是自动化运维工具,基于python开发,实现了批量系统配置、批量程序部署、批量运行命令等功能,默认通过SSH协议管理机器。目前需要主机上有python2.6或2.7就可以运行。

  • ansible的特点: --模块化设计 --仅需要ssh和Python即可以使用 --无客户端 --功能强大,模块丰富 --上手容易门槛低 --基于python开发,做二次开发更容易 --使用公司较多,社区活跃
  • 工作流程:
  • 架构: --Ansible: Ansible核心程序。 --HostInventory:记录由Ansible管理的主机信息,包括端口、密码、ip等。
    --CoreModules:核心模块,主要操作是通过调用核心模块来完成管理任务。 --CustomModules:自定义模块,完成核心模块无法完成的功能,支持多种语言。 --ConnectionPlugins:连接插件,Ansible和Host通信使用
  • hosts文件:主机清单文件 环境:6台虚拟机 1 管理机器:ansible 192.168.1.10 2托管机器:web1 192.168.1.11 3托管机器:web2 192.168.1.12 4托管机器:db1 192.168.1.21 5托管机器:db2 192.168.1.22 6托管机器:cache 192.168.1.33
  • 在管理节点配置yum源,安装ansible
  • ansible配置文件是 ansible.cfg
  • ansible查找配置文件的顺序是: 1、ANSIBLE_CONF变量定义的配置文件 2、当前目录下的./ansible.cfg 3、当前用户家目录下的~/ansible.cfg 4、/etc/ansible/ansible.cfg(默认配置文件) ansible.cfg中inventory行定义年托管主机列表文件。
  • 编辑/etc/ansible/hosts文件 格式: [组名称] 主机名称或ip地址,登彔用户名,密码、端口等信息
  • 测试 ansible [组名称] --list-hosts
  • 主机定义与分组 inventory 参数说明 – ansible_ssh_host – 将要连接的远程主机名. – ansible_ssh_port – ssh端口号.如果不是默认的端口号,通过此变量设置. – ansible_ssh_user – 默认的 ssh 用户名 – ansible_ssh_pass – ssh 密码(这种方式并并安全,我们强烈建议使用 --ask-pass 或 SSH 密钥) – ansible_sudo_pass – sudo 密码(建议使用 --ask-sudo-pass) – ansible_sudo_exe (new in version 1.8) – sudo 命令路径(适用于1.8及以上版本) – ansible_connection – 与主机的连接类型.比如:local, ssh 或者 paramiko.Ansible 1.2 以前默认使用 paramiko.1.2 以后默认使用 'smart','smart' 方式会根据是否支持ControlPersist, 来判断'ssh' 方式是否可行. – ansible_ssh_private_key_file – ssh 使用的私钥文件.适用于有多个密钥,而你不想使用SSH 代理的情况. ansible_shell_type --目标系统的shell类型.默认情况下,命令的执行使用 'sh'用法,可设置为 'csh' 或 'fish'. – ansible_python_interpreter – 目标主机的 python 路径.适用于的情况: 系统中有多个Python, 或者命令路径不是"/usr/bin/python”
  • hosts文件编辑:定义一个为web组,db组,cache组, [web] web1 web2 [db] db[1:2]
    [cache] 192.168.1.16 [app1:children] 指定子组 web db [app1:vars] #组的变量 这个组的主机都使用如下配置 ansible_ssh_user="root" ansible_ssh_pass="1"自定义配置文件
  • 自定义分组 – 创建文件夹 myansible – 创建配置文件 ansible.cfg [defaults] inventory = myhost – 配置主机文件 [nginx] 192.168.1.11 192.168.1.12 192.168.1.13 – ansible nginx --list-hosts
  • 动态主机 – Ansible Inventory实际上是包含静态Inventory和动态Inventory两部分,静态Inventory指的是在文件/etc/ansible/hosts中指定的主机和组,DynamicInventory指通过外部脚本获取主机列表,并按照ansible 所要求的格式返回给ansilbe命令的。 • json – JSON的全称是”JavaScript Object Notation”,意思是JavaScript对象表示法,它是一种基于文本,独立于语言的轻量级数据交换格式。 注意事项: – 1、主机部分必须是列表格式的; – 2、hostdata行,其中的"hosts" 部分可以省略,但如 果使用时,必须是"hosts"
#!/bin/bash
echo '
{
    "web"   : ["web1", "web2"],
    "db"    : ["db1", "db2"],
    "other" : ["cache"]
}'
  • ansible命令基础 ansible <host-pattern> [options] – host-pattern 主机或定义的分组 – -M 指定模块路径
    – -m 使用模块,默认 command 模块 – -a or --args 模块参数 – -i inventory 文件路径,或可执行脚本 – -k 使用交互式登彔密码 – -e 定义变量 – -v 详细信息,-vvvv 开启debug 模式
  • 批量检测主机 – ansible all -m ping
  • 批量执行命令 – ansible all -m command -a 'id' -k
  • 批量部署密钥 生成密钥:ssh-keygen -N ‘’ -f /root/.ssh ansible all -m authorized_key -a "user=root exclusive=true manage_dir=true key='$(</root/.ssh/authorized_keys)'" -k 报错 – "msg": "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." – 解决方法: – 修改 ansible.cfg host_key_checking = False
  • 模块 ansible-doc – 模块的手册,相当于 shell 的 man 非常重要,非常重要,非常重要 – ansible-doc -l 列出所有模块 – ansible-doc modulename 查看帮助
  • command模块 – 默认模块,远程执行命令 – 用法 – ansible host-pattern -m command -a '[args]' – 查看所有机器负载 ansible all -m command -a 'uptime' – 查看日期和时间 ansible all -m command -a 'date +%F_%T'模块 • command模块注意事项: – 该模块通过-a跟上要执行的命令可以直接执行,不过命令里如果有带有如下字符部分则执行不成功 – "<", ">", "|", "&" – 该模块不启动shell 直接在 ssh 过程中执行,所有使用到 shell 特性的命令执行都会失败 – 下列命令执行会失败 ansible all -m command -a 'ps aux|grep ssh' ansible all -m command -a 'set' • shell | raw 模块 – shell 模块用法基本和command一样,区别是 shell模块是通过/bin/sh迚行执行命令,可以执行任意命令 – raw模块,用法和shell 模块一样 ,可以执行任意命令 – 区别是 raw 没有chdir、creates、removes参数 – 执行以下命令查看结果 ansible t1 -m command -a 'chdir=/tmp touch f1'
    ansible t1 -m shell -a 'chdir=/tmp touch f2' ansible t1 -m raw -a 'chdir=/tmp touch f3' 在当前目录下执行的 • script模块 – 直接在本地写脚本,然后使用 script 模块批量执行 – ansible t1 -m script -a 'urscript' – 友情提示: 该脚本包含但不限于 shell 脚本,只要指定 Sha-bang 解释器的脚本都可运行