Change variable in Ansible template based on group

前端 未结 4 1458
自闭症患者
自闭症患者 2021-02-05 19:25

I\'ve got an Ansible inventory file a bit like this:

[es-masters]
host1.my-network.com

[es-slaves]
host2.my-network.com
host3.my-network.com

[es:children]
es-m         


        
4条回答
  •  耶瑟儿~
    2021-02-05 20:01

    You do it the other way around. You check if the identifier (hostname or IP or whatever is in your inventory) is in the defined group. Not if the group is in the hostvars.

    {% if ansible_fqdn in groups['es-masters'] %}
    node_master=true
    {% else %}
    node_master=false
    {% endif %}
    

    But, what you better should do is this:

    Provide default in template

    # role_name/templates/template.j2
    node_master={{ role_name_node_master | default(true) }}
    

    Than override in group_vars

    # group_vars/es-masters.yml
    role_name_node_master: false
    

提交回复
热议问题