ansible基础

Ansible 基础学习

一、Ansible 概述

  • 什么是Ansible

Ansible是一个自动化统一配置管理工具,自动化主要体现在Ansible集成了丰富模块以及功能组件,可以通过一个命令完成一系列的操作,进而能减少重复性的工作和维护成本,可以提高工作效率。
  • 自动化工具

1.puppet 学习难,安装ruby环境难,没有远程执行功能
2.ansible 轻量级,大规模环境下只通过ssh会很慢,串行的
3.saltstack 一般选择salt会使用C/S结构的模式,salt-master和salt-minion,并行的,大规模批量操作的情况下,会比Ansible速度快一些,底层使用的是zero-MQ消息队列
  • 自动化运维相较于手动运维

1.提高工作效率
2.提高了工作的准确度
3.减少人员成本
4.减少了重复的工作
  • Ansible的功能及优点

1.远程执行
批量执行远程命令,可以对多台主机进行远程操作

2.配置管理
批量配置软件服务,可以进行自动化方式配置,服务的统一配置管理,和启停

3.事件驱动
通过Ansible的模块,对服务进行不同的事件驱动
比如:
    1)修改配置后重启
    2)只修改配置文件,不重启
    3)修改配置文件后,重新加载
    4)远程启停服务管理

4.管理公有云
通过API接口的方式管理公有云,不过这方面做的不如saltstack.
saltstack本身可以通过saltcloud管理各大云厂商的云平台。

5.二次开发
因为语法是Python,所以便于运维进行二次开发。

6.任务编排
可以通过playbook的方式来统一管理服务,并且可以使用一条命令,实现一套架构的部署

7.跨平台,跨系统
几乎不受到平台和系统的限制,比如安装apache和启动服务

    在Ubuntu上安装apache服务名字叫apache2
    在CentOS上安装apache服务名字叫httpd

    在CentOS6上启动服务器使用命令:/etc/init.d/nginx start
    在CentOS7上启动服务器使用命令:systemctl start nginx

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YQdPGEq2-1623253274906)(img/image-20201217190310958.png)]

二、Ansible 构成

  • 组成部分

1.连接插件connection plugins用于连接主机 用来连接被管理端
2.核心模块core modules连接主机实现操作, 它依赖于具体的模块来做具体的事情
3.自定义模块custom modules根据自己的需求编写具体的模块
4.插件plugins完成模块功能的补充
5.剧本playbookansible的配置文件,将多个任务定义在剧本中,由ansible自动执行
6.主机清单inventor定义ansible需要操作主机的范围

最重要的一点是 ansible是模块化的 它所有的操作都依赖于模块

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RhCto40v-1623253274915)(img/image-20201217190252208.png)]

  • Ansible执行流程

1.Ansible读取playbook剧本,剧本中会记录对哪些主机执行哪些任务。    #web 安装nginx
2.首先Ansible通过主机清单找到要执行的主机,然后调用具体的模块。	 #web 是谁
3.其次Ansible会通过连接插件连接对应的主机并推送对应的任务列表。	 #使用yum模块安装nginx
4.最后被管理的主机会将Ansible发送过来的任务解析为本地Shell命令执行。	#受控端执行yum install -y nginx

三、Ansible使用

  • 环境准备

主机外网IP内网IP身份
m0110.0.0.61172.16.1.61控制端
web0110.0.0.7172.16.1.7受控端
web0210.0.0.8172.16.1.8受控端
  • 控制端安装ansible

#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参数

# ansible <host-pattern> [options]
#不常用参数
--version   #ansible版本信息
-v          #显示详细信息
-i          #主机清单文件路径,默认是在/etc/ansible/hosts
-k          #提示输入ssh密码,而不使用基于ssh的密钥认证
-C          #模拟执行测试,但不会真的执行
-T          #执行命令的超时yum install -y nginx

#常用参数
-m          #使用的模块名称,默认使用command模块
-a          #使用的模块参数,模块的具体动作
--syntax-check		#验证语法

[root@m01 ~]# ansible --version
ansible 2.9.15
  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)]
  • ansible配置文件

 [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/或使用ansibe-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.
  • ansible配置文件解释

[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                 #跳过检查主机指纹  vim /etc/ansible/ansible.cfg  71行
log_path = /var/log/ansible.log           #ansible日志

#普通用户提权操作
[privilege_escalation]
#become=True
#become_method=sudo
#become_user=root
#become_ask_pass=False 

四、主机清单配置

  • 基于密码的方式

单主机配置

#方式一: ip + 端口 + 用户名 + 用户密码
[root@m01 ~]# vim /etc/ansible/hosts 
[web01]
10.0.0.7 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass='1'
[web02]
10.0.0.8 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass='1'

#测试
[root@m01 ~]# ansible web01 -m ping
10.0.0.7 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
#方式二: ip + 用户密码
[root@m01 ~]# vim /etc/ansible/hosts 
[web01]
10.0.0.7 ansible_ssh_pass='1'
[web02]
10.0.0.8 ansible_ssh_pass='1'

[root@m01 ~]# ansible web02 -m ping
10.0.0.8 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
#方式三: ip + 变量密码
[root@m01 ~]# vim /etc/ansible/hosts 
[web01]
10.0.0.7
[web02]
10.0.0.8 ansible_ssh_pass='1'

[web01:vars]
ansible_ssh_pass='1'

多主机配置

[root@m01 ~]# vim /etc/ansible/hosts 
[web_group]
10.0.0.7 ansible_ssh_pass='1'
10.0.0.8 ansible_ssh_pass='1'

[root@m01 ~]# ansible 'web_group' -m ping
10.0.0.8 | SUCCESS =>  {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
10.0.0.7 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
  • 基于秘钥的方式

生成密钥对

1.生成密钥对
[root@m01 ~]# ssh-keygen

2.推送公钥
[root@m01 ~]# ssh-copy-id 10.0.0.7
[root@m01 ~]# ssh-copy-id 10.0.0.8

配置主机清单

[root@m01 ~]# vim /etc/ansible/hosts 
[web_group]
10.0.0.7
10.0.0.8

修改hosts配置主机清单

#配置主机清单
[root@m01 ~]# vim /etc/ansible/hosts 
[web_group]
web01
web02

#配置hosts
[root@m01 ~]# vim /etc/hosts
10.0.0.7 web01
10.0.0.8 web02

#测试
[root@m01 ~]# ansible 'web_group' -m ping
web02 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
web01 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
  • 定义整合组

定义整合组

[root@m01 ~]# vim /etc/ansible/hosts 
[web_group]
web01
web02
[nfs_server]
nfs ansible_ssh_pass='1'
[rsync_server]
backup ansible_ssh_pass='1'
[db_server]
db01 ansible_ssh_pass='1'
#定义整合组,下面包含多个组
[www:children]
web_group
nfs_server
rsync_server

#配置hosts
[root@m01 ~]# vim /etc/hosts
10.0.0.7 web01
10.0.0.8 web02
10.0.0.31 nfs
10.0.0.41 backup
10.0.0.51 db01

查看整合组下面的主机组

[root@m01 ~]# ansible www --list-host
  hosts (4):
    web01
    web02
    nfs
    backup

测试

#单主机
[root@m01 ~]# ansible 'web01' -m ping

#所有主机
[root@m01 ~]# ansible '*' -m ping

#所有主机
[root@m01 ~]# ansible 'all' -m ping

#主机组
[root@m01 ~]# ansible 'web_group' -m ping

#整合组
[root@m01 ~]# ansible 'www' -m ping
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值