如何建立一个ansible库存
how to build an ansible inventory
我需要 ansible inventory 中的一些结构来做这样的事情:
我有多个地址(有不同的项目),每个地址都有多个队列(有不同的项目)我如何为此建立清单文件。
像这样:
addresses:
- name: "a-address"
type: "anycast"
read_username: "read"
read_password: "xxxxx"
write_username: "write"
write_password: "xxxx"
- queue_name: "a-queue1"
queue_type: xxxx
- queue_name: "a_queue2"
queue_type: xvx
- name: "b-address"
type: "multicast"
read_username: "readb"
read_password: "xxxxx"
write_username: "writeb"
write_password: "xxxx"
- queue_name: "b-queue1"
queue_type: xvx
- queue_name: "b_que
我该怎么做以及我应该如何在 ansible 中完成这个过程?
问:"如何构建库存文件?"
A:这是库存的YAML version。我添加了变量 queues 使其成为有效的 YAML
shell> cat hosts.yml
all:
hosts:
a-address:
type: anycast
read_username: read
read_password: xxxxx
write_username: write
write_password: xxxx
queues:
- queue_name: a-queue1
queue_type: xxxx
- queue_name: a_queue2
queue_type: xvx
b-address:
type: multicast
read_username: readb
read_password: xxxxx
write_username: writeb
write_password: xxxx
queues:
- queue_name: b-queue1
queue_type: xxxx
- queue_name: b_queue2
queue_type: xvx
例如剧本
- hosts: all
tasks:
- debug:
var: type
给出(删节)
ok: [a-address] =>
type: anycast
ok: [b-address] =>
type: multicast
问:"在地址上循环,并在该循环内在队列上循环。"
A:下面的剧本
- hosts: localhost
tasks:
- include_vars: hosts.yml
- debug:
msg: "{{ item.0.key }} {{ item.1 }}"
with_subelements:
- "{{ all.hosts|dict2items }}"
- value.queues
给出(删节)
msg: 'a-address {''queue_name'': ''a-queue1'', ''queue_type'': ''xxxx''}'
msg: 'a-address {''queue_name'': ''a_queue2'', ''queue_type'': ''xvx''}'
msg: 'b-address {''queue_name'': ''b-queue1'', ''queue_type'': ''xxxx''}'
msg: 'b-address {''queue_name'': ''b_queue2'', ''queue_type'': ''xvx''}'
我需要 ansible inventory 中的一些结构来做这样的事情:
我有多个地址(有不同的项目),每个地址都有多个队列(有不同的项目)我如何为此建立清单文件。
像这样:
addresses:
- name: "a-address"
type: "anycast"
read_username: "read"
read_password: "xxxxx"
write_username: "write"
write_password: "xxxx"
- queue_name: "a-queue1"
queue_type: xxxx
- queue_name: "a_queue2"
queue_type: xvx
- name: "b-address"
type: "multicast"
read_username: "readb"
read_password: "xxxxx"
write_username: "writeb"
write_password: "xxxx"
- queue_name: "b-queue1"
queue_type: xvx
- queue_name: "b_que
我该怎么做以及我应该如何在 ansible 中完成这个过程?
问:"如何构建库存文件?"
A:这是库存的YAML version。我添加了变量 queues 使其成为有效的 YAML
shell> cat hosts.yml
all:
hosts:
a-address:
type: anycast
read_username: read
read_password: xxxxx
write_username: write
write_password: xxxx
queues:
- queue_name: a-queue1
queue_type: xxxx
- queue_name: a_queue2
queue_type: xvx
b-address:
type: multicast
read_username: readb
read_password: xxxxx
write_username: writeb
write_password: xxxx
queues:
- queue_name: b-queue1
queue_type: xxxx
- queue_name: b_queue2
queue_type: xvx
例如剧本
- hosts: all
tasks:
- debug:
var: type
给出(删节)
ok: [a-address] =>
type: anycast
ok: [b-address] =>
type: multicast
问:"在地址上循环,并在该循环内在队列上循环。"
A:下面的剧本
- hosts: localhost
tasks:
- include_vars: hosts.yml
- debug:
msg: "{{ item.0.key }} {{ item.1 }}"
with_subelements:
- "{{ all.hosts|dict2items }}"
- value.queues
给出(删节)
msg: 'a-address {''queue_name'': ''a-queue1'', ''queue_type'': ''xxxx''}'
msg: 'a-address {''queue_name'': ''a_queue2'', ''queue_type'': ''xvx''}'
msg: 'b-address {''queue_name'': ''b-queue1'', ''queue_type'': ''xxxx''}'
msg: 'b-address {''queue_name'': ''b_queue2'', ''queue_type'': ''xvx''}'