Ansible 简单的说是一个配置管理系统(configuration management system)。你只须要可使用 ssh 访问你的服务器或设备就行。它也不一样于其余工具,由于它使用推送的方式,而不是像 puppet 等 那样使用拉取安装agent的方式。你能够将代码部署到任意数量的服务器上!node
Ansible能够帮助咱们完成一些批量任务,或者完成一些须要常常重复的工做。 好比:同时在100台服务器上安装nginx服务,并在安装后启动它们。 好比:将某个文件一次性拷贝到100台服务器上。 好比:每当有新服务器加入工做环境时,你都要为新服务器部署某个服务,也就是说你须要常常重复的完成相同的工做。 这些场景中咱们均可以使用到ansible。
python
PLAYBOOKS:linux
任务剧本(任务集),编排定义Ansible任务集的配置文件,由Ansible顺序依次执行,一般是JSON格式的YML文件nginx
INVENTORY:web
Ansible管理主机的清单,/etc/ansible/hosts正则表达式
MODULES:shell
Ansible执行命令的功能模块,多数为内置模块,也能够自定义,ansible-doc -l可查看模块编程
PLUGINS:json
模块功能的补充,如链接类型插件,循环插件,变量插件,过滤插件等,不经常使用windows
API:
提供第三方程序调用的应用程序编程接口
ANSIBLE:
组合INVENTORY、API、MODULES、PLUGINS的,能够理解为是ansible的命令工具,其为核心执行工具
安装方法有不少,这里仅仅以Centos yum安装为例。
Ansible默认不在标准仓库中,须要用到EPEL源。
#yum install epel-release -y #yum install ansible -y
查看本身安装的版本
#ansible --version ansible 2.8.0.dev0 config file = /etc/ansible/ansible.cfg configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python2.7/site-packages/ansible executable location = /usr/bin/ansible python version = 2.7.5 (default, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
配置文件或者指令 | 描述 |
/etc/ansible/ansible.cfg | 主配置件,配置ansible工做特性 |
/etc/ansible/hosts | 主机清单 |
/etc/ansible/roles/ | 存放角色的目录 |
/usr/bin/ansible | 主程序,临时命令执行工具 |
/usr/bin/ansible-doc | 查看配置文档,模块功能查看工具 |
/usr/bin/ansible-galaxy | 下载/上传优秀代码或者Roles模块的官网平台 |
/usr/bin/ansible-playbook | 定制自动化任务,编排剧本工具 |
/usr/bin/ansible-pull | 远程执行命令的工具 |
/usr/bin/ansible-vault | 文件加密工具 |
/usr/bin/ansible-console | 基于console界面与用户交互的工具 |
Ansible配置文件/etc/ansible/ansible.cfg(通常保持默认)部分参数的讲解
[default]
#inventory = /etc/ansible/hosts #主机列表配置
#library = /usr/share/my_modules/ #库文件存放目录
#remote_tmp = $HOME/.ansible/tmp #临时py命令文件存放在远程主机目录
#local_tmp = $HOME/.ansible/tmp #本地的临时命令执行目录
#forks = 5 #默认并发数5
#sudo_user = root #默认sudo用户
#ask_sudo_pass = True #每次执行ansible命令是否询问ssh密码
#ask_pass = True #链接是提示输入ssh密码
#remote_port = 22 #远程主机的默认端口
#log_pass = /var/log/ansible.log #日志文件路径
#host_key_checking = False #检查对应服务器上的host_key,建议取消注释,也就是不会弹出以下消息:
Are you sure you want to continue connecting (yes/no)?
主机名 | 系统版本 | IP地址 | 功能 |
ansible | CentOS 7.4 | 10.39.2.38 | ansible的主控端 |
node1 | CentOS 7.5 | 10.39.0.49 | 服务器 |
配置ansible主控端的hosts实现解析,或者直接用ip地址
ansible必须经过Inventory来管理主机。Ansible可同时操做属于一个组的多台主机,组和主机之间的关系经过Inventory文件配置
单台主机
node1 > FQDN
10.39.0.49 > IP地址
10.39.0.49:222 > 非标准ssh端口
[webservers] > 定义一个组名
web1 >组内的单台主机
10.39.0.49 >
[dbservers]
10.39.0.49 >一台主机能够是不一样的组,这台主机同时属于[webservers]组
[group:children] > 组嵌套组,group为自定义的组名,children是关键字,固定语法,必须填写
webservers > group组内包含其余的组名
dbservers > group组内包含其余的组名
[webservers]
node[001:006] > 有规律的名称列表
这里表示至关于:
node001
node002
node003
node004
node005
node006
[databases]
db-[a:e] > 定义字母范围的简写模式
这里至关于:
db-a
db-b
db-c
db-d
db-e
一下这两条定义了一台主机的连接方式,而不是读取默认的配置设定
localhost ansible_connection=local
node1 ansible_connection=ssh ansible_ssh_user=root
最后还有一个隐藏的分组,那就是all,表明所有主机,这个是隐式的,不须要写出来的
ansible_ssh_host
将要连接的远程主机名,与你想要设定的主机的别名不一样的话,可经过此变量设置
ansible_ssh_port
ssh端口号,若是不是默认端口号,经过此变量设置,这种业可使用IP:端口 10.39.2.39:222
ansible_ssh_user
默认的ssh用户名
ansible_ssh_pass
ssh密码(这种方式并不安全,建议使用--ask-pass或者ssh密钥)
ansible_sudo_pass
sudo密码(这种方式并不安全,建议使用--ask-sudo-pass)
ansible_sudo_exe
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",好比 \*BSD, 或者 /usr/bin/python 不是 2.X 版本的 Python.咱们不使用 "/usr/bin/env" 机制,由于这要求远程用户的路径设置正确,且要求 "python" 可执行程序名不可为 python之外的名字(实际有可能名为python26).
与 ansible_python_interpreter 的工做方式相同,可设定如 ruby 或 perl 的路径....
下面用几个例子更直观的展现一下:
some_host ansible_ssh_port=222 ansible_ssh_user=manager
aws_host ansible_ssh_private_key_file=/home/example/.ssh/aws.pem
freebsd_host ansible_python_interpreter=/usr/local/bin/python
ruby_module_host ansible_ruby_interpreter=/usr/bin/ruby.1.9.3
很重要的一点,主机清单必需要先配置,
cat /etc/ansible/hosts # This is the default ansible 'hosts' file. # # It should live in /etc/ansible/hosts # # - Comments begin with the '#' character # - Blank lines are ignored # - Groups of hosts are delimited by [header] elements # - You can enter hostnames or ip addresses # - A hostname/ip can be a member of multiple groups # Ex 1: Ungrouped hosts, specify before any group headers. ## 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] 10.39.0.49 ## alpha.example.org ## beta.example.org ## 192.168.1.100 ## 192.168.1.110 # If you have multiple hosts following a pattern you can specify # them like this: ## www[001:006].example.com # Ex 3: A collection of database servers in the 'dbservers' group ## [dbservers] ## ## db01.intranet.mydomain.net ## db02.intranet.mydomain.net ## 10.25.1.56 ## 10.25.1.57 # Here's another example of host ranges, this time there are no # leading 0s: ## db-[99:101]-node.example.com
#ssh-copy-id root@10.39.0.49
#配置单向免密登录
# ansible webservers -m ping #使用ansible对webservers组内的主机进行ping模块测试 10.39.0.49 | SUCCESS => { "changed": false, "ping": "pong" }
是否是很简单呀,Ansible用的指令也不是太多,可使用man ansible和官方网站去查询。http://docs.ansible.com/ansible/latest/
ansible <host-pattern> [-m module_name] [options]
指令 匹配规则的主机抢单 -m 模块名 选项
--version 显示版本
-a 模块参数(若是有)
-m module指定模块,默认为conmand
-v 详细过程 -vv -vvv更详细
--list-hosts显示主机列表,可简写--list
-k --ask-pass提示链接密码,默认Key验证
-K --ask-become-pass 提示使用sudu密码
-C --check 检查,并不执行
-T --timeout=TIMEOUT 执行命令的超时时间,默认10S
-u --user=REMOTE_USER 远程执行的用户
-U SUDO_USER --sudo-user 指定sudo用户
-b --become 代替旧版的sudo切换
ansible-doc:显示模块帮助
ansible-doc [options] [module...]
-a 显示全部模块的文档
-l, --list 列出可用模块
-s, --snippet 显示指定模块的简要说明
例子:#ansible-doc ping
因为ansible的模块有1378个(2.4.2.0),而且一直在持续更新。所以,这个指令必需要掌握的。
#ansible-doc -l |wc -l
1378
通配符
注意用单引号
* 匹配任意字符
#ansible '*' -m ping 等同于 #ansible all -m ping
#ansible '*dns*' -m ping
6-dns-1.hunk.tech | SUCCESS
? 匹配单个字符
#ansible '192.168.7.20?' -m ping
192.168.7.201 | SUCCESS
192.168.7.203 | SUCCESS
192.168.7.202 | SUCCESS
192.168.7.200 | SUCCESS
: 或者
#ansible '192.168.7.201:192.168.7.254' -m ping
192.168.7.201 | SUCCESS
192.168.7.254 | SUCCESS
:& 而且 (逻辑与)
#ansible 'test3:&test' --list
hosts (1):
192.168.7.254
:! 逻辑非。在test3组内,可是并不在test组内
#ansible 'test3:!test' --list > 用到感叹号的时候,记得引号为单引号,不然会被bash解析为历史命令
hosts (2):
192.168.7.200
192.168.7.203
使用正则表达式
~表示后面是正则匹配,注意~后面不能有空格
#ansible '~[67]-(db|dns).*\.hunk.*' --list
hosts (2):
6-dns-1.hunk.tech
7-db-3.hunk.tech
以 ansible webservers -m command -a 'ls -l /' -vvv 这条命令为例,根据显示的信息时行解读
1.加载本身的配置文件,默认/etc/ansible/ansible.cfg
Using /etc/ansible/ansible.cfg as config file
2.匹配主机清单
Parsed /etc/ansible/hosts inventory source with ini plugin
3. 加载指令对应的模块文件,如command,生成.py的文件到本机的临时目录,这个目录就是在/etc/ansible/ansible.cfg定义的
Using module file /usr/lib/python2.7/site-packages/ansible/modules/commands/command.py
PUT /root/.ansible/tmp/ansible-local-2495QkKiC4/tmp4T17Dk TO /root/.ansible/tmp/ansible-tmp-1559646714.7-176279961872658/AnsiballZ_command.py
4.经过ansible将模块或命令生成对应的临时py文件,并将该文件传输至远程服务器的对应执行用户$HOME/.ansible/tmp/ansible-tmp-数字/XXX.PY文件,
这个目录就是在/etc/ansible/ansible.cfg定义的
sftp> put /root/.ansible/tmp/ansible-local-2495QkKiC4/tmp4T17Dk /root/.ansible/tmp/ansible-tmp-1559646714.7-176279961872658/AnsiballZ_command.py\n
5.给文件+x权限
chmod u+x /root/.ansible/tmp/ansible-tmp-1559646714.7-176279961872658/ /root/.ansible/tmp/ansible-tmp-1559646714.7-176279961872658/AnsiballZ_command.py && sleep 0
6.执行并返回结果
/usr/bin/python /root/.ansible/tmp/ansible-tmp-1559646714.7-176279961872658/AnsiballZ_command.py && sleep 0
7.删除临时py文件,sleep 0退出
rm -f -r /root/.ansible/tmp/ansible-tmp-1559646714.7-176279961872658/ > /dev/null 2>&1 && sleep 0
8.关闭远程主机链接
Shared connection to 10.39.0.49 closed.\r\n
绿色:执行成功而且不须要作改变的操做
×××:执行成功而且对目标主机作变动
红色:执行失败