当前位置:网站首页>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中的信息
单元小结
边栏推荐
猜你喜欢
随机推荐
7. Summary of common interview questions
Female service community product design
【MySQL】Mysql范式及外键作用
MySQL数据库操作
01 Encounter typescript, build environment
The principle of hough transform detection of straight lines (opencv hough straight line detection)
JVM参数解析 Xmx、Xms、Xmn、NewRatio、SurvivorRatio、PermSize、PrintGC「建议收藏」
Grafana安装后web打开报错
RecyclerView的高效使用第一节
NC | 中国农大草业学院杨高文组揭示发现多因子干扰会降低土壤微生物多样性的积极效应...
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
TRACE32——常用操作
hough变换检测直线原理(opencv霍夫直线检测)
leetcode303 Weekly Match Replay
Ubuntu Topic 5: Setting a Static IP Address
Deployment application life cycle and Pod health check
After Grafana is installed, the web opens and reports an error
The use of button controls
MySQL的相关问题
R语言ggplot2可视化:使用ggpubr包的ggmaplot函数可视化MA图(MA-plot)、font.legend参数和font.main参数设置标题和图例字体加粗









