Ansible部署

本文介绍了Ansible的使用,包括构建Ansible清单,管理配置文件,使用不同模块执行任务,如user和group模块,以及如何查看帮助文档。重点讲解了静态清单的定义和验证,配置文件的优先级,以及如何通过模块执行临时命令。
摘要由CSDN通过智能技术生成

1.构建Ansible清单

1.1 Anxible清单的定义

定义了Ansible管理的一批主机名单,通过执行Ansible模块,在调用过程中直接对清单中的主机进行批量管理。这些主机也可以分配到组中,以进行集中管理。组可以包含子组,主机也可以是多个组的成员。
可以通过两种方式定义主机清单:

  • 静态主机清单可以通过文本文件定义。(定义完成之后,只有修改配置文件,否则无法发生变动)
  • 动态主机清单可以根据需要使用外部信息提供程序通过脚本或其他程序来生成。

1.2 使用静态清单指定受管主机

/etc/ansible/hosts是系统默认的静态清单文件,一般情况下我们是不会用这个地址。通常是进去/etc/ansoble/ansible.cfg文件中修改默认清单文件。

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

# some basic default values...

#inventory      = /etc/ansible/hosts			//默认清单位置
inventory      = /etc/ansible/inventory			//修改之后的清单位置

静态清单文件是指定Ansible目标受管主机的文本文件。可以使用多种不同的格式编写此文件,包括INI样式或YAML。

1.2.1 单独定义

每行一个主机名或IP地址

[root@localhost ansible]# cat inventory 
beta.example.org
192.168.237.156
192.168.237.157
1.2.2 组定义

受管主机组织为主机组,每一部分的开头为以中括号括起来的主机组名称。其后为该组中每一受管主机的主机名或IP地址,每行一个。

[root@localhost ansible]# cat inventory 
[webservers]			//主机组的名称
beta.example.org
192.168.237.156
192.168.237.157

www[001:006].example.com		//[001:006] 批量格式,即从001到006

[dbservers]
db01.intranet.mydomain.net
db02.intranet.mydomain.net
10.25.1.56
				//在组的下方的主机名或IP即属于主机组,不必考虑是否有空行
db-[99:101]-node.example.com	

1.3 验证清单

列出清单中所有主机: ansible all --list-hosts

列出不属于某个组的主机:ansible ungrouped --list-hosts

列出属于webservers组的主机:ansible webservers --list-hosts

列出指定清单所有主机:ansible all -i inventory --list-hosts 

列出指定清单不属于某个组的主机:ansible ungrouped -i inventory --list-hosts

列出指定清单属于webservers组的主机:ansible webservers -i inventory --list-hosts

使用 ansible 命令验证计算机是否存在于清单中:

[root@localhost ansible]# ansible db-99-node.example.com --list-hosts
  hosts (1):
    db-99-node.example.com

[root@localhost ansible]# ansible db-9999-node.example.com --list-hosts
[WARNING]: Could not match supplied host pattern, ignoring: db-9999-node.example.com
[WARNING]: No hosts matched, nothing to do
  hosts (0):

列出指定组中的所有主机:

[root@localhost ansible]# ansible webservers --list-hosts
  hosts (9):
    beta.example.org
    192.168.237.156
    192.168.237.157
    www001.example.com
    www002.exa
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值