Pass Ansible template variable from inventory group

Viewed 27

I have 2 different server groups in my hosts inventory configuration:

[tier1]
server1

[tier2]
server2

I need to upload a configuration file (app.conf) into the remote servers, in which I need to set a parameter (environment:) according to the inventory group (tier1 or tier2).

Playbook

- name: Add node
  hosts: all
  gather_facts: true
  become: true
  roles:
  - node

Template (app.conf.j2)

  {% if ansible_fqdn in groups['tier1'] %}
  environment: tier1
  {% else %}
  environment: tier2
  {% endif %}

main.yaml snippet

- name: Copy over app.conf configuration file
  template:
    src: app.conf.j2
    dest: /etc/app.conf

Output

changed: [server1]
changed: [server2]

Unfortunately it doesn't seem to be working, as the setup value in app.conf file is "tier2" in both servers...

ssh_service:
  enabled: "yes"
  labels:
    environment: ['tier2']

Am I missing something?

0 Answers
Related