ansible基础使用
ansible的安装 Ansible —基于 Python paramiko开发,分布式,无需客户端,轻量级,配置语法使用 YMAL 及 Jinja2模板语言,更强的远程命令执行操作。 类似的自动化运维工具有很多常用的还有: Puppet: —基于 Ruby 开发,采用 C/S架构,扩展性强,基于SSL,远程命令执行相对较弱. SaltStack: —基于 Python 开发,采用 C/S 架构,相对 puppet 更轻量级,配置语法使用 YMAL,使得配置脚本更简单。 Ansible 工作机制 Ansible 在管理节点将 Ansible 模块通过 SSH 协议(或者 Kerberos、LDAP)推送到被管理端执行,执行完之后自动删除,可以使用 SVN 等来管理自定义模块及编排。 由上面的图可以看到 Ansible 的核心组件组成由 5 个部分组成: Ansible core: 核心; core Modules: 包括 Ansible 自带的核心模块及自定义模块; connect Plugins: 完成模块功能的补充,包括连接插件、邮件插件等; Playbooks: 定义 Ansible 多任务配置文件,由 Ansible 自动执行; host Inventory: 定义 Ansible 管理主机的清单、主机池、主机列表; yum安装ansible yum install ansible (ansible依赖于Python 2.6或更高的版本、paramiko、PyYAML及Jinja2。) 三台主机: 192.168.38.3 192.168.38.4 192.168.38.5 (安装ansible) 使用ssh-keygen生成ssh秘钥,通过ssh-copy-id -i root@192.168.38.3 将秘钥传送到另外两台被管理的主机,测试登录被管理主机。 ansible的参数: -u 远程执行命令的user,默认为root。 -i 指定主机清单,默认为/etc/ansible/hosts。 -m 指定模块的名称,默认command命令模块。 -a 模块的具体参数。 -k 提示输入远程主机的密码。 vim /etc/ansible/hosts 主机清单配置文件。 [test] test组有2台机器。 192.168.38.4 192.168.38.3 也可以用":"来定义一系列连续的主机,下面那样写也是ok的。 [test] 192.168.38.[3:4] [root@192 ansible]# ansible -m ping 'test' ping模块测试,返回pong,代表ok。 192.168.38.3 | SUCCESS => { "changed": false, "ping": "pong" } 192.168.38.4 | SUCCESS => { "changed": false, "ping": "pong" } [root@192 ansible]# ansible -m command -a 'uptime' 'test' -m命令模块,具体执行uptime命令。 192.168.38.4 | SUCCESS | rc=0 >> 19:23:51 up 1:02, 2 users, load average: 0.00, 0.01, 0.05 192.168.38.3 | SUCCESS | rc=0 >> 19:23:51 up 1:02, 2 users, load average: 0.00, 0.02, 0.05 [root@192 ansible]# ansible -u root -m command -a date 'test'-u 指定用户。 192.168.38.3 | SUCCESS | rc=0 >> 2016年 11月 05日 星期六 19:24:45 CST 192.168.38.4 | SUCCESS | rc=0 >> 2016年 11月 05日 星期六 19:24:45 CS ansible组的划分定义,2台主机分别属于2个组,但同时又属于一个大组。 cat /etc/ansible/hosts test1和test2同属于test组。 [test:children] test1 test2 [test1] 192.168.38.3 [test2] 192.168.38.4 [root@192 ansible]# ansible -a date 'test' 192.168.38.3 | SUCCESS | rc=0 >> 2016年 11月 05日 星期六 19:44:25 CST 192.168.38.4 | SUCCESS | rc=0 >> 2016年 11月 05日 星期六 19:44:25 CST [root@192 ansible]# ansible -a date 'test1' 192.168.38.3 | SUCCESS | rc=0 >> 2016年 11月 05日 星期六 19:44:31 CST [root@192 ansible]# ansible -a date 'test2' 192.168.38.4 | SUCCESS | rc=0 >> 2016年 11月 05日 星期六 19:44:34 CST 本文转自青衫解衣 51CTO博客,原文链接:http://blog.51cto.com/215687833/1869847