ansible主机清单配置以及变量解释(二)

简介: 1.配置ansible主机清单清单文件位于/etc/ansible/hosts

1.配置ansible主机清单

清单文件位于/etc/ansible/hosts

[root@ansible ~]# vim /etc/ansible/hosts 
[web_clust]     //定义清单名
192.168.81.220      //主机ip
192.168.81.230
192.168.81.240

2.验证ansible是否可用

[root@ansible ~]# ansible web_clust -m ping
192.168.81.240 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
192.168.81.230 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
192.168.81.220 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

如果提示permission denied(publickey.gssapi-keyex…)表示ssh生成的公钥有问题

3.ansible命令语法格式

ansible 主机组模块名 -m 模块名 -a 指定利用模块执行的动作参数 批量执行操作动作image.png

command模块和shell模块的区别

command模块只是调用一个命令,不能添加类似|、&&、;、>这种的符号,也不能调用命令别名,shell模块相当于提供一个shell环境,可以使用管道、定向、并且等这种逻辑性的符号

3.1.获取主机清单中所有服务器的主机名

[root@ansible ~]# ansible web_clust -m command -a "hostname"
192.168.81.230 | CHANGED | rc=0 >>
nfs
192.168.81.240 | CHANGED | rc=0 >>
backup
192.168.81.220 | CHANGED | rc=0 >>
web

3.2.获取主机清单中所有服务器的ip地址

[root@ansible ~]# ansible web_clust -m shell -a "ifconfig ens33 | awk '{if(NR==2){print $1}}'"
192.168.81.240 | CHANGED | rc=0 >>
        inet 192.168.81.240  netmask 255.255.255.0  broadcast 192.168.81.255
192.168.81.230 | CHANGED | rc=0 >>
        inet 192.168.81.230  netmask 255.255.255.0  broadcast 192.168.81.255
192.168.81.220 | CHANGED | rc=0 >>
        inet 192.168.81.220  netmask 255.255.255.0  broadcast 192.168.81.255

4ansible清单配置

inventory文件通常用于定义要管理主机的认证信息,例如ssh登录用户名、登录密码以及key相关信息。清单位置文件位于/etc/ansible/hosts


可以配置主机、主机组


主机:


1.支持主机名通配以及正则表达式web[1:3].oldboy.com/192.168.81.2[2:4]

2.支持基于非标准的ssh端口,例如web1.oldboy.com:666

3.支持指定变量,可以对个别主机进行特殊配置

主机组:


1.支持嵌套组,例如[web_clust:children],web_clust模块下的所有主机都会被包含

2.指定变量,例如[web_clust:vars]在下面指定变量将会对所有web_clust模块中的所有主机生效

4.1.清单配置实例

添加三台主机至web_clust(升级版)

[root@ansible ~]# vim /etc/ansible/hosts
[web_clust]
192.168.81.220
192.168.81.230
192.168.81.240

添加三台主机至web_clust(升级版)

[root@ansible ~]# vim /etc/ansible/hosts
[web_clust]
192.168.81.2[2:4]0

4.2.清单配置实例2

添加三台主机并指定密码(初版)

[root@ansible ~]# vim /etc/ansible/hosts
[web_clust]
192.168.81.220  ansible_ssh_pass='redhat'
192.168.81.230  ansible_ssh_pass='redhat'
192.168.81.240  ansible_ssh_pass='redhat'

添加三台主机并指定密码(升级版)

[root@ansible ~]# vim /etc/ansible/hosts
[web_clust]
192.168.81.2[2:4]0  
[web_clust:vars]
ansible_ssh_pass='redhat'

添加三台主机并指定密码(拆分版)

[root@ansible ~]# vim /etc/ansible/hosts
[web_clust]
192.168.81.220  
192.168.81.230  
192.168.81.240  
[web_clust:vars]
ansible_ssh_pass='redhat'

5.ansible清单配置真实案例

5.1.环境概述

image.png

5.2.安装ansible

[root@ansible ~]# yum -y install ansible
[root@ansible ~]# ansible --version
ansible 2.9.9

5.3.配置主机清单

常用的配置是对应的主机做成对应的模块,然后将所有模块加入到主机组中

[root@ansible ~]# vim /etc/ansible/hosts 
[web]
192.168.81.220
[nfs]
192.168.81.230
[mysql]
192.168.81.240
[web_clust:children]
web
nfs
backup

5.4.验证

既可以对整个组进行操作
[root@ansible ~]# ansible web_clust -m ping
192.168.81.230 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
192.168.81.240 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
192.168.81.220 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
也可以对单个模块进行操作
[root@ansible ~]# ansible web -m ping
192.168.81.220 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
[root@ansible ~]# ansible nfs -m ping
192.168.81.230 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
[root@ansible ~]# ansible mysql -m ping
192.168.81.240 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

6.主机清单常用变量image.png

目录
相关文章
|
9月前
|
运维 Kubernetes 网络安全
Ansible自动化运维工具之主机管理与自定义配置文件(2)
Ansible自动化运维工具之主机管理与自定义配置文件(2)
103 0
|
2月前
|
关系型数据库 MySQL 网络安全
ansible 深入介绍之 主机清单与playbook
ansible 深入介绍之 主机清单与playbook
|
2月前
|
运维 关系型数据库 MySQL
Ansible自动化运维工具主机清单配置
Ansible自动化运维工具主机清单配置
|
2月前
|
网络协议 Shell
Ansible 学习笔记 - 定位主机和组的模式
Ansible 学习笔记 - 定位主机和组的模式
|
9月前
|
Kubernetes Linux 持续交付
在 Alibaba Cloud Linux 上搭建并配置 Ansible
本场景简单介绍了在Alibaba Cloud Linux上安装并配置Ansible的方式。
|
11月前
|
运维 监控
【运维知识进阶篇】Zabbix5.0稳定版详解10(Zabbix自动注册+Ansible自动部署,实现一条命令监控任意主机)
【运维知识进阶篇】Zabbix5.0稳定版详解10(Zabbix自动注册+Ansible自动部署,实现一条命令监控任意主机)
133 0
|
10月前
|
Linux 网络安全 数据安全/隐私保护
在 Alibaba Cloud Linux 上配置 Ansible
本场景是在 Alibaba Cloud Linux 上配置 Ansible
188 0
|
11月前
|
缓存 运维 监控
【运维知识进阶篇】Ansible变量详解(变量定义+变量优先级+变量注册+层级定义变量+facts缓存变量)
【运维知识进阶篇】Ansible变量详解(变量定义+变量优先级+变量注册+层级定义变量+facts缓存变量)
165 0
|
11月前
|
运维 Ubuntu 应用服务中间件
【运维知识进阶篇】Ansible自动化运维-Ansible安装与主机列表
【运维知识进阶篇】Ansible自动化运维-Ansible安装与主机列表
145 0
|
11月前
|
网络协议 网络安全 数据安全/隐私保护
Ansible模块介绍——配置网络模块、上传下载文件模块
Ansible模块介绍——配置网络模块、上传下载文件模块
304 0