是一个自动化统一配置管理工具,自动化主要体现在Ansible集成了丰富模块以及功能组件,可以通过一个命令完成一系列的操作,进而能减少重复性的工作和维护成本,可以提高工作效率。
puppet
ansible #轻量,只要配置一个主机清单就可以进行管理了
saltstack #大规模速度比ansible快,一般采取c/s结构模式,底层是zero-MQ消协队列
1、批量命令执行
2、批量安装服务
3、批量配置
4、批量任务执行
5、批量代码部署
1、提高工作效率
2、提高工作准确性
3、降低人工成本
4、减少重复工作
1、连接插件connection plugins用于连接主机 用来连接被管理端
2、核心模块core modules连接主机实现操作, 它依赖于具体的模块来做具体的事情
3、自定义模块custom modules根据自己的需求编写具体的模块
4、插件plugins完成模块功能的补充
5、剧本playbookansible的配置文件,将多个任务定义在剧本中,由ansible自动执行
6、主机清单inventor定义ansible需要操作主机的范围
最重要的一点是 ansible是模块化的 它所有的操作都依赖于模
1.Ansible读取playbook剧本,剧本中会记录对哪些主机执行哪些任务。
2.首先Ansible通过主机清单找到要执行的主机,然后调用具体的模块。
3.其次Ansible会通过连接插件连接对应的主机并推送对应的任务列表。
4.最后被管理的主机会将Ansible发送过来的任务解析为本地Shell命令执行。
主机名 | ip | 身份 |
---|---|---|
m01 | 192.168.15.61 | 控制端 |
web01 | 172.16.1.7 | 被控端 |
web02 | 172.16.1.8 | 被控端 |
#1.安装epel源
[root@m01 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
#2.安装Ansible
[root@m01 ~]# yum install -y ansible
# ansible <host-pattern> [options]
--version #ansible版本信息
-v #显示详细信息
-i #主机清单文件路径,默认是在/etc/ansible/hosts
-m #使用的模块名称,默认使用command模块
-a #使用的模块参数,模块的具体动作
-k #提示输入ssh密码,而不使用基于ssh的密钥认证
-C #模拟执行测试,但不会真的执行
-T #执行命令的超时
#3.查看Ansible版本及模块路径
[root@m01 ~]# ansible --version
ansible 2.7.1
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, Apr 11 2018, 07:36:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]
[root@m01 ~]# vim /etc/ansible/ansible.cfg
# nearly all parameters can be overridden in ansible-playbook
# or with command line flags. ansible will read ANSIBLE_CONFIG,
# ansible.cfg in the current working directory, .ansible.cfg in
# the home directory or /etc/ansible/ansible.cfg, whichever it
# finds first
[root@m01 ~]# rpm -ql ansible
[root@m01 ~]# zcat /usr/share/man/man1/ansible-config.1.gz
#要查看完整列表,请访问https://docs.ansibe.com/或使用ansible-config命令。
For a full list check \fI\%https://docs.ansible.com/\fP\&. or use the \fIansible\-config\fP command.
#/etc/ansible/ansible.cfg 配置文件,如果存在则使用
/etc/ansible/ansible.cfg \-\- Config file, used if present
#~/.ansible.cfg 用户配置文件,覆盖默认配置(如果存在)
~/.ansible.cfg \-\- User config file, overrides the default config if present
#\&/ansible.cfg 本地配置文件(在当前工作目录中)假定为(aqproject-specific)(aq,如果存在,则重写其余文件)。
\&./ansible.cfg \-\- Local config file (in current working directory) assumed to be \(aqproject specific\(aq and overrides the rest if present.
#如上所述,ANSIBLE_CONFIG环境变量将覆盖所有其他环境变量。
As mentioned above, the ANSIBLE_CONFIG environment variable will override all others.
#读取的时候自上而下
#生效的时候自下而上
[root@m01 ansible]# vim /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日志
#普通用户提权操作
[privilege_escalation]
#become=True
#become_method=sudo
#become_user=root
#become_ask_pass=False
# 说明:使用主机清单,如果出现需要记录known_host的时候,可以在/etc/ssh/ssh_config中增加StrictHostKeyChecking no
[root@m01 ~]# cat /etc/ansible/hosts
# 方式一:配置IP及用户名密码
#[web]
#172.16.1.7 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=‘123‘ ansible_ssh_host=172.16.1.7
#172.16.1.8 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=‘123‘
#172.16.1.9 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=‘123‘
# 主机名+密码
[web]
web0[1:3] ansible_ssh_pass=‘123‘
# 本机
172.16.1.61 ansible_connection=local
# 公共变量
[db]
172.16.1.51
[db:vars]
ansible_ssh_pass=‘123‘
# 说明:密钥连接的前提是需要做好免密
[web]
172.16.1.7
172.16.1.8
172.16.1.9
# 别名连接
[web]
oldboy ansible_ssh_host=172.16.1.7
# 分组连接
[web]
172.16.1.7
172.16.1.8
172.16.1.9
[lb]
172.16.1.5
172.16.1.6
# 多组
[web]
172.16.1.7
172.16.1.8
172.16.1.9
[lb]
172.16.1.5
172.16.1.6
[db]
172.16.1.51
[servers:children]
lb
web
-i : 临时指定host路径
[root@m01 ~]# ansible lbserver -m ping -i ./host
172.16.1.6 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
172.16.1.5 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
原文:https://www.cnblogs.com/zhaokunhao/p/14814771.html