首页 > 其他分享 >ansible inventory文件

ansible inventory文件

时间:2023-02-16 04:44:05浏览次数:49  
标签:文件 root lab ansible hosts inventory com example

目录

ansible inventory文件

在使用Ansible来批量管理主机的时候,通常我们需要先定义要管理哪些主机或者主机组,而这个用于管理主机与主机组的文件就叫做Inventory,也叫主机清单。该文件默认位于/etc/ansible/hosts。当然我们也可以通过修改ansible配置文件的hostfile配置项来修改默认inventory的位置。

定义主机和组

有五个主机

node1 192.168.132.131 主控端

node2 192.169.132.132 被控端

node3 192.169.132.133 被控端

node4 192.169.132.134 被控端

node5 192.169.132.135 被控端

直接写进hosts文件

对于/etc/ansible/hosts最简单的定义格式像下面,直接把IP写在里面:
[root@node1 ansible]# cat /etc/ansible/hosts

localhost
192.168.132.132
192.168.132.133
192.168.132.134
192.168.132.135

简单实用ping模块检测连通性

[root@node1 ansible]# ansible 192.168.132.132 -m ping

ansible 指定主机或者主机组 -m 后跟使用的模块,ping是ansible测试和被控端的连通性
image

是因为没有配置密码认证也没有配置ssh秘钥自动连接,主机之间不能交互
方法一:

修改配置,可以输入密码

[root@node1 ansible]# vim /etc/ansible/ansible.cfg
[defaults]

# some basic default values...

#inventory      = /etc/ansible/hosts
#library        = /usr/share/my_modules/
#module_utils   = /usr/share/my_module_utils/
#remote_tmp     = ~/.ansible/tmp
#local_tmp      = ~/.ansible/tmp
#plugin_filters_cfg = /etc/ansible/plugin_filters.yml
#forks          = 5
#poll_interval  = 15
#sudo_user      = root
#ask_sudo_pass = True
ask_pass      = True    #打开这个配置
#transport      = smart
#remote_port    = 22
#module_lang    = C
#module_set_locale = False

再次执行
image

方法二: 使用秘钥自动登录认证

恢复配置,注释掉ack_pass

[root@node1 ansible]# ssh-copy-id [email protected]

[root@node1 ansible]# ansible 192.168.132.132 -m ping

不用输入密码,也可以使用模块操作
image

方法三 使用hosts配置密码
修改hosts文件

[root@node1 ansible]# cat /etc/ansible/hosts

localhost
192.168.132.132  
192.168.132.133  ansible_ssh_pass=redhat
192.168.132.134
192.168.132.135

执行

[root@node1 ansible]# ansible 192.168.132.133 -m ping
image
在这里可以使用这种方式推送key到被控端,然后删掉hosts的密码配置

实用普通用户控制

配置普通用户

[root@node2 ~]# useradd ansible

[root@node2 ~]# vim /etc/sudoers.d/ansible

ansible       ALL=(ALL)       NOPASSWD: ALL

配置秘钥

[root@node2 ~]# mkdir  /home/ansible/.ssh
[root@node2 ~]# cp /root/.ssh/authorized_keys /home/ansible/.ssh/
[root@node2 ~]# chmod 600 /home/ansible/.ssh/authorized_keys
[root@node2 ~]# chown ansible:ansible /home/ansible/.ssh  -R
[root@node2 ~]# ll /home/ansible/.ssh/ -d
drwxr-xr-x 2 ansible ansible 29 Apr 27 10:52 /home/ansible/.ssh/
[root@node2 ~]# ll /home/ansible/.ssh/authorized_keys
-rw------- 1 ansible ansible 0 Apr 27 10:52 /home/ansible/.ssh/authorized_keys

主控段配置ansible用户
[root@node1 ansible]# vim ansible.cfg
remote_user = ansible
检查连通性
image

检查用户,使用shell模块
image

但是到133就会报错,用户被denied
image

按照相同方式配置其他几台机器配置ansible用户,并提权设置

hosts文件管理

