# Different variable values based on hostname (3 hosts)

**URL:** <https://forum.ansible.com/t/different-variable-values-based-on-hostname-3-hosts/39629>\
**Category:** Get Help\
**Created:** [January 15, 2025, 4:06pm UTC](https://forum.ansible.com/t/different-variable-values-based-on-hostname-3-hosts/39629 "2025-01-15T16:06:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [January 15, 2025, 4:06pm UTC](https://forum.ansible.com/t/different-variable-values-based-on-hostname-3-hosts/39629/1 "2025-01-15T16:06:12Z")

</div>

I have a `template` tasks like

```auto
- name: template 'etcd.env'
  ansible.builtin.template:
    src: ../templates/templates/etcd.env.j2
    dest: "{{ etcd_bin_dir }}/.etcd.env"
    owner: "{{ ansible_become_user }}"
    group: "{{ ansible_become_user }}"
    mode: '0600'
  become: true

```

the template is:

```auto
ETCD_NAME="{{ etcd_name }}"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://{{ ansible_default_ipv4.address }}:2380"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_ADVERTISE_CLIENT_URLS="http://{{ ansible_default_ipv4.address }}:2379"

```

- I send this to 3 hosts.
- The value for `"{{ etcd_name }}"` should be different based on the hostname (identified by `inventory_hostname`, or `anisble_hostname`) of the target?

how to I set this up?

3 x `set_fact` with a `when` condtion for each host comes to mind, but I have a feeling having heard people mentionen `set_fact` not being really _ansible-ish_.

setting up a `host_var_dict` was also thrown at me, but I do not quite get how this practically would look like.

what I need is  
`etcd_host0: rd5428.dbinfra.internal` → `etcd_name: etcd_0`  
`etcd_host1: rd5429.dbinfra.internal` → `etcd_name: etcd_1`  
`etcd_host2: rd5230.dbinfra.internal` → `etcd_name: etcd_2`

any hint how to approach this?

the hostnames would be set into variable like  
`etcd0_host: d5428.dbinfra.internal`  
inside a vars file loaded into the play.

---

<div class="post-metadata">

**Author:** ![mariolenz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mariolenz/32/108_2.png) [@mariolenz](https://forum.ansible.com/u/mariolenz)\
**Post date:** [January 15, 2025, 4:13pm UTC](https://forum.ansible.com/t/different-variable-values-based-on-hostname-3-hosts/39629/2 "2025-01-15T16:13:47Z")

</div>

Call me dense, but not simply use [host variables](https://docs.ansible.com/ansible/latest/inventory_guide/intro_inventory.html#assigning-a-variable-to-one-machine-host-variables) in your inventory?

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [January 15, 2025, 4:16pm UTC](https://forum.ansible.com/t/different-variable-values-based-on-hostname-3-hosts/39629/3 "2025-01-15T16:16:15Z")

</div>

it is a dynamic inventory with potentially different machines per execution. So I was thinking of putting this inside the playbook/tasksfile.

---

<div class="post-metadata">

**Author:** ![mariolenz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mariolenz/32/108_2.png) [@mariolenz](https://forum.ansible.com/u/mariolenz)\
**Post date:** [January 15, 2025, 5:51pm UTC](https://forum.ansible.com/t/different-variable-values-based-on-hostname-3-hosts/39629/4 "2025-01-15T17:51:52Z")

</div>

In that case, I don’t have any idea. I’m only using static inventories.

But maybe someone else comes up with a solution.

---

<div class="post-metadata">

**Author:** ![jrglynn2](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@jrglynn2](https://forum.ansible.com/u/jrglynn2)\
**Post date:** [January 15, 2025, 6:28pm UTC](https://forum.ansible.com/t/different-variable-values-based-on-hostname-3-hosts/39629/5 "2025-01-15T18:28:36Z")

</div>

> [@dulhaver](#):
>
> 3 x `set_fact` with a `when` condtion for each host comes to mind, but I have a feeling having heard people mentionen `set_fact` not being really _ansible-ish_.

I have never heard this. I use `set_fact` all the time, including for this type of if statements.

The other option is to use `vars:`, either at the playbook level or the task level

---

<div class="post-metadata">

**Author:** ![markuman](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@markuman](https://forum.ansible.com/u/markuman)\
**Post date:** [January 17, 2025, 3:25pm UTC](https://forum.ansible.com/t/different-variable-values-based-on-hostname-3-hosts/39629/6 "2025-01-17T15:25:07Z")

</div>

I use tags on my instances and map than in different group\_vars via dynamic inventories.  
Maybe this helps you.

On my proxmox dynamic inventory

```yml
want_facts: true
keyed_groups:
  - key: proxmox_tags_parsed
    separator: ""

```

So tag `ubuntu` on instances load all `group_vars/ubuntu.yml`

And on aws ec2 inventory

```yml
filters:
  instance-state-name:
    - running
  tag:ansible_group:
    - ubuntu
    - oraclelinux
    - opnsense
keyed_groups:
  - key: tags['ansible_group']
    separator: ''
  - key: tags['infra_system']
    separator: ''
  - key: placement.availability_zone
    separator: ''

```

the tag `infra_system: dns` results in loading `group_vars/dns.yml`

maybe this helps…
