Ansible 主机清单 Inventory文件

Ansible 主机清单 Inventory文件

Ansible 可同时操作属于一个组的多台主机,组和主机之间的关系通过 inventory 文件配置. 默认的文件路径为 /etc/ansible/hosts

修改Inventory前环境准备:(域名是自己随便起的)

node1-dns.shm.com :搭建DNS域名服务器,配置域名解析,192.168.10.9
node2-ansible.shm.com
node3-jenkins.shm.com
node4-centos.shm,com

基本的主机清单

#修改主机清单
[root@localhost ~]# cd /etc/ansible/
[root@localhost ansible]# vim /etc/ansible/hosts 
---
[dns]
192.168.10.9

[localhost]
192.168.10.20

[webserver]
192.168.10.21

[dbserver]
192.168.10.22

添加所有得远程主机IP地址,这种简单的主机清单只在测试环境学习环境使用,真实环境如下。

主机与组

/etc/ansible/hosts 文件的格式与windows的ini配置文件类似:

1、基本Inventory文件写法、本机写法
[root@localhost ansible]# vim /etc/ansible/hosts 

[localhost]
ansible.shm.com ansible_connection=local		#本机写法


[webservers]			#主机组
foo.example.com			#主机组域名主机
bar.example.com			#主机组域名主机

[dbservers]				#第二个主机组
one.example.com
two.example.com
three.example.com

1.方括号[]中是组名,用于对系统进行分类,便于对不同系统进行个别的管理
2.一个系统可以属于不同的组,比如一台服务器可以同时属于 webserver组 和 dbserver组
2、当被控端修改 SSH 端口号的 Inventory写法

(此时被控端为root用户)

1.修改被控端 SSH 端口
[root@localhost ~]# vim /etc/ssh/sshd_config 
Port 2222
AddressFamily any
ListenAddress 0.0.0.0
ListenAddress ::

2.开放防火墙端口,添加selinux允许ssh端口,防止重启失败
[root@localhost ~]# firewall-cmd --zone=public --add-port=2222/tcp --permanent
[root@localhost ~]# firewall-cmd --reload
[root@localhost ~]# sestatus -v | grep SELinux
[root@localhost ~]# rpm -qa | grep policycoreutils-python
[root@localhost ~]# semanage port -l | grep ssh

[root@localhost ~]# semanage port -a -t ssh_port_t -p tcp 2222
[root@localhost ~]# systemctl restart sshd
[root@localhost ~]# netstat -anpt | grep sshd
tcp        0      0 0.0.0.0:2222            0.0.0.0:*               LISTEN      60561/sshd    

验证访问,需要指定 ssh端口号
[root@localhost ~]# ssh -p 2222  root@192.168.10.22

3.修改Inventory文件
[dbserver]
centos.shm.com:2222

4、静态IP地址 、修改ssh端口 、 别名写法
[dbserver]
centos.shm.com:2222
centos ansible_ssh_port=2222 ansible_ssh_host=192.168.10.22

centos:别名,如果指定别名必须配置 静态IP参数
ansible_ssh_port: ssh修改后端口号
ansible_ssh_host: 静态主机IP地址

#通过别名的方式进行连接
[root@localhost ~]# ansible centos -m ping 
5、指定多个普通用户加密认证,修改ssh端口,别名连接。
1.创建两个用户,设置密码
zs 123.com
ls 123.com

2.通过2222端口号,发送公钥到zs的家目录,为zs用户提供免密登录,ls同理
[root@localhost ~]# ssh-copy-id -p 2222 -i /root/.ssh/id_rsa.pub  zs@192.168.10.22
[root@localhost ~]# ssh-copy-id -p 2222 -i /root/.ssh/id_rsa.pub  ls@192.168.10.22

3.验证
[root@localhost ~]# ssh -p 2222 zs@192.168.10.22
[root@localhost ~]# ssh -p 2222 ls@192.168.10.22

--------------没问题之后修改Inventory文件-------------

[dbserver]
zs  ansible_connection=ssh ansible_ssh_port=2222 ansible_ssh_user=zs ansible_ssh_host=192.168.10.22

ls ansible_connection=ssh ansible_ssh_port=2222 ansible_ssh_user=ls ansible_ssh_host=192.168.10.22

在这里插入图片描述

Inventory参数的说明

ansible_ssh_host
      将要连接的远程主机名.与你想要设定的主机的别名不同的话,可通过此变量设置.

ansible_ssh_port
      ssh端口号.如果不是默认的端口号,通过此变量设置.

ansible_ssh_user
      默认的 ssh 用户名

ansible_ssh_pass
      ssh 密码(这种方式并不安全,我们强烈建议使用 SSH 密钥)
      
ansible_connection
      与主机的连接类型.比如:local, ssh 

ansible_python_interpreter
      目标主机的 python 路径.适用于的情况: 系统中有多个 Python, 或者命令路径不是"/usr/bin/python"
	 与 ansible_python_interpreter 的工作方式相同,可设定如 ruby 或 perl 的路径....
例:
node1.python.com      ansible_python_interpreter=/usr/local/bin/python
node2.ruby.com	     ansible_ruby_interpreter=/usr/bin/ruby.1.9.3
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值