首页
视频
资源
登录
原
Ansible 常见企业级应用实战
4922
人阅读
2021/3/21 15:40
总访问:
2602212
评论:
0
收藏:
1
手机
分类:
Ansible
![ansible](https://img.tnblog.net/arcimg/hb/e283c36cf43847558bcb2c8461e20850.jpg "ansible") ># Ansible 常见企业级应用实战 [TOC] Fetch模块 ------------ <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 fetch`命令来查看fetch的参数 </p> ### Fetch主参数表参考 | 参数 | 描述 | | ------------ | ------------ | | src | 指定远程主机的单个文件路径(不可指定文件夹) | | dest | 指定存放的主机目录,格式为`本地指定目录/主机名/远程的路径` | ### 获取远程单个日志文件 <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;"> 从远程主机上获取`/var/log/messages`文件保存到本地`/data`目录下 </p> ```bash # 创建/data目录 mkdir /data # 收集message文件 ansible all -m fetch -a 'src=/var/log/messages dest=/data' ``` <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 # tree /data /data ├── 10.211.55.5 │ └── var │ └── log │ └── messages └── 10.211.55.6 └── var └── log └── messages ``` ### 获取多个日志文件 <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 # 打包/var/log/*.log 所有的日志文件 ansible all -m shell -a 'tar Jcf log.tar.xz /var/log/*.log' # 获取该文件 ansible all -m fetch -a 'src=/root/log.tar.xz dest=/data' # 查看文件 tree /data /data ├── 10.211.55.5 │ ├── root │ │ └── log.tar.xz │ └── var │ └── log │ └── messages └── 10.211.55.6 ├── root │ └── log.tar.xz └── var └── log └── messages # 查看包中日志 tar tvf /data/10.211.55.5/root/log.tar.xz ``` File模块 ------------ <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> ### 参数列表 | 参数 | 描述 | | ------------ | ------------ | | path | 要管理的文件路径 (强制添加) | | recurse | 递归,文件夹要用递归 | | src | 创建硬链接,软链接时,指定源目标,配合`state=link` `state=hard` 设置软链接,硬链接 | | mode | 文件权限设置 | | owner | 设置目录拥有者 | | state | 这里的状态表示操作状态,值有如下几个:<br/> `absent`表示删除操作 <br/> `directory`表示创建一个文件夹<br/> `touch`表示创建一个文件 <br/> `link`创建一个软连接 | tn>更多请参考`ansible-doc -s file` ### 示例 ```bash # 创建文件夹 ansible all -m file -a 'path=/data state=directory' # 创建文件 ansible all -m file -a 'path=/data/test.txt state=touch' # 设置目录拥有者,与文件权限设置为755 ansible all -m file -a 'path=/data/test.sh owner=testuser mode=755' # 创建源文件`/data/test.txt`的`/root/test.txt`软连接 ansible all -m file -a 'src=/data/test.txt dest=/root/test.txt state=link' ``` Hostname模块 ------------ <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;"> 可以修改主机或一个组主机的hostname </p> ### 参数 | 参数 | 描述 | | ------------ | ------------ | | name | (必要)需要修改成什么样的主机名 | | use | 更新主机名的策略 | ### 示例 ```bash # 修改IP 10.211.55.5 的主机名 ansible 10.211.55.5 -m hostname -a 'name=app.node1.com' # 修改组webserver下的所有主机名 ansible webserver -m hostname -a 'name=app.webserver.com' ``` Cron模块 ------------ <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> ### 参数列表 | 参数 | 描述 | | ------------ | ------------ | | cron_file | 如果指定,则使用此文件而不是单个用户的crontab。如果这是一个相对路径,那么它将根据`/etc/cron.d`进行解释。如果是绝对值,则通常为`/etc/crontab`。许多linux发行版期望(有些发行版要求)文件名部分仅由大小写字母、数字、下划线和连字符组成。要使用`cron_file`参数,还必须指定`user`。 | | hour | 作业应运行的小时(`0-23`,`*`,`*/2`,等等)。 | | minute | 作业应运行的分钟 (`0-59`, `*`, `*/2`,等等). | | month | 作业应运行的月份 (`1-12`, `*`, `*/2`, 等等). | | weekday | 作业应运行的星期几 (`0-6` 星期天到星期六, `*`, 等等). | | name | 作业名称 | | state | 表示是否确保作业或环境变量存在或不存在。(可以做删除操作) | | job | 执行的命令 | | disabled | 启动(no)或关闭(yes) 作业任务| ### 示例 ```bash # 每分钟执行 FBI warning ansible webserver -m cron -a 'minute=* job="/usr/bin/wall FBI warning" name=fbijg' # 关闭该任务 ansible webserver -m cron -a 'disabled=yes job="/usr/bin/wall FBI warning" name=fbijg' # 删除该任务 ansible webserver -m cron -a 'state=absent job="/usr/bin/wall FBI warning" name=fbijg' # 可以在客户端中 crontab -e 查看任务列表 ``` Yum管理包模块 ------------ <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;"> 使用`yum`包管理器安装、升级、降级、删除和列出包和组。 </p> ### 参数列表 | 参数 | 描述 | | ------------ | ------------ | | name | 包名称 | | state | `present`为安装,`absent`为删除 | | list | 查看应用列表与`yum list`相似,值如下:<br/> `installed`表示以及安装的列表。<br/> `updates` 需要更新的应用列表。还有 `available` 与 `repos`. | | disable_gpg_check | yes进行gpg检查 | | update_cache | yes进行更新缓存(需要指定某个包的name) | tn>更多说明请参考:`ansible-doc -s yum` ### 安装tree包 ```bash # 安装tree应用,还可以通过,号的形式安装多个 ansible all -m yum -a 'name=tree' # 查看以及安装应用 ansible all -m yum -a 'list=installed' # 卸载应用 ansible all -m yum -a 'name=tree state=absent' ``` ### 安装本地包 ```bash # 运用copy模块将文件分发出去 ansible all -m copy -a 'src=/data/xxx.rpm dest=/root/' # 安装该程序 ansible all -m yum -a 'name=/root/xxx.rpm' ``` Service服务管理模块 ------------ <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;"> 控制远程主机上的服务。支持的init系统包括BSD init、OpenRC、SysV、Solaris SMF、systemd和upstart。 </p> ### 参数列表 | 参数 | 描述 | | ------------ | ------------ | | name | 指定服务的名称 | | enabled | 服务是(yes)否(no)应在开机时启动。 | | state | 该状态包括对服务对操作方式。<br/>`stopped`表示停止服务。<br/>`started`表示开启服务。<br/>`reloaded`表示重新加载该服务。<br/>`restarted`表示重新启动服务 | tn>更多参考`ansible-doc -s service` ### 示范 ```bash # 启动阿帕奇,并设置自启动 ansible all -m service -a 'name=httpd state=started enabled=yes' # 关闭 ansible all -m service -a 'name=httpd state=stopped' ``` User用户模块 ------------ <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> ### 参数列表 | 参数 | 描述 | | ------------ | ------------ | | home | 指定家目录路径 | | system | 指定系统账号 | | group | 指定组 | | remove | 清除账户 | | shell | 指定shell类型 | | home | 工作目录 | | uid | 进程账号 | | comment | 描述 | ### 示例 ```bash # 创建一个nginx的用户,shell类型为`/sbin/nologin`,并设置为系统账号,设置家目录为`/var/nginx`,分配到root与bin组目录下,uid进程号为80 ansible all -m user -a 'name=nginx shell=/sbin/nologin system=yes home=/var/nginx groups=root,bin uid=80 comment="nginx service"' # 查看用户账号 ansible all -a 'getent passwd nginx' # 删除nginx并删除 ansible all -m user -a 'name=nginx state=absent remove=yes' ``` Group用户组模块 ------------ <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> ### 参数列表 | 参数 | 描述 | | ------------ | ------------ | | name | 组名称。 | | system | 是否为系统组。 | | gid | 指定gid为多少 | ### 示例 ```bash # 创建一个nginx组且为系统组,gid为80 ansible all -m group -a 'name=nginx system=yes gid=80' # 删除该组 ansible all -m group -a 'name=nginx state=absent' ```
欢迎加群讨论技术,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
欢迎加群
欢迎加群交流技术