[toc]
Ansible基础知识
1.Ansible基本概述
Ansible是一个配置管理系统configuration management system你只需要可以使用ssh访问你的服务器或设备就行
1.1 Ansible能 做什么
ansible可以帮助我们完成一些批量任务,或者完成一些需要经常重复的工作。
-
比如:同时在100台服务器上安装nginx服务,并在安装后启动服务。
-
比如:将某个文件一次性拷贝到100台服务器上。
-
比如:每当有新服务器加入工作环境时,你都要为新服务器部署某个服务,也就是说你需要经常重复的完成相同的工作。
这些场景中我们都可以使用到ansible。
1.2 Ansible软件特点
-
ansible不需要单独安装客户端,SSH相当于ansible客户端。
-
ansible不需要启动任何服务,仅需安装对应工具即可。
-
ansible依赖大量的python模块来实现批量管理。
-
ansible默认配置文件是
/etc/ansible/ansible.cfg
1.3 Ansible基础架构
-
连接插件(connectior plugins) 用于连接主机 用来连接被管理端
-
核心模块(core modules) 连接主机实现操作, 它依赖于具体的模块来做具体的事情
-
自定义模块(custom modules) 根据自己的需求编写具体的模块
-
插件(plugins) 完成模块功能的补充
-
剧本(playbooks)ansible的配置文件,将多个任务定义在剧本中,由ansible自动执行
-
主机清单(host inventory)定义ansible需要操作主机的范围
最重要的一点是 ansible是模块化的 它所有的操作都依赖于模块
2. Ansible安装配置
所有的受控主机必须与ansible服务端做ssh免密登陆
2.1 安装ansible(需要配置epel源)
yum -y install ansible
centos7.9安装的ansible版本为2.9.21
$ ansible --version
ansible 2.9.21
2.2 配置ansible
编辑主机清单文件
cat >> /etc/ansible/hosts<EOF
[host]
10.0.0.100
10.0.0.101
EOF
###############################
/etc/absible/hosts ansible主机清单配置文件
[host] 主机清单名
10.0.0.100 主机IP地址1
10.0.0.101 主机IP地址2
2.3 验证ansible与受控机是否通信
# ansible是通过ssh端口探测通信
$ ansible all -m ping
10.0.0.101 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
10.0.0.100 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
2.4 ansible语法格式
命令 主机模块名 指定模块参数 模块名称 指定利用模块执行的动作选项 批量执行操作动作
$ ansible all -m command -a "hostname"
10.0.0.101 | CHANGED | rc=0 >>
jenkins
10.0.0.100 | CHANGED | rc=0 >>
gitlab-server
# 说明
all 模块名
-m 指定模块
command command模块,完成基础命令
-a 指定执行动作
"hostname" 执行hostname命令
3.Ansible系列命令
3.1 ansiblie系列命令1:ansible
使用场景:
-
非固化需求
-
临时一次性操作
-
二次开发接口调用
使用示例
-
检查服务器存活状态
-
ansible all -m ping
$ ansible all -m ping
k8s-node03 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
k8s-master01 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
k8s-node02 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
k8s-node01 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
-
3.2 ansiblie系列命令2:ansible-galaxy
命令作用:
- 根据下载量和关注量等信息,查找和安装优秀的roles
命令格式:
ansible-galaxy [init|info|install|list|remove] [ --help] [options] ...
命令分为3部分
1⃣️ 选项
选项 | 说明 |
---|---|
init | 初始化本地的roles配置,以备上传roles至galaxy |
info | 列表指定role的详细信息 |
install | 下载并安装galaxy指定的roles到本地 |
list | 列出本地已经下载的roles |
remove | 删除本地已经下载的roles |
2⃣️ help用法显示[--help]
ansible-galaxy init --help
3⃣️ 参数
ansible-galaxy init [options] role_name
3.3 ansiblie系列命令3:ansible-doc
命令作用:
- 模块文档说明
命令格式:
ansible-doc [options] [module]
示例:
# 列出ansible支持的 模块
ansible-doc -l
# 模块功能说明
ansible-doc ping
3.4 ansiblie系列命令4:ansible-playbook
命令作用:
- 读取预先编写好的playbook文件实现批量管理
命令格式:
ansible-playbook xxx.yaml
示例:
# 执行http_install.yaml这个playbook中定义的所有任务集
ansible-playbook http_install.yaml
3.5 ansiblie系列命令5:ansible-vault
命令作用:
- 用于配置文件加密
命令格式:
ansible-vault [encrypt|decrypt|create|edit|rekey|view] [--help] [options] file
示例:
a.yaml文件内容如下
# 安装apache
- hosts: web
tasks:
- name: install httpd
yum: name=httpd state=installed
加密文件 ansible-vault encrypt a.yaml
$ ansible-vault encrypt a.yaml
New Vault password:
Confirm New Vault password:
Encryption successful
加密后查看a.yaml文件就会显示乱码
$ cat a.yaml
$ANSIBLE_VAULT;1.1;AES256
33666435656365396237363533616365346662373963393835376261333031356162373934383363
3633656532336436653261613539393532646131623433370a353865303931356131353065666261
35613738333338356635613337396565616663653366663134373537663935633134643734376333
6539333733353163380a356232663636343766313930636639383835656136623632393935636330
32386235313566383135386465613338346566623435363035646262356236393231353933396261
62626263613438313865666433323363636261616634613830623936393866616135663937386139
31636631313665613933393638663163393836386261316430353935363166633166383466363630
39333238623933613965333362396438303534363237393936393133636539633931306237366466
63303336336662346135356462316134616266366162316239373733636265633432
解密文件 ansible-vault decrypt a.yaml
$ ansible-vault decrypt a.yaml
Vault password: # 这里输入密码
Decryption successful
查看解密后的文件
$ cat a.yaml
# 安装apache
- hosts: web
tasks:
- name: install httpd
yum: name=httpd state=installed
4.Ansible正则
4.1 ALL全量匹配 all或*
匹配所有主机,all
与 *
号功能相同,但是*号需要用 ""
引起来
ansible all -m ping
$ ansible all -m ping
10.0.0.101 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
10.0.0.100 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
ansible "*" -m ping
$ ansible "*" -m ping
10.0.0.101 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
10.0.0.100 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
4.2 逻辑或匹配 :
同时对多台主机或多个组同时执行,相互之间用 :
分割,例如 jenking:gitlab
$ ansible jenkins:gitlab -m ping
jenkins | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
gitlab | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
4.3 逻辑非匹配 !
逻辑非用 !
表示,主要针对多重条件的匹配规则
# 所有在a组但不在b组的主机
ansible a:!b -m ping