ansible-playbook 要进行默认变量的生产,可以依靠jinja 的模板渲染功能
看几个官方给出的例子
调用setup 中的变量 例如 setup 中的变量层级为 ansible_eth0_ipv4_address ---->['ansible_facts']['eth0']['ipv4']['address']
{% for host in groups['app_servers'] %} {{ hostvars[host]['ansible_facts']['eth0']['ipv4']['address'] }} {% endfor %}
调用 inventory中的变量
{{ hostvars['test.example.com']['ansible_facts']['distribution'] }}
{% if 'webserver' in group_names %} # some part of a configuration file that only applies to webservers {% endif %}
调用运行时变量ansible_play_hosts
is the full list of all hosts still active in the current play
综合以上文档思考得出我们play book 如下
TMP_NODES: "{% for h in ansible_play_hosts %}{{ hostvars[h]['name'] }}=http://{{ hostvars[h]['ansible_facts']['default_ipv4']['address'] }}:20002,{% endfor %}" ETCD_NODES: "{{ TMP_NODES.rstrip(',') }}"
一定要在外层使用双引号