首页
视频
资源
登录
原
Ansible 使用和模块化深入解析
4375
人阅读
2021/3/16 15:41
总访问:
2588933
评论:
0
收藏:
0
手机
分类:
Ansible
![ansible](https://img.tnblog.net/arcimg/hb/e283c36cf43847558bcb2c8461e20850.jpg "ansible") ># Ansible 使用和模块化深入解析 [TOC] Ansible 主配置文件讲解 ------------ tn>Ansible中的某些设置可以通过配置文件(ansible.cfg)进行调整。 ### Ansible读取配置文件的顺序 <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 当前执行的上下文目录($PWD) 用户家目录($HOME)下的".ansible.cfg" `/etc/ansible/ansible.cfg` </p> ### 确认Ansible配置文件 <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 首先我们查看一下ansible.cfg文件是否存在 </p> ```bash # ansible --version ansible 2.10.6 config file = None 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_base-2.10.6-py2.7.egg/ansible executable location = /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)] ``` <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 这里我们并没有发现配置文件,所以我们需要创建一个ansible.cfg的文件 </p> ```bash vim /etc/ansible/ansible.cfg # 然后将 https://github.com/ansible/ansible/blob/devel/examples/ansible.cfg 写进去 # 如果这样下载太慢还可以这样 # curl -L https://raw.staticdn.net/ansible/ansible/devel/examples/ansible.cfg > /etc/ansible/ansible.cfg # 最后检查一遍 # ansible --version ansible 2.10.6 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_base-2.10.6-py2.7.egg/ansible executable location = /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配置文件 | 大分类配置模块 | 描述 | | ------------ | ------------ | |[defaults]|定义常规默认的配置,如:定义文件路径、设置并发连接数、设置sudo用户、是否开启sudo功能等等| |[inventory]|设置插件及可忽略的文件格式(没几个参数,个人感觉用的地方不多)| |[privilege_escalation]|用户权限类的设定,如:sudo、是否启用sudo密码| |[paramiko_connection]|不记录遇到的新主机密钥、增加新主机的性能。(用的地方不多)| |[ssh_connection]|对SSH连接的一些配置,配置项比较少| |[persistent_connection]|主要就两个选项,连接超时、命令超时| |[accelerate]|连接加速的相关配置| |[selinux]|selinux相关配置,基本不会涉及| |[colors]|设置输出颜色,最好默认| |[diff]|输出打印是是否打印diff| ###### [defaults]模块 | 参数 | 描述 | | ------------ | ------------ | | inventory | 主机清单的文件 | | library | 库的位置 | | module_utils | 模块的路径 | | remote_tmp | 远程临时脚本目录(执行完后会删除) | | local_tmp | 本地临时脚本目录(执行完后会删除) | | plugin_filters_cfg | 插件的配置文件 | | forks | 并行多少台主机进行执行 | | poll_interval | 多长时间去拉一次数据 | | sudo_user | 以管理员的权限进行执行 | | ask_sudo_pass | 询问是否需要管理员的口令 | | ask_pass | 询问是否需要普通的口令 | | remote_port | 远程主机的端口号 | | module_lang | 模块语言 | | log_path | 日志文件(这个最好也记录一下) | | host_key_checking | 是否检查对方的密钥也就是ssh的known_hosts(一般把这个注释取消) | Ansible系列命令 ------------ ### ansible-doc 显示模块帮助 <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 命令:`ansible-doc [options] [module]` 参数如下表所示: </p> | 参数 | 描述 | | ------------ | ------------ | | -a | 显示所有模块的文档 | | -l,--list | 列出可用模块 | | -s,--snippet | 显示指定模块的play (缩减内容) | >举例查看ping模块:`ansible-doc ping` 或 `ansible-doc -s ping` ### ansible <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> ansible通过ssh实现配置管理、应用部署、任务执行等功能,建议配置ansible端能基于密钥认证端方式联系各被管理节点 ansible [host-pattern] [-m module_name] [-a args] </p> | 参数 | 描述 | | ------------ | ------------ | | `--version` | 显示版本 | | `-m module` | 指定模块,默认为command | | `-v` | 详细过程 `-vv`或`-vvv`更详细 | | `--list-hosts,--list` | 显示主机列表,可简写--list | | `-k,--ask-pass` | 提示输入sudo时的口令 | | `-K,--ask-become-pass` | 提示输入sudo时的口令 | | `-C,--check` | 检查,并不执行 | | `-T,--timeout=TIMEOUT` | 执行命令的超时时间,默认10s | | `-u,--user=REMOTE_USER` | 执行行程执行的用户 | | `-b,--become` | 代替旧版的sudo切换 | >列出组主机清单与所有主机清单 ```bash # ansible dbserver --list-hosts hosts (1): 10.211.55.5 # ansible all --list-hosts hosts (2): 10.211.55.5 10.211.55.6 ``` ###使用command模块 <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 使用管理员权限访问`/root`目录 </p> ```bash ansible webserver -u root -k -m command -a 'ls /root' ``` <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 我们通过aidasi用户用以root的账号密码去遍历`/root`目录下的文件 </p> ```bash ansible webserver -u aidasi -k -m command -a 'ls /root' -b -K ``` ### 新用户设置为管理员访问 <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 如果我们的用户没有该root的访问权限,那么我们可以一台一台的去添加wheel组,以及添加testuser账号,每台可以按照如下步骤操作: </p> ```bash # 添加用户 useradd testuser # 添加密码 passwd testuser # 开通wheel组 visudo ``` ![](https://img.tnblog.net/arcimg/hb/25afe97661394b968fe21cc4ae303703.png) ```bash # 将testuser添加到wheel组里面去 usermod -aG wheel testuser ``` <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 最后通过`testuser`去访问试试 </p> ```bash # 使用testuser进行管理员管理 ansible webserver -u testuser -k -m command -a 'ls /root' -b -K ``` <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 但是我们发现它要输入两次密码比较麻烦,所以我们可以在设置wheel的时候进行修改,像下图修改后就不会输入两次密码了。 </p> ![](https://img.tnblog.net/arcimg/hb/0b1bf62033144efcba4e971e77bfd169.png) ### 密钥访问 <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 但这样还是觉得麻烦的话可以通过密钥的形式进行远程操作,不需要输入密码了,在主机上进行如下操作 </p> ```bash # 生成密钥(全部回车) ssh-keygen # 将密钥分发给各个子机 ssh-copy-id 10.211.55.5 ssh-copy-id 10.211.55.5 ``` ```bash # ansible webserver -m command -a 'ls /root' 10.211.55.5 | CHANGED | rc=0 >> anaconda-ks.cfg original-ks.cfg 10.211.55.6 | CHANGED | rc=0 >> anaconda-ks.cfg original-ks.cfg ``` ### ansible的Host-pattern <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> All :表示所有Inventory中的所有主机 </p> ```bash ansible all -m ping ``` <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> * :通配符 </p> ```bash ansible "*" -m ping ansible 10.211.55.* -m ping ansible "*server" -m ping ``` <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 或关系上 </p> ```bash # cat /etc/ansible/hosts [webserver] 10.211.55.5 10.211.55.6 [dbserver] 10.211.55.5 # 获取指定这两个组的访问 ansible "dbserver:webserver" -m ping # 获取指定ip模块的访问 ansible 10.211.55.5:10.211.55.6 -m ping ``` <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 逻辑与 </p> ```bash # 在webserver组并且在dbserver组中的主机 ansible "webserver:&dbserver" -m ping ``` <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 逻辑非 </p> ```bash # 在webserver组,但不在dbserver组中的主机 注意:此处为单引号。 ansible 'webserver:!dbserver' -m ping ``` <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 综合逻辑 </p> ```bash ansible 'webserver:dbserver:&webserver:!dbserver' -m ping ``` <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 正则表达式 </p> ```bash ansible 'webserver:&dbserver' -m ping ansible '~(db|web)server' -m ping ``` ansible命令执行过程 ------------ ![](https://img.tnblog.net/arcimg/hb/c24ac70fbeef4b31b5972bb00cabc8b4.png) <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> ansible命令执行过程: 1. 加载自己的配置文件默认`/etc/ansible/ansible.cfg` 2. 加载自己对应的模块文件,如command 3. 通过ansible将模块或命令生成对应的临时py文件,并将该文件传输至远程服务器的对应执行用户`$HOME/.ansible/tmp/ansible-tmp-数字/XXX.py`文件 4. 给文件+x执行 5. 执行并返回结果 6. 删除临时py文件,sleep 0退出 执行状态: 绿色:执行成功并且不需要做改变的操作 黄色:执行成功并且对目标主机做变更 红色:执行失败 </p> tn>可以在执行模块后面加`-v`,`-vv`或`-vvv`显示得更加详细哟 ansible常见模块 ------------ ###command模块 <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> Command :是在远程主机上执行命令的模块,也是默认模块,可以不用写`-m`选项,关于该模块的参数如下表所示 </p> | 参数 | 描述 | | ------------ | ------------ | | `chdir` | 在运行该命令之前,请切换到此目录。 | | `creates` | 判断指定文件存在,则后面的命令不执行 | | `removes` | 判断指定文件不存在,则后面的命令不执行 | tn>更多请参考:`ansible-doc command` <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> chdir参数应用,例如我要到`/etc`目录下执行`ls`的命令 </p> ```bash ansible all -a 'chdir=/etc ls' ``` <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> creates参数应用,例如有`/home/a.sh`文件,我就不执行后面的语句 </p> ```bash ansible all -a 'creates=/home/a.sh echo "not find a.sh"' # removes 反之 ansible all -a 'removes=/home/a.sh echo "not find a.sh"' ``` <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 但是这个模块不可以使用环境变量,以及管道字符(`< > | ; &`)使用起来有问题如: `ansible all -a 'echo $SUDO_USER'` 所以这个时候就有Shell模块出现了 </p> ###Shell模块 <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> Shell模块与Command相似,相当与在Command上支持环境变量的输出与管道的支持 示例 </p> ```bash # 随便输出一个环境变量 ansible all -m shell -a 'echo $XDG_RUNTIME_DIR' # 创建`/root/a.sh`文件 ansible all -m shell -a '> /root/a.sh' ``` ### Secript模块 <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 只需要在本地写一个shell脚本,然后就可以在其他子机上执行。 </p> ```bash vim b.sh #!/bin/bash hostname # 执行这个脚本 ansible all -m script -a '/home/aidasi/ansible-2.9.13/b.sh' ``` ###Copy模块 <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> Copy模块可以将文件复制到指定目录下,我们可以通过`ansible-doc -s copy`来查看该模块的详细内容 </p> | 参数 | 描述 | | ------------ | ------------ | | `backup` | 创建一个包含时间戳信息的备份文件,以便在错误地重击原始文件时可以将其取回。 | | `dest` | 文件应复制到的远程绝对路径。 | | `mode` | 文件权限 | |`src` | 要复制到远程服务器的文件的本地路径。 | |`owner` | 所有者 | |`content` | 直接写入内容 | <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 接着我们通过一个关闭SeLinux的方式来使用该方法 </p> ```bash # 查看Selinux是否启动 ansible all -m shell -a 'getenforce' # 查看selinux ansible all -a 'ls -l /etc/sysconfig/selinux' # 结果我们发现这个才是真实的路径,上面是个软连接 ansible all -a 'ls -l /etc/selinux/config' # 然后我们先将本地的文件修好 vim /etc/selinux/config SELINUX=disabled # 然后将文件复制过去 ansible all -m copy -a 'src=/etc/selinux/config dest=/etc/selinux/config backup=yes' # 检查一下文件 ansible all -m shell -a 'cat /etc/selinux/config' # 最后重启然后过几分钟再检查一遍 ansible all -m shell -a 'reboot' ansible all -m shell -a 'getenforce' ``` <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 然后我们也可以用所有者是testuser去设置文件权限 </p> ```bash ansible all -m copy -a 'src=/home/aidasi/ansible-2.9.13/b.sh dest=/root mode=000 owner=testuser' # 然后查看权限 ansible all -m shell -a 'ls -al' ---------- 1 testuser root 21 3月 18 22:10 b.sh ``` <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 你也可以直接通过写内容的方式 </p> ```bash ansible all -m copy -a 'content="hello\nthanks\n" dest=/root/f1' ```
欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739
👈{{preArticle.title}}
👉{{nextArticle.title}}
评价
{{titleitem}}
{{titleitem}}
{{item.content}}
{{titleitem}}
{{titleitem}}
{{item.content}}
尘叶心繁
这一世以无限游戏为使命!
博主信息
排名
6
文章
6
粉丝
16
评论
8
文章类别
.net后台框架
168篇
linux
17篇
linux中cve
1篇
windows中cve
0篇
资源分享
10篇
Win32
3篇
前端
28篇
传说中的c
4篇
Xamarin
9篇
docker
15篇
容器编排
101篇
grpc
4篇
Go
15篇
yaml模板
1篇
理论
2篇
更多
Sqlserver
4篇
云产品
39篇
git
3篇
Unity
1篇
考证
2篇
RabbitMq
23篇
Harbor
1篇
Ansible
8篇
Jenkins
17篇
Vue
1篇
Ids4
18篇
istio
1篇
架构
2篇
网络
7篇
windbg
4篇
AI
18篇
threejs
2篇
人物
1篇
嵌入式
2篇
python
13篇
HuggingFace
8篇
pytorch
9篇
opencv
6篇
最新文章
最新评价
{{item.articleTitle}}
{{item.blogName}}
:
{{item.content}}
关于我们
ICP备案 :
渝ICP备18016597号-1
网站信息:
2018-2024
TNBLOG.NET
技术交流:
群号656732739
联系我们:
contact@tnblog.net
欢迎加群
欢迎加群交流技术