使用主机名连接,则需要保证主机名可以被解析

[root@node1 ansible]# cat /etc/ansible/hosts

localhost
node2  
192.168.132.133 
192.168.132.134
192.168.132.135

不能解析
image

添加/etc/hosts

[root@node1 ansible]# vim /etc/hosts

192.168.132.132  node2  

[root@node1 ansible]# ansible node2 -m shell -a "whoami"

node2 | CHANGED | rc=0 >>
ansible

也可以配置/etc/ansivle/hosts

[root@node1 ansible]# vim /etc/ansible/hosts

localhost
node2  
node3 ansible_ssh_host=192.168.132.133 
192.168.132.134
192.168.132.135

也可以使用主机名
image

但是不能使用IP
image

连接自己[root@node1 ansible]# ansible localhost -m ping
image

也可以直接指定

[root@node1 ansible]# vim /etc/ansible/hosts

localhost  ansible_connection=local
node2  
node3 ansible_ssh_host=192.168.132.133 
192.168.132.134
192.168.132.135

主机分组

配置都使用主机组

简单配置

[root@node1 ansible]# vim /etc/hosts

192.168.132.131  node1  
192.168.132.132  node2  
192.168.132.133  node3  
192.168.132.134  node4  
192.168.132.135  node5  

[root@node1 ansible]# vim /etc/ansible/hosts

[web]
node1
node2
[mysql]
node3
node4
[redis]
node5

执行

[root@node1 ansible]# ansible web -m ping
image

一个主机可位于多个组

[web]
node1 
node2
[mysql]  
node3 
node4
[redis]
node5
[php]
node1
node3

执行

[root@node1 ansible]# ansible php -m shell -a "hostname"
image

# 中括号中的名字代表组名,可以根据自己的需求将庞大的主机分成具有标识的组,如上面分了两个组webservers和dbservers组;

# 主机(hosts)部分可以使用域名、主机名、IP地址表示;当然使用前两者时,也需要主机能反解析到相应的IP地址,一般此类配置中多使用IP地址;
node4
[web]
node1 
node2
[mysql]  
node3 
node4
[redis]
node5
[php]
node1
node3

hosts的名字可以使用其他的文件名,但是默认找hosts文件,需要用到其他文件

[root@node1 ansible]# cp hosts inventory
[root@node1 ansible]# ansible -i inventory php -m shell -a "hostname"
image
也可以使用飞hosts命名的文件

指定主机范围

# 下面指定了从web-node01到web-node50,webservers组共计50台主机;databases组有db-node-a到db-node-f共6台主机
[webservers]
web-node[01:50].test.com
[databases]
db-node[a:f].test.com

定义主机组嵌套

# 如下示例中,production组包含两个子组,分别为webservers和dbservers,webservers和dbservers组分别包含若干主机
[webservers]
node1
node2
[dbservers]
node3

[php:children]
webservers
dbservers

执行

[root@node1 ansible]# ansible php -m shell -a "echo hostname"

node3 | CHANGED | rc=0 >>
hostname
node2 | CHANGED | rc=0 >>
hostname
node1 | CHANGED | rc=0 >>
hostname

复制代码
两个主机组都有打印

选择主机与组

在前面定义Inventory的时候,我们会把所有被管理主机通过主机组的方式定义到Inventory当中,但是当我们实际使用的时候,可能只需要对某一主机或主机组进行操作,这个时候就需要通过匹配的方式指定某一特定主机或主机组。
在此之前,先定义一个主机清单示例:

[root@node1 ansible]# cat /etc/ansible/hosts

srv1.example.com
srv2.example.com
s1.lab.example.com
s2.lab.example.com

[web]
jupiter.lab.example.com
saturn.example.com

[db]
db1.example.com
db2.example.com
db3.example.com

[lb]
lb1.lab.example.com
lb2.lab.example.com

[boston]
db1.example.com
jupiter.lab.example.com
lb2.lab.example.com

[london]
db2.example.com
db3.example.com
file1.lab.example.com
lb1.lab.example.com

[dev]
web1.lab.example.com
db3.example.com

