Ansible 进阶 (1) inventory

前言 Ansible 配置

主要包括:
主机清单文件”inventory”,extra module放置路径”library” ,远程主机的临时文件位置” remote_tmp” ,管理节点上临时文件的位置”local_tmp”

inventory      = /etc/ansible/hosts

library        = /usr/share/my_modules/

remote_tmp     = $HOME/.ansible/tmp

local_tmp      = $HOME/.ansible/tmp

优先级

ansible的默认配置文件是/etc/ansible/ansible.cfg。其实ansible会按照下面的顺序查找配置文件,并使用第一个发现的配置文件。

  • ANSIBLE_CONFIG (an environment variable)
  • ansible.cfg (in the current directory)
  • .ansible.cfg (in the home directory)
  • /etc/ansible/ansible.cfg

1 Host Inventory (主机清单)

主机清单,告诉ansible需要管理哪些server,和server的分类和分组信息。可以根据你自己的需要根据地域分类,也可以按照功能的不同分类。

位置 :/etc/ansible/hosts

若想修改修改主机目录

vim /etc/ansible/ansible.cfg

...
inventory      = /etc/ansible/hosts
...

1.1 分组

  1. 简单分组
    简单的分组[]内是组名
mail.example.com

[webservers]
foo.example.com
bar.example.com

[dbservers]
one.example.com
two.example.com
three.example.com

[webservers]
www[01:50].example.com

[databases]
db-[a:f].example.com
  1. 分组usa的子组还可以是其它的组,例如[usa:children]中还可以包含southeast子组, [southeast:children]中还可以包含atlanta和releigh
[atlanta]
host1
host2

[raleigh]
host2
host3

[southeast:children]
atlanta
raleigh


[usa:children]
southeast
northeast
southwest
northwest

1.2 连接参数和变量

  1. 参数
    指定Server的连接参数,其中包括连接方法,用户等
[targets]

localhost              ansible_connection=local
other1.example.com     ansible_connection=ssh        ansible_user=mpdehaan
other2.example.com     ansible_connection=ssh        ansible_user=mdehaan


[atlanta]
host1 http_port=80 maxRequestsPerChild=808
host2 http_port=303 maxRequestsPerChild=909
  1. 变量
[atlanta]
host1
host2

[atlanta:vars]
ntp_server=ntp.atlanta.example.com
proxy=proxy.atlanta.example.com
  1. 按目录结构存储变量
    假设inventory文件为/etc/ansible/hosts,那么相关的hosts和group变量可以放在下面的目录结构下
/etc/ansible/group_vars/raleigh # can optionally end in '.yml', '.yaml', or '.json'
/etc/ansible/group_vars/webservers
/etc/ansible/host_vars/foosball

/etc/ansible/group_vars/raleigh 文件内容可以为

---
ntp_server: acme.example.org
database_server: storage.example.org

如果对应的名字为目录名,ansible会读取这个目录下面所有文件的内容

/etc/ansible/group_vars/raleigh/db_settings
/etc/ansible/group_vars/raleigh/cluster_settings

group_vars/ 和 host_vars/ 目录可放在 inventory 目录下,或是 playbook 目录下. 如果两个目录下都存在,那么 playbook 目录下的配置会覆盖 inventory 目录的配置.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不会长胖的斜杠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值