01@ansible基础详解

Ansible

在这里插入图片描述

1、ansible简述(自动化运维)

“运维的未来是,让研发人员能够借助工具、自动化和流程,并且让他们能够在运维干预极少的情况下部署和运营服务,从而实现自助服务。每个角色都应该努力使工作实现自动化。” ——《运维的未来》

官网: https://www.ansible.com/

官方文档: https://docs.ansible.com/

源代码仓库: https://github.com/ansible/ansible

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

2、[企业实际应用]

Dev开发环境
Test测试环境
Pre预发布环境
Master生成环境

3、[应用场景]

文件传输
应用部署
配置管理
任务流编排

#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 ngin

4、常用自动化工具

1、Ansible:python,Agentless,中小型应用环境
2、Saltstack:python,一般需部署agent,执行效率更高
3、Puppet:ruby, 功能强大,配置复杂,重型,适合大型环境
4、Fabric:python,agentless
5、Chef:ruby,国内应用少

5、ansible发展史

#作者:Michael DeHaan( Cobbler 与 Func 作者)

ansible 的名称来自科幻小说《安德的游戏》中跨越时空的即时通信工具,使用它可以在相距数光年的距离,远程实时控制前线的舰队战斗。

2012-03-09,发布0.0.1版,

2015-10-17,Red Hat宣布1.5亿美元收购


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

6、[Ansible的特性]

模块化:调用特定的模块完成特定任务,支持自定义模块,可使用任何编程语言写模块
Paramiko(python对ssh的实现),PyYAML,Jinja2(模板语言)三个关键模块
基于Python语言实现
部署简单,基于python和SSH(默认已安装),agentless,无需代理不依赖PKI(无需ssl)
安全,基于OpenSSH
幂等性:一个任务执行1遍和执行n遍效果一样,不因重复执行带来意外情况,此特性非绝对
支持playbook编排任务,YAML格式,编排任务,支持丰富的数据结构
较强大的多层解决方案 role

7、[Ansible架构]

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命令执行。

8、安装Ansible

#1.安装epel源
[root@localhost ~]# curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

#2.安装Ansible
[root@localhost ~]# yum install ansible -y


  
  
#ansible格式
ansible <host-pattern> [options]
#参数详解
--version   #ansible版本信息
-v          #显示详细信息
-i          #主机清单文件路径,默认是在/etc/ansible/hosts
-m          #使用的模块名称,默认使用command模块
-a          #使用的模块参数,模块的具体动作
-k          #提示输入ssh密码,而不使用基于ssh的密钥认证
-C          #模拟执行测试,但不会真的执行
-T          #执行命令的超时



#Ansible是否安装成功查看(版本)
[root@m01 ~]# ansible --version          #版本查看
ansible 2.9.21
  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, Nov 16 2020, 22:23:17) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]

Ansible配置文件读取顺

1、$ANSIBLE_CONFIG
2、./ansible.cfg
3、~/.ansible.cfg
4、/etc/ansible/ansible.cfg

[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.

9、[Ansible相关文件]

#配置文件
/etc/ansible/ansible.cfg 主配置文件,配置ansible工作特性
/etc/ansible/hosts 主机清单
/etc/ansible/roles/ 存放角色的目录


#ansible主配置文件
Ansible 的配置文件 /etc/ansible/ansible.cfg ,其中大部分的配置内容无需进行修改



#配置文件注释
[defaults]
#inventory           = /etc/ansible/hosts              # 主机列表配置文件
#library             = /usr/share/my_modules/          # 库文件存放目录
#remote_tmp          = $HOME/.ansible/tmp              # 临时py命令文件存放在远程主机目录
#local_tmp           = $HOME/.ansible/tmp              # 本机的临时命令执行目录
#forks               = 5                               # 默认并发数
#sudo_user           = root                            # 默认sudo 用户
#ask_sudo_pass       = True                            # 每次执行ansible命令是否询问ssh密码
#ask_pass            = True   
#remote_port         = 22
#host_key_checking   = False                            # 检查对应服务器的host_key,建议取消注释
#log_path            = /var/log/ansible.log             # 日志文件,建议启用
#module_name         = command                          # 默认模块,可以修改为shell模块

10、[inventory 主机清单]

ansible的主要功用在于批量主机操作,为了便捷地使用其中的部分主机,可以在inventory file中将其分组命名,默认的inventory file为 /etc/ansible/hosts,inventory file可以有多个,且也可以通过Dynamic Inventory来动态生成

官方文档:https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html

/etc/ansible/hosts是ansible默认主机资产清单文件,用于定义被管理主机的认证信息, 例如ssh登录用户名、密码以及key相关信息。Inventory文件中填写需要被管理的主机与主机组信息。还可以自定义Inventory主机清单的位置,使用-i指定文件位置即可。

11、主机清单文件格式

inventory文件遵循INI文件风格,中括号中的字符为组名。可以将同一个主机同时归并到多个不同的组中

此外,当如若目标主机使用了非默认的SSH端口,还可以在主机名称之后使用冒号加端口号来标明,如果主机名称遵循相似的命名模式,还可以使用列表的方式标识各主机。

1场景一:基于密码连接方式一

#############################(IP+端口+用户+密码)#####################
[root@m01 ~]# cat /etc/ansible/hosts 
[web]
172.16.1.7 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='1' 
172.16.1.8 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='1'
172.16.1.9 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='1'
[root@@m01 ~]# ansible all -m ping 
172.16.1.8 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
......
...



#################################(主机名+密码)##########################
[root@localhost ~]#vim /etc/ansible/hosts
[web]
web0[1:3] ansible_ssh_pass='1'


#添加配置文件
[root@localhost ~]#vim /etc/ssh/ssh_config           #增加配置文件
Host *
        GSSAPIAuthentication yes
.........
......
StrictHostKeyChecking no 



[root@localhost ~]# ansible all -m ping          #检查测试ok
web02 | SUCCESS => {     
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
.....
...

场景二:基于密钥连接 (需要先创建公钥和私钥)

秘钥
#秘钥对制作
[root@localhost ansible]# ssh-keygen 
.....
..
#方式一秘钥推送传输
[root@localhost ansible]# ssh-copy-id 172.16.1.7
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
......
...


#方式二推送公钥
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.7
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.8
#方式一、主机+端口+密钥
[webs]
10.0.0.7:22
10.0.0.8

[root@m01 ~]# ansible webs -m ping -i ./hosts
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"
}




#方式二、别名+主机+端口+密钥
[webs]
web01 ansible_ssh_host=10.0.0.7 ansible_ssh_port=22
web02 ansible_ssh_host=10.0.0.8

[root@m01 ~]# ansible webs -m ping -i ./hosts
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@localhost ~]# vim /etc/ansible/hosts
172.16.1.61 ansible_connection=local   #指定本地连接,无需ssh配置
......
...
[root@localhost ~]# ansible all -m ping 
172.16.1.61 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}



