一、 ansible 简介
1.ansible 特性
模块化 设计 ,调用特定的模块来完成特定任务 ,本身是核心组件,短小精悍 ;
部署简单,agentless 无客户端工具;
主从模式 工作;
支持自定义模块 功能;
支持playbook 剧本,连续任务按先后设置顺序完成;
2.ansible 架构
ansible core : ansible 自身核心模块
host inventory: 主机库,定义可管控的主机列表
connection plugins: 连接插件,一般默认基于 ssh 协议连接
modules:core modules ( 自带模块 ) 、 custom modules ( 自定义模块 )
playbooks :剧本,按照所设定编排的顺序执行完成安排任务
3. 配置 文件:
(1)ansible 应用程序的 主配置文件:/etc/ansible/ansible.cfg
(2) Host Inventory 定义管控主机 :/etc/ansible/hosts
遵循 INI风格;中括号中的字符是组名;一个主机可同时属于多个组;
示例:
# Ex 1: Ungrouped hosts, specify before any groupheaders. 直接在任何组的头部前面指定,不属于任何组的主机
green.example.com
blue.example.com
192.168.100.1
192.168.100.10
# Ex 2: A collection of hosts belonging to the'webservers' group ;一批主机属于一个组,例如定义为 'webservers' 的组
[webservers]
alpha.example.org
beta.example.org
192.168.1.100
192.168.1.110
注意:默认是以 root 用户执行,但是基于 ssh 连接操作要多次输入密码,为方便可以使用基于ssh 密钥方式进行认证
二、 ansible 应用程序命令
1. ansible-doc命令:获取模块列表,及模块使用格式;
ansible-doc -l :获取列表
ansible-doc -s module_name :获取指定模块的使用信息
2.ansible 命令格式
ansible <host-pattern> [-f forks] [-m module_name] [-a args]
三、ansible 模块介绍
1.command 执行常用命令
ansible all -m command -a '<command>'
2. user 用户相关
ansible test-web -m user -a 'name=ansible state=present'
3. group 用户组
ansible all -m group -a 'name=mygroup state=present system=true'