Inventory文件对主机名分组的写法

关于Inventory文件的写法,共有两种方式:
1. ini方式
2. yaml方式

一、 一个简单的Inventory文件
(ansible) [[email protected] initNodes]# cat /opt/ansible/hosts
[master]
main.plus7s.com
[node]
n1.plus7s.com
n2.plus7s.com
n3.plus7s.com
二、 Inventory文件中多个连续主机的缩写方法:
如之前的Inventory文件,可以写成如下格式:
(ansible) [[email protected] initNodes]# cat /tmp/hosts
[master]
main.plus7s.com
[node]
n[1:3].plus7s.com

(ansible) [[email protected] ~]# ansible -i /tmp/hosts node -m ping
n1.plus7s.com | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
n2.plus7s.com | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
n3.plus7s.com | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
(ansible) [[email protected] ~]#

三、此外,Inventory文件也可以通过yaml的形式来表达:
  几个要点:
1. 最上面的”all:“不可省
2. 每一个分组,都需要用"children:"标签进行标示,并在下一锁进使用hosts: 标示列出所有主机名或IP
3. 每一个主机名或IP后必须要有“:”
4. 主机名或IP的下一级缩进中可以增加该主机的主机属性,比如端口、地址等……
(ansible) [[email protected] ansible]# cat hosts.yaml
all:
  hosts:
  children:
    master:
      hosts:
        10.1.1.1:
    nodes:
      hosts:
        n1.plus7s.com:
        n2.plus7s.com:
        n3.plus7s.com:
    db:
      hosts:
        dbmix1.plus7s.com:
          ansible_hosts: 1.1.1.1
          ansible_port: 5307


(ansible) [[email protected] ansible]#
This entry was posted in Ansible and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *