当前位置:网站首页>Ansible中的inventory主機清單(預祝你我有數不盡的鮮花和浪漫)
Ansible中的inventory主機清單(預祝你我有數不盡的鮮花和浪漫)
2022-07-07 04:57:00 【Steve lu】
前言
本篇博客主要解釋Ansible主機清單的相關配置知識
一、inventory 主機清單

Inventory支持對主機進行分組,每個組內可以定義多個主機,每個主機都可以定義在任何一個或多個主機組內。
如果是名稱類似的主機,可以使用列錶的方式錶示各個主機
vim /etc/ansible/hosts
[dbservers]
192.168.109.131:2222 #冒號後定義遠程連接端口,默認是ssh的22端口
192.168.109.13[1:3] #範圍是:192.168.109.131到192.168.109.133
[[email protected] ansible]# ansible dbservers -a 'date'
192.168.109.133 | CHANGED | rc=0 >>
2022年 07月 06日 星期三 14:43:18 CST
192.168.109.132 | CHANGED | rc=0 >>
2022年 07月 06日 星期三 14:43:18 CST
192.168.109.131 | CHANGED | rc=0 >>
2022年 07月 06日 星期三 14:43:18 CST
[dbservers]
db-[a:f].example.org #支持主機名匹配 a~f


1.1 inventory 中的變量
| Inventory變量名 | 含義 |
|---|---|
| ansible_host | ansible連接節點時的IP地址 |
| ansible_port | 連接對方的端口號,ssh連 接時默認為22 |
| ansible_user | 連接對方主機時使用的主機名。不指定時,將使用執行ansible或ansible-playbook命令的用戶 |
| ansible_passwd | 連接時的用戶的ssh密碼,僅在未使用密鑰對驗證的情况下有效 |
| ansible_ssh_private_key_file | 指定密鑰認證ssh連接時的私鑰文件 |
| ansible_ssh_common_args | 提供給ssh、sftp、 scp命 令的額外參數 |
| ansible_become | 允許進行權限提昇 |
| ansible_become_method | 指定提昇權限的方式,例如可使用sudo/su/runas等方式 |
| ansible_become_user | 提昇為哪個用戶的權限,默認提昇為root |
| ansible_become_password | 提昇為指定用戶權限時的密碼 |
1.2 主機變量
#修改被管理客戶端192.168.109.134 ssh配置文件17行修改端口號
systemctl stop firewalld.service
setenforce 0
vim /etc/ssh/sshd_config
Port 2222
systemctl restart sshd
#這臺客戶端我沒有配置免密登錄
#修改Ansible管理服務器主機清單配置文件
vim /etc/ansible/hosts
[lhq]
192.168.109.134 ansible_port=2222 ansible_user=root ansible_password=123123
#不建立將密碼直接寫入配置,不安全
ansible dbservers -a 'date'



1.3 組變量
#删除客戶端已有的免密登錄密鑰文件
#客戶端配置
cd
cd .ssh/
rm -rf authorized_keys
#ansible控制端配置
vim /etc/ansible/hosts
[webservers]
192.168.109.131
192.168.109.132
192.168.109.133
[webservers:vars]
#錶示為 webservers 組內所有主機定義變量
ansible_user=root
ansible_password=123456
[[email protected] ansible]# ansible webservers -a 'date'
192.168.109.132 | CHANGED | rc=0 >>
2022年 07月 06日 星期三 17:34:46 CST
192.168.109.134 | CHANGED | rc=0 >>
2022年 07月 06日 星期三 17:34:46 CST
192.168.109.133 | CHANGED | rc=0 >>
2022年 07月 06日 星期三 17:34:46 CST
192.168.109.131 | CHANGED | rc=0 >>
2022年 07月 06日 星期三 17:34:46 CST


1.4 組嵌套
vim /etc/ansible/hosts
[webservers]
192.168.239.20
192.168.239.30
192.168.239.40
[webservers:vars]
ansible_user=root
ansible_password=1999612
[dbservers]
192.168.239.50:2222 ansible_user=root ansible_password=1999612
[webservers1:children]
webservers
dbserers
#錶示為 webservers1 主機組中包含了webservers組和dbservers組內的所有主機


边栏推荐
- Fiance donated 500million dollars to female PI, so that she didn't need to apply for projects, recruited 150 scientists, and did scientific research at ease!
- A simple and beautiful regression table is produced in one line of code~
- Oracle - views and sequences
- 日常工作中程序员最讨厌哪些工作事项?
- MySQL forgot how to change the password
- [practice leads to truth] is the introduction of import and require really the same as what is said on the Internet
- STM32封装ESP8266一键配置函数:实现实现AP模式和STA模式切换、服务器与客户端创建
- Run the command once per second in Bash- Run command every second in Bash?
- 谈谈讲清楚这件事的重要性
- Chapter 9 Yunji datacanvas company won the highest honor of the "fifth digital finance innovation competition"!
猜你喜欢

Lessons and thoughts of the first SQL injection

Kivy tutorial of setting the size and background of the form (tutorial includes source code)

Flex layout and usage

Win11 control panel shortcut key win11 multiple methods to open the control panel

offer如何选择该考虑哪些因素

Markdown编辑器

What about the collapse of win11 playing pubg? Solution to win11 Jedi survival crash

DFS and BFS concepts and practices +acwing 842 arranged numbers (DFS) +acwing 844 Maze walking (BFS)

Introduction to the PureMVC series

Common Oracle SQL statements
随机推荐
Up to 5million per person per year! Choose people instead of projects, focus on basic scientific research, and scientists dominate the "new cornerstone" funded by Tencent to start the application
Windows are not cheap things
sscanf,sscanf_ S and its related usage "suggested collection"
Depth first traversal template principle of tree and graph
MySQL forgot how to change the password
窗口可不是什么便宜的东西
What if the win11 screenshot key cannot be used? Solution to the failure of win11 screenshot key
微信能开小号了,拼多多“砍一刀”被判侵权,字节VR设备出货量全球第二,今日更多大新闻在此
JS variable plus
What is Web3
Win11 control panel shortcut key win11 multiple methods to open the control panel
Camera calibration (I): robot hand eye calibration
赠票速抢|行业大咖纵论软件的质量与效能 QECon大会来啦
使用Thread类和Runnable接口实现多线程的区别
Introduction to the PureMVC series
Leetcode notes
树与图的深度优先遍历模版原理
What if win11 pictures cannot be opened? Repair method of win11 unable to open pictures
Can I specify a path in an attribute to map a property in my class to a child property in my JSON?
史上最全学习率调整策略lr_scheduler