Ansible简介
Ansible是⼀个⾃动化统⼀配置管理⼯具,⾃动化主要体现在Ansible集成了丰富模块以及功能组件,可以通过⼀个命令完成⼀系列的操作,进⽽能减少重复性的⼯作和维护成本,可以提⾼⼯作效率。
⾃动化⼯具
1.puppet :学习难,安装ruby环境难,没有远程执⾏功能
2.ansible :轻量级,⼤规模环境下只通过ssh会很慢,不能实现多台服务器并发
3.saltstack :⼀般选择salt会使⽤C/S结构的模式,salt-master和salt-minion,并⾏的,⼤规模批量操作的情况下,会⽐Ansible速度快⼀些,底层使⽤的是zero-MQ消息队列
⾃动化运维优点
1.提⾼了⼯作效率2.提⾼了⼯作准确度3.减少⼈⼯成本
1.远程执⾏
批量执⾏命令,可对多台主机进⾏操作2.配置管理
批量配置软件服务,可进⾏⾃动化⽅式配置,服务的统⼀配置3.事件驱动
对服务进⾏不同的驱动
eg:1.修改配置后重启,2.只修改配置不重启,3.修改配置后重新加载4.管理公有云
通过API接⼝⽅式管理公有云(不如saltstack)5.⼆次开发
因为语法为python,便于运维进⾏⼆次开发6.任务编排
通过playbook⽅式进⾏统⼀管理服务,并且可使⽤⼀条命令实现⼀套架构部署7.跨平台,跨系统
⼏乎不受到平台与系统限制 eg:安装apache和启动服务
Ansible执⾏流程
Ansible的功能
1.Ansible读取playbook剧本,剧本中会记录对哪些主机执⾏哪些任务。2.⾸先Ansible通过主机清单找到要执⾏的主机,然后调⽤具体的模块。3.其次Ansible会通过连接插件连接对应的主机并推送对应的任务列表。
4.最后被管理的主机会将Ansible发送过来的任务解析为本地Shell命令执⾏。
安装Ansible
[root@localhost ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo[root@localhost ~]# yum install ansible
1、Ansible的hosts主机⽂件
[root@localhost ~]# cat /etc/ansible/hosts [web] # 分组
192.168.13.20 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass='1' IP ⽤户名 端⼝ 密码2、核⼼模块ping
[root@localhost ~]# ansible web -m ping
3.⾃定义模块custom modules根据⾃⼰的需求编写具体的模块4.插件plugins完成模块功能的补充
5.剧本playbookansible的配置⽂件,将多个任务定义在剧本中,由ansible⾃动执⾏6.主机清单inventor定义ansible需要操作主机的范围
最重要的⼀点是 ansible是模块化的 它所有的操作都依赖于模块1、查看ansible版本
[root@localhost ~]# ansible --version
2、执⾏的详情
[root@localhost ~]# ansible web -v -m command -a 'ls'3、主机清单路径
[root@localhost ~]# ansible web -i ./hosts -m ping4、输⼊SSH密码
[root@localhost ~]# ansible web -k -i ./hosts -m ping
5、测试执⾏的步骤是否正确
[root@localhost ~]# ansible web -m command -a 'mkdir /ss/dd/aa' -C
ansible配置⽂件ansible的具体使⽤ansible的组成
ansible的配置⽂件是:/etc/ansible/ansible.cfg
[root@m01 ~]# cat /etc/ansible/ansible.cfg
#inventory = /etc/ansible/hosts #主机列表配置⽂件#library = /usr/share/my_modules/ #库⽂件存放⽬录
#remote_tmp = ~/.ansible/tmp #临时py⽂件存放在远程主机⽬录#local_tmp = ~/.ansible/tmp #本机的临时执⾏⽬录#forks = 5 #默认并发数
#sudo_user = root #默认sudo⽤户
#ask_sudo_pass = True #每次执⾏是否询问sudo的ssh密码#ask_pass = True #每次执⾏是否询问ssh密码#remote_port = 22 #远程主机端⼝
host_key_checking = False #跳过检查主机指纹log_path = /var/log/ansible.log #ansible⽇志
主机清单配置
1、基于密码的⽅式
ip地址 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass='1' ansible_ssh_user :⽤户名 ansible_ssh_port :端⼝ ansible_ssh_pass :密码
2、基于变量密码的⽅式 [web01]
192.168.13.20 ansible_ssh_user=root ansible_ssh_port=22 [web01:vars]
ansible_ssh_pass='1'
3、⼀个分组配置多主机
[root@localhost ~]# cat /etc/ansible/hosts [web01]
[web01]
ip1 ansible_ssh_user=root ansible_ssh_port=22 iP2 ansible_ssh_user=root ansible_ssh_port=22
4、基于密钥的⽅式登录
[root@localhost ~]# cat /etc/ansible/hosts [web01]
ip1 ansible_ssh_user=root ansible_ssh_port=22 iP2 ansible_ssh_user=root ansible_ssh_port=22
5、分组组合
[root@localhost ~]# cat /etc/ansible/hosts [web01]
ip1 ansible_ssh_user=root ansible_ssh_port=22 [web02]
ip2 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass='1' [web:children] web01 wen02
ansible帮助#1.查看所有模块
[root@m01 ~]# ansible-doc -l
#2.查看指定模块的⽤法
[root@m01 ~]# ansible-doc yum
#3.查看模块参数
[root@m01 ~]# ansible-doc -s yum
因篇幅问题不能全部显示,请点此查看更多更全内容