[stage]
file2.example.com
db2.example.com

[prod]
lb2.lab.example.com
db1.example.com
jupiter.lab.example.com

[function:children]
web
db
lb
city

[city:children]
boston
london
environments

[environments:children]
dev
stage
prod
new

[new]
172.25.252.23
172.25.252.44

匹配所有主机

可以通过all或者*来指定匹配所有主机,通过如下指令查看all匹配到的主机:

[root@node1 ansible]# ansible all --list-hosts

或者
image

匹配指定的主机或主机组

匹配单个组

[root@node1 ansible]# ansible prod --list-hosts
image

匹配单个主机

[root@node1 ansible]# ansible db2.example.com --list-hosts

  hosts (1):
    db2.example.com

匹配多个主机
[root@node1 ansible]# ansible 'lb1.lab.example.com,s1.lab.example.com,db1.example.com' --list-hosts

  hosts (3):
    lb1.lab.example.com
    s1.lab.example.com
    db1.example.com

匹配多个组

[root@node1 ansible]# ansible 'london,boston' --list-hosts

  hosts (7):
    db2.example.com
    db3.example.com
    file1.lab.example.com
    lb1.lab.example.com
    db1.example.com
    jupiter.lab.example.com
    lb2.lab.example.com

匹配不属于任何组的主机
[root@node1 ansible]# ansible ungrouped --list-hosts

  hosts (4):
    srv1.example.com
    srv2.example.com
    s1.lab.example.com
    s2.lab.example.com

通配符匹配

匹配'*.example.com':

[root@node1 ansible]# ansible '*.example.com' --list-hosts

hosts (14):
    s1.lab.example.com
    file1.lab.example.com
    lb1.lab.example.com
    srv2.example.com
    db3.example.com
    srv1.example.com
    web1.lab.example.com
    db2.example.com
    db1.example.com
    jupiter.lab.example.com
    lb2.lab.example.com
    file2.example.com
    s2.lab.example.com
    saturn.example.com

匹配172.25.*的主机:

[root@node1 ansible]# ansible '172.25.*' --list-hosts

[root@node1 ansible]# ansible '172.25.*' --list-hosts

  hosts (2):
    172.25.252.23
    172.25.252.44

匹配以s开头的主机及主机组:
[root@node1 ansible]# ansible 's*' --list-hosts

  hosts (7):
    file2.example.com
    db2.example.com
    s1.lab.example.com
    srv2.example.com
    srv1.example.com
    s2.lab.example.com
    saturn.example.com

通配符组合匹配

匹配包含.example.com但不包含.lab.example.com的主机:

[root@node1 ansible]# ansible '.example.com,!.lab.example.com' --list-hosts
复制代码
hosts (7):
srv2.example.com
db3.example.com
srv1.example.com
db2.example.com
db1.example.com
file2.example.com
saturn.example.com
复制代码
匹配包含prod以及172开头、包含lab关键字的主机或组

[root@node1 ansible]# ansible 'prod,172,lab*' --list-hosts

  hosts (10):
    lb2.lab.example.com
    db1.example.com
    jupiter.lab.example.com
    172.25.252.23
    172.25.252.44
    s1.lab.example.com
    file1.lab.example.com
    lb1.lab.example.com
    web1.lab.example.com
    s2.lab.example.com

匹配属于db组同时还属于london组的主机:
[root@node1 ansible]# ansible 'db,&london' --list-hosts

  hosts (2):
    db2.example.com
    db3.example.com

匹配在london组或者boston组,还必须在prod组中且必须不在lb组中的主机:
[root@node1 ansible]# ansible 'boston,london,&prod,!lb' --list-hosts

  hosts (2):
    db1.example.com
    jupiter.lab.example.com

正则表达式匹配

在开头的地方使用”~”,用来表示这是一个正则表达式:

[root@node1 ansible]# ansible '~(s|db).*example.com' --list-hosts

hosts (8):
    s1.lab.example.com
    srv2.example.com
    db3.example.com
    srv1.example.com
    db2.example.com
    db1.example.com
    s2.lab.example.com
    saturn.example.com