# 注:密钥连接的前提是需要做好免密
[root@localhost ~]# vim /etc/ansible/hosts
[web]
172.16.1.7
172.16.1.8
172.16.1.9
[root@localhost ~]# ansible all -m ping 
172.16.1.7 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}


#别名连接
[root@localhost ~]# vim /etc/ansible/hosts
[web]
oldboy ansible_ssh_host=172.16.1.7

[root@localhost ~]# ansible web -m ping   &&   ansible oldboy  -m ping 
172.16.1.7 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}


场景三:主机组

# 分组连接
[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


#指连接的组
[root@localhost ~]# ansible web -m ping 







#主机组使用方式
[root@m01 ~]# vim hosts
[db_group]
db01 ansible_ssh_host=10.0.0.51
db02 ansible_ssh_host=10.0.0.52

[web_group]
web01 ansible_ssh_host=10.0.0.7
web02 ansible_ssh_host=10.0.0.8



#查看指定组内主机数量
[root@m01 ~]# ansible web_group -m ping -i ./hosts  --list-host
  hosts (2):
    web01
    web02

[root@m01 ~]# ansible db_group -m ping -i ./hosts  --list-host
  hosts (2):
    db01
    db02


#方式一、主机组变量+主机+密码
[db_group]
db01 ansible_ssh_host=10.0.0.51
db02 ansible_ssh_host=10.0.0.52
[db_group:vars]
ansible_ssh_pass='1'

#方式二、主机组变量+主机+密钥
[web_group]
web01 ansible_ssh_host=10.0.0.7
web02 ansible_ssh_host=10.0.0.8

#定义多组,多组汇总整合
# lnmp组包括两个子组[db,web]
[lnmp:children]
db_group
web_group

#最终配置文件
[root@m01 ~]# cat hosts
[db_group]
db01 ansible_ssh_host=10.0.0.51
db02 ansible_ssh_host=10.0.0.52

[web_group]
web01 ansible_ssh_host=10.0.0.7
web02 ansible_ssh_host=10.0.0.8

[lnmp:children]
db_group
web_group

#查看多组
[root@m01 ~]# ansible all -m ping -i ./hosts  --list-host
  hosts (4):
    db01
    db02
    web01
    web02
[root@m01 ~] ansible lnmp -m ping -i ./hosts  --list-host
  hosts (4):
    db01
    db02
    web01
    web02

12、查看服务器(统计)

#查看当前有多少台主机
[root@localhost ~]# ansible all -m ping -i /etc/ansible/hosts --list-hosts
  hosts (7):
    172.16.1.7
    172.16.1.8
    172.16.1.9
    oldboy
    web01
    web02
    web03
[root@localhost ~]# ansible web -m ping -i /etc/ansible/hosts --list-hosts
  hosts (3):
    172.16.1.7
    172.16.1.8
    172.16.1.9
[root@localhost ~]# ansible server -m ping -i /etc/ansible/hosts --list-hosts
  hosts (3):
    web01
    web02
    web03
[root@localhost ~]# ansible webserver -m ping -i /etc/ansible/hosts --list-hosts
  hosts (1):
    oldboy
[root@localhost ~]# ansible oldboy -m ping -i /etc/ansible/hosts --list-hosts
  hosts (1):
    oldboy
[root@localhost ~]# 

posted @ 2021-05-28 00:01  ଲ一笑奈&何  阅读(109)  评论(0编辑  收藏  举报