运维之道 | Ansible 安装使用
前言
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 --listhosts (3):192.168.182.11192.168.182.12
[root@localhost ansible]# ansible apache --listhosts (1):192.168.182.12192.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"
}
运维之道 | Ansible 安装使用相关推荐
- 运维之道 | Ansible Module
Ansible Ad-hoc 模式常用模块 Ansible 自动运维工具管理客户端案例操作,由于 Ansible 管理远程服务器基于 SSH,在登录远程服务器执行命令时需要远程服务器的用户名和密码,也 ...
- 运维之道 | Zabbix监控mysql主从同步状态并设置触发微信告警
一.主机规划 服务器 IP zabbix-server 192.168.1.111 mysql-master.zabbix-agent 192.168.1.115 mysql-slave.zabbix ...
- 运维之道 | Redis 集群搭建
Redis 集群搭建 集群中至少应该有奇数个节点,所以搭建集群最少需要3台主机.同时每个节点至少有一个备份节点,所以下面最少需要创建使用6台主机,才能完成Redis Cluster 集群搭建(主节点. ...
- 【大型网站运维之道 天道 人道 运维之道】
大型网站运维之道漫谈 网站运维绝不是某些人眼中安装系统.做几根网线那么简单: 除去应用开发和业务运营之外的保障网站能运转的事儿都可能是运维工作的职责范围.本系列是冯大辉(Fenng)的一个系列博文,从 ...
- 运维自动化之ANSIBLE
成功不易,加倍努力! 运维自动化之ANSIBLE 本章内容 1 自动化运维应用场景 1.1 云计算运维工程师核心职能 1.2 运维职业发展路线 1.3 企业实际应用场景分析 1.3.1 Dev开发环境 ...
- Linux运维之道-基础命令
基础命令 参照 linux运维之道(丁明一) 文章目录 基础命令 一.目录以及文件的基本操作 1. pwd 2.cd 3.ls 4.touch 5.mkdir 6.cp 7.rm 8.mv 9.fin ...
- 运维之道 | 基于 LAMP 环境部署 Discuz 论坛
基于 LAMP 环境部署 Discuz 论坛 LAMP环境部署 一.基于VMware虚拟机部署Centos系统 [root@localhost ~]# cat /etc/redhat-release ...
- 运维之道 | Mysql主从复制+mycat读写分离
运维之道 | Mysql主从复制 + Mycat读写分离 1.什么是读写分离 读写分离,基本的原理是让主数据库处理事务性增.删.改操作(INSERT.UPDATE.DROP),而从数据库处理SELEC ...
- 第20章,运维自动化之ansible
更多内容请点击: Linux学习从入门到打死也不放弃,完全笔记整理(持续更新,求收藏,求点赞~~~~) https://blog.51cto.com/13683480/2095439 第20章,运维自 ...
最新文章
- javascript之基本概念
- 【一起去大厂系列】什么是回表查询?怎么优化回表查询?
- 碎玻璃:诊断生产Cassandra问题
- tableau for循环_Tableau for Data Science and Data Visualization-速成课程
- html实现动态多表单输入,使用javascript动态编辑多个相同的HTML表单
- Java 代码块:静态代码块、构造代码块、构造函数块
- 第二次作业:分布式版本控制系统Git的安装与使用
- 毕业论文自动去重软件,内附软件
- arduino中利用LiquidCrystal内置的scrool函数实现屏幕文字滚动播放
- 爬虫项目实操四、用Scrapy爬取招聘网站的招聘信息
- 码农到架构师视频学习笔记
- 2022年可能会继续的一些品牌设计趋势
- Vue前端开发——微信扫码支付
- ubuntu14.04安裝chromium浏览器的Flash播放插件
- idea中Project JDK is not defind的其中之一解决办法
- 追求卓越之--arm MMU详解
- 最简单的makefile编写练习【main.c addc.c addc.h makefile】
- 使用Unity开发RPG游戏完整指南
- php7配置mysqli和使用mysqli连接mysql
- InnoDB学习笔记--锁
热门文章
- 使用JS代码禁止某些地区用户访问网站
- RAM嵌入式:LR、PC、SP寄存器的功能及使用
- 前端基础(5):Validate
- 微信小程序抽奖转盘v1.1(更新圈数控制和速度控制)
- 2017年9月11日 晴转多云
- 五大主流手机操作系统
- 人类使用计算机的一种重要方式是,计算机基础知识试题与答案
- Paralleled Merge Sort using CUDA
- 苹果电池ti测试软件,小技巧|不利用第三方软件,查看苹果设备电池循环次数...
- 全球及中国物联网智能模组行业竞争态势及发展战略规划报告2022-2028年版