前言
Ansible是一款简单的运维自动化工具,只需要使用ssh协议连接就可以来进行系统管理,自动化执行命令,部署等任务
Ansible的特点
- ansible不需要单独安装客户端,也不需要启动任何服务;
- ansible是python中的一套完整的自动化执行任务模块;
- ansible playbook 采用yaml配置,对于自动化任务执行过一目了然;
Ansible组成结构
Ansible
是Ansible的命令工具,核心执行工具;一次性或临时执行的操作都是通过该命令执行。Ansible Playbook
任务剧本(又称任务集),编排定义Ansible任务集的配置文件,由Ansible顺序依次执行,yaml格式。Inventory
Ansible管理主机的清单,默认是/etc/ansible/hosts文件。Modules
Ansible执行命令的功能模块,Ansible2.3版本为止,共有1039个模块。还可以自定义模块。Plugins
插件,模块功能的补充,常有连接类型插件,循环插件,变量插件,过滤插件,插件功能用的较少。API
提供给第三方程序调用的应用程序编程接口。
一、Ansible 安装
1、配置epel源
[root@localhost ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@localhost ~]# yum clean all
[root@localhost ~]# yum makecache
2、安装ansible
[root@localhost ~]# yum install -y ansible
[root@localhost ~]# ansible --version
ansible 2.9.2
二、Ansible Inventory 文件
Ansible Inventory 中文文档
Inventory文件通常用于定义要管理的主机的认证信息
,例如ssh登录用户名、密码以及key相关信息。可以同时操作一个组的多台主机,组与主机组之间的关系都是通过inventory文件配置。配置文件路径为:/etc/ansible/hosts
1、基于密码连接(不推荐)
[root@localhost ~]# vim /etc/ansible/hosts
# 方法一 主机+端口+密码
[webserver]
192.168.182.10 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"
192.168.182.11 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"
192.168.182.12 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"
# 方法二 主机+端口+密码
[webserver]
192.168.182.10[1:3] ansible_ssh_user=root ansible_ssh_pass="123456"
# 方法三 主机+端口+密码
[webserver]
192.168.182.10[1:3]
[webserver:vars]
ansible_ssh_pass="123456"
PS:第一次使用密码连接时,需要使用ssh+密码登录,验证指纹密码,否则会报"msg"错误
PS:ansible的携带密码访问
2、基于秘钥连接(推荐)
基于秘钥连接需要先创建公钥和私钥,并发送给被管理机器
- 将密钥发送至
192.168.182.10
、192.168.182.11
、192.168.182.12
三个主机
[root@localhost ~]# ssh-keygen
[root@localhost ~]# for i in {0,1,2}; do ssh-copy-id -i 192.168.1.1$i ; done
- 配置连接
[root@localhost ~]# vim /etc/ansible/hosts
# 方法一 主机+端口+密钥
[webserver]
192.168.182.10:22
192.168.182.11:22
192.168.182.12:22
# 方法二 主机+端口+密钥
[webserver]
node1 ansible_ssh_host=192.168.182.10 ansible_ssh_port=22
node2 ansible_ssh_host=192.168.182.11 ansible_ssh_port=22
node3 ansible_ssh_host=192.168.182.12 ansible_ssh_port=22
3、临时指定inventory(同基于密码连接,第一次需要使用ssh连接)
- 先编辑一个主机定义清单
[root@localhost ~]# vim /etc/nginx
[Nginxserver]
192.168.182.13 ansible_ssh_pass='123456'
- 测试是否连通
[root@localhost ansible]# ansible nginx -m ping -i /etc/ansible/nginx -o
192.168.182.13 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
4、主机组的使用
# 主机组变量名+主机+密码
[apache]
192.168.182.10
192.168.182.11
[apache.vars]
ansible_ssh_pass='123456'
# 主机组变量名+主机+密钥
[nginx]
192.168.182.12[1:2]
# 定义多个组,把一个组当另外一个组的组员
[webserver:children] #webserver组包括两个子组:apache nginx
apache
nginx
5、测试是否连接成功
[root@localhost ansible]# ansible all -m ping
192.168.182.11 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.182.12 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.182.10 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
三、Inventory 内置参数
参数 | 用途 | 例子 |
---|---|---|
ansible_ssh_host | 定义host ssh地址 | ansible_ssh_host=192.168.182.10 |
ansible_ssh_port | 定义hots ssh端口 | snsible_ssh_port=3009 |
ansible_ssh_user | 定义hosts ssh 认证用户 | ansible_ssh_user=villian |
ansible_ssh_pass | 定义hosts ssh认证密码 | ansible_ssh_pass=123456 |
ansible_duso | 定义hosts sudo的用户 | ansible_sudo=villian |
ansible_sdo_pass | 定义hosts sudo密码 | ansible_sudo_pass=“123456” |
ansible_sudo_exe | 定义hosts duso 路径 | ansible_sudo_exe=/usr/bin/sudo |
ansible_ssh_private_key_file | 定义hosts私钥 | ansible_ssh_private_key_file=/root/key |
ansible_shell_type | 定义hosts shell类型 | ansible_shell_type=bash |
ansible_python_interpreter | 定义hosts任务执行python的路径 | ansible_python_interpreter=/usr/bin/python2.6 |
ansible_interpreter | 定义hosts其他语言解析器路径 | ansible_interpreter=/usr/bin/ruby |
四、Ansible Ad-Hoc
ad-hoc —— 临时的,在ansible中是指需要快速执行,并且不需要保存的命令。说白了就是执行简单的命令——一条命令。对于复杂的命令则为playbook,类似于saltstack的state sls状态文件
1、ansible命令格式
- 常用命令参数
[root@localhost ansible]# ansible -h
Usage: ansible <host-pattern> [options]
-a MODULE_ARGS #模块参数
-C, --check #检查语法
-f FORKS #并发
--list-hosts #列出主机列表
-m MODULE_NAME #模块名字
-o 使用精简的输出
- 示例
[root@localhost ansible]# ansible webserver -m shell -a 'uptime' -o
192.168.182.12 | CHANGED | rc=0 | (stdout) 17:18:20 up 1:38, 2 users, load average: 0.00, 0.01, 0.05
192.168.182.11 | CHANGED | rc=0 | (stdout) 17:18:20 up 1:38, 3 users, load average: 0.00, 0.01, 0.05
192.168.182.10 | CHANGED | rc=0 | (stdout) 17:18:20 up 1:45, 3 users, load average: 0.00, 0.01, 0.05
- 命令说明
2、host-pattern格式
目标target主机,主机组匹配方式
[root@localhost ansible]# ansible 192.168.182.11 -m ping ///一台主机
192.168.182.11 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
[root@localhost ansible]# ansible 192.168.182.11,192.168.182.12 -m ping ///两台主机
192.168.182.11 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.182.12 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
[root@localhost ansible]# ansible all -m ping ///多台主机
192.168.182.11 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.182.12 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.182.10 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
3、组的匹配
组的配置信息如下:这里定义了一个nginx组
和一个apache组
[root@localhost ansible]# ansible nginx --list
hosts (3):
192.168.182.11
192.168.182.12
[root@localhost ansible]# ansible apache --list
hosts (1):
192.168.182.12
192.168.182.13
- 一个组的所有主机匹配
[root@localhost ansible]# ansible nginx -m ping
192.168.182.12 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.182.11 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
- 匹配apache组中有,但是nginx组中没有的所有主机
[root@localhost ansible]# ansible 'apache:!nginx' -m ping -o
192.168.182.13 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
- 匹配apache组和nginx组中都有的机器(并集)
[root@localhost ansible]# ansible 'apache:&nginx' -m ping -o
192.168.182.12 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
- 匹配apache组nginx组两个组所有的机器(并集);等于ansible apache,nginx -m ping
[root@localhost ansible]# ansible 'apache:nginx' -m ping -o
192.168.182.13 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
192.168.182.12 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
192.168.182.11 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
- 匹配所有主机
[root@localhost ansible]# ansible all -m ping
192.168.182.11 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.182.12 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.182.13 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}