通过--limit明确指定主机或组

通过--limit在选定的组中明确指定主机:

[root@node1 ansible]# ansible ungrouped --limit srv1.example.com --list-hosts

  hosts (1):
    srv1.example.com

通过--limit参数,还可以指定一个文件,该文件中定义明确指定的主机的列表,定义一个retry_hosts.txt如下:

[root@node1 ansible]# vim retry_hosts.txt

srv1.example.com

[root@node1 ansible]# ansible ungrouped --limit @retry_hosts.txt --list-hosts

  hosts (1):
    srv1.example.com

4.8 通配符和正则表达式配合使用

[root@node1 ansible]# ansible '~(s|db).,prod,.lab.example.com' --list-hosts

  hosts (14):
    db1.example.com
    db2.example.com
    db3.example.com
    file2.example.com
    s1.lab.example.com
    srv2.example.com
    srv1.example.com
    s2.lab.example.com
    saturn.example.com
    lb2.lab.example.com
    jupiter.lab.example.com
    file1.lab.example.com
    lb1.lab.example.com
    web1.lab.example.com

主机组示例结束

参考文档

参考了一位大佬的文档,欢迎大家关注他 https://www.cnblogs.com/zyxnhr/p/12788339.html

标签:文件,root,lab,ansible,hosts,inventory,com,example
From: https://www.cnblogs.com/liwenchao1995/p/17125328.html

相关文章

  • ansible 简介和基本安装
    目录ansible简介和基本安装自动化运维运维的自动化发展历程运维工程师的职能划分自动化运维的应用场景企业实际应用场景分析Dev开发环境测试环境发布环境生产环境灰度环境......
  • spring管理配置文件实现注入
    创建配置文件  写入以下内容:  创建配置文件的bean:  <beanid="configProperties"class="org.springframework.beans.factory.config.PropertiesFact......
  • Jdbc_Druid_Utils_V2连接池-需要在src文件夹下创建配置文件druid.properties
    packagecom.atguigu.api.utils;importcom.alibaba.druid.pool.DruidDataSourceFactory;importjavax.sql.DataSource;importjava.io.IOException;importjava.io.InputS......
  • JavaWeb文件上传(感谢狂神)
    1、准备工作采用Apache的开源工具common-fileupload这个文件上传组件。common-fileupload是依赖于common-io这个包的,所以还需要下载这个包。(这两个jar包需要下载引入,Tomc......
  • 文件的操作
    文件的属性是用一个结构体来封装的: 文件属性的操作函数: 文件的类型:  函数操作: 文件权限:  access函数(检查指定文件是否具有某种操作)头文件:#i......
  • 文件类型
    声明TypeTMingRiFile=fileofinteger;文件操作varmyfile:textfile;//定义一个文本文件beginAssignfile(myfile,'c:\Ikh001.txt');Rewrite(myfile);/......
  • 【C++批量生成文件夹】
    1、使用C++创建文件夹需要添加头文件;#include<direct.h>stringfileName;mkdir(fileName.to_str())2、mkdir()如果文件夹已存在,则不会创建新的文件夹,但是mkdir函数......
  • Linux 压缩文件用法
    #tar命令:可以用来压缩或解压缩文件:#压缩tar-czvffilename.tar.gzfiles#解压缩tar-xzvffilename.tar.gz#gzip命令:可以用来压缩或解压缩文件:#压缩gzipf......
  • Linux基础——文件权限、搜索查找、解压压缩、磁盘管理、进程管理、软件包管理
    一、文件权限Linux系统是一种典型的多用户系统,不同的用户处于不同的地位,拥有不同的权限。为了保护系统的安全性,Linux系统对不同的用户访问同一文件(包括目录文件)的权限......
  • 【批量生成文件夹】
     1、使用C++创建文件夹需要添加头文件;#include<direct.h>stringfileName;mkdir(fileName.to_str())2、mkdir()如果文件夹已存在,则不会创建新的文件夹,但是mkdir函......