大网数据广告活动
服务器运维问答

What's the correct way to reference hosts as variables in Ansible?

· 52CE 我爱测

I'm fairly new to Ansible, and am in a bit of a dilemma with our flow for this particular script. We have a list of certs that we want to deploy to each host; these are unique to each host, so 1:1 transfer. # hostfile [prod] host…

问题原文

原帖链接 · MrDuk · CC BY-SA 3.0

I'm fairly new to Ansible, and am in a bit of a dilemma with our flow for this particular script. We have a list of certs that we want to deploy to each host; these are unique to each host, so 1:1 transfer.

# hostfile
[prod]
host_a
host_b
host_c

Currently, I'm doing this as part of the group_vars for my role:

haproxy:
  prod:
    certs:
      host_a:  a.my.endpoint.com.pem
      host_b:  b.my.endpoint.com.pem
      host_c:  c.my.endpoint.com.pem

And then referencing this in a task:

- name: upload haproxy server certificates
  copy:
    src: "{{haproxy[env].certs[inventory_hostname]}}"
    dest: "/etc/haproxy/ssl/{{haproxy[env].certs[inventory_hostname]}}"
    backup: yes
  notify:
    - restart haproxy
  tags:
    - haproxy

This works fine, but I don't like it. I primarily don't like it because it forces you to remember to update hosts in two places (the hostfile and the vars file). What I'd considered was to define the hosts as a var in group_vars, but I'm not entirely sure that I can reference vars within the file? So something like:

hosts:
  host_a
  host_b
  host_c

haproxy:
  prod:
    certs:
      {{ hosts.host_a }}:  a.my.endpoint.com.pem
      {{ hosts.host_b }}:  b.my.endpoint.com.pem
      {{ hosts.host_c }}:  c.my.endpoint.com.pem

And then this further complicates things a bit when I get into the task. I assume I could change my task to have a separate copy for each host, like with a when: {{ inventory_host }} == {{ hosts.host_a}} and just copy each file in it's own section. This seems equally as ugly to me though.

Is there a better, more intuitive way I could go about this?

社区高分解答

原帖链接 · udondan · CC BY-SA 3.0

Best practice would be to use host-vars. host-vars work exactly the same as group-vars but define variables per host.

So you could have a file host_vars/host_a with the content

cert: a.my.endpoint.com.pem

...and use cert as the variable in your task.

But your problem stays the same:

I primarily don't like it because it forces you to remember to update hosts in two places (the hostfile and the vars file).

But you as well can define host vars in your inventory. This gets ugly if you have more variables to define but if that's the only one and your goal is to manage all defintions in one place, you can do it like this:

# hostfile
[prod]
host_a   cert=a.my.endpoint.com.pem
host_b   cert=b.my.endpoint.com.pem
host_c   cert=c.my.endpoint.com.pem

In both cases your task would reduce to this:

- name: upload haproxy server certificates
  copy:
    src: "{{ cert }}"
    dest: "/etc/haproxy/ssl/{{ cert }}"
    backup: yes
  notify:
    - restart haproxy
  tags:
    - haproxy

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-03-09)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。