【发布时间】:2019-08-27 08:33:43
【问题描述】:
我想动态复制以下静态清单:
novaclient ansible_connection=local
[masters]
192.168.13.236
[nodes]
192.168.13.201
192.168.13.237
[cluster:children]
masters
nodes
阅读 ansible 文档,add_host 模块似乎是合适的候选者,因为它旨在将主机(或者组)添加到 ansible-playbook 内存清单。这是我的做法:
- hosts: all
connection: local
vars:
ips_per_group:
- ["masters", "192.168.13.236"]
- ["nodes" , "192.168.13.201"]
- ["nodes" , "192.168.13.237"]
tasks:
- name: add host dynamically
add_host:
name: "{{ item[1] }}"
groups: "{{ item[0] }}"
loop: "{{ ips_per_group }}"
- name: add masters and nodes groups to cluster group
add_host:
name: "{{ item[0] }}"
groups: "cluster"
loop: "{{ ips_per_group }}"
- name: test
debug:
var: groups
运行该剧本会触发以下警告:
[WARNING]: Found both group and host with same name: masters
[WARNING]: Found both group and host with same name: nodes
据我了解,这些警告来自以下事实:masters 和 nodes 尚未声明为 cluster 组的 children。尝试以下语法:
- name: add masters and nodes groups to cluster group
add_host:
name: "{{ item }}"
groups: "cluster:children"
loop:
- masters
由于groups 名称中的冒号而触发以下警告:
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
你有什么想法在没有任何警告的情况下这样做吗?
谢谢
【问题讨论】:
标签: ansible