当前位置:网站首页>ansible学习笔记02
ansible学习笔记02
2022-07-31 15:28:00 【51CTO】
ansible的inventory文件
文件定义了ansible管理的主机,或主机组
静态inventory文件
用txt文本记录的文件,包括主机的ip地址,域名等信息,只要不修改inventory文件内容,被管理的主机就不会发生变化
动态inventory文件
从数据库获取动态信息,信息随着数据库的变化而变化,大多数情况下以json格式输出
inventory文件
一个简单的inventory文件
定义主机组
通过方括号[]括起来,一个主机文件可以属于多个组
组里面能嵌套组
定义主机范围
使用[start:end]定义范围
注意事项
如果主机和主机组重名了,ansible会忽略主机组,选择主机名,配置inventory文件尽量避免这件事发生。
查看主机信息
命令: ansible 主机名or组名 -i 路径 --list-hosts
| 参数 | 含义 |
|---|---|
| -i | 指定inventory文件路径,默认路径是/etc/ansible/hosts,不指定该参数,会进入默认路径查找 |
| –list-hosts | 列出主机信息 |
| all | 列出所有主机组信息 |
| ungrouped | 列出非主机组信息 |
ansible的配置文件
ansible配置文件不是全局的,任何用户都可以拥有自己的ansible配置文件
配置文件的优先级
| 路径 | 优先级 |
|---|---|
| /etc/ansible/ansible.cfg | 最低 |
| ~/.ansible.cfg(家目录下) | 低 |
| ./ansible.cfg(当前目录下) | 中(建议使用) |
| ANSIBLE_CONFIG(全局变量指定) | 高 |
ANSIBLE_CONFIG不建议使用,一旦使用,所有的用户都会指向该路径
使用默认的ansible.cfg
使用家目录下的.ansible.cfg
使用当前目录下ansible.cfg
ansible配置文件的相关参数
ansible配置文件练习
创建目录/home/student/deploy-manage
目录下有ansible.cfg文件,Inventory文件也在该目录下
Inventory文件的主机组如下
[myself] 拥有主机localhost
[intranetweb] 拥有主机 servera.lab.example.com
[internetweb] 拥有主机 serverb.lab.example.com
[web] 嵌套组拥有主机组 intranetweb 和 internetweb
在ansible.cfg增加[privilege_escalation]选项,能够使用sudo方式提权,提权到root用户,并且输入密码
ansible ad hoc 命令
ad hoc:临时的,一行通过ansible开头执行的命令,使用起来非常的简单,快速。
使用格式: ansible host-pattern -m moudle [-a ‘moudle arguments’] [-i inventory]
| 模块分类 | 模块 |
|---|---|
| 文件模块 | copy、file、lineinfile、synchronize |
| 软件包模块 | package、yum、apt、dnf、gem、pip |
| 系统模块 | firewalld、service、user、reboot |
| Net工具 | get_url(下载)、nmcli(设置网卡)、uri(与web交互) |
ad hoc方式,使用ping模块
ad hoc方式,使用user模块添加用户
ad hoc方式,使用user模块移除用户
ad hoc方式,使用copy模块
ad hoc方式,使用command模块1
ad hoc缺省值就是command,使用的时候可省略 -m command参数
| 配置文件参数 | command选项 |
|---|---|
| inventory | -i |
| remote_user | -u |
| become | –become,-b |
| become_method | –become-method |
| become_user | –become-user |
| become_ask_pass | –ask-become-pass,-K |
ad hoc方式,使用command模块2
ad hoc相关练习
1、 查看ansible版本
2、 新建文件夹/home/student/deploy-review
3、 在文件夹内创建ansible.cfg,指定inventory目录为:/home/student/deploy-review/inventory,指定登录用户为devops
4 、创建子目录/home/student/deploy-review/inventory,inventory文件从:从http://materials.example.com/labs/deploy-review/inventory下载
5、 使用ansible ad hoc 中command模块查看主机id信息
6、 使用ansible ad hoc 中copy模块将文本:This server is managed by Ansible. \n 输出到/etc/motd中
7、 再次运行题目6相同的ad hoc,查看现象
8、 使用ansible ad hoc 中command模块查看/etc/motd中的信息
单元小结
边栏推荐
- Getting Started with TextBlock Control Basic Tools Usage, Get Started
- Kubernetes常用命令
- Word table to Excel
- R language test whether the sample conforms to normality (test whether the sample comes from a normally distributed population): shapiro.test function tests whether the sample conforms to the normal d
- 7. Summary of common interview questions
- TRACE32 - C source code association
- what exactly is json (c# json)
- RecyclerView高效使用第二节
- 思路迪医药冲刺港股:5个月亏2.9亿 泰格医药与先声药业是股东
- The principle of hough transform detection of straight lines (opencv hough straight line detection)
猜你喜欢

【MySQL】Mysql范式及外键作用

WeChat chat record search in a red envelope

mysql black window ~ build database and build table

Synchronized和volatile 面试简单汇总

【Meetup预告】OpenMLDB+OneFlow:链接特征工程到模型训练,加速机器学习模型开发

"Autumn Recruitment Series" MySQL Interview Core 25 Questions (with answers)

工程流体力学复习

radiobutton的使用

定时器的类型

WPF项目--控件入门基础用法,必知必会XAML
随机推荐
TRACE32——C源码关联
JVM参数解析 Xmx、Xms、Xmn、NewRatio、SurvivorRatio、PermSize、PrintGC「建议收藏」
Ubantu专题5:设置静态ip地址
thread_local 变量的析构顺序
R language moves time series data forward or backward (custom lag or lead period): use the lag function in the dplyr package to move the time series data forward by one day (set the parameter n to a p
Kubernetes common commands
Bilateral filtering acceleration "recommended collection"
R语言ggstatsplot包ggbarstats函数可视化条形图、并添加假设检验结果(包含样本数、统计量、效应大小及其置信区间、显著性、组间两两比较、贝叶斯假设)、检验结果报告符合APA标准
Codeforces Round #796 (Div. 2) (A-D)
Unity中实现点选RenderTexture中的3D模型
Deployment应用生命周期与Pod健康检查
为什么黑客领域几乎一片男生?
mongo enters error
The principle of hough transform detection of straight lines (opencv hough straight line detection)
R语言ggplot2可视化:使用ggpubr包的ggboxplot函数可视化分组箱图、使用ggpar函数改变图形化参数(caption、添加、修改可视化图像的题注、脚注内容)
Excel quickly aligns the middle name of the table (two-word name and three-word name alignment)
如何进行需求分析评审
TRACE32 - SNOOPer-based variable logging
Why don't you make a confession during the graduation season?
Jmeter常用的十大组件