10
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ansibleで対象Host全てを含む/etc/hostsを自動生成する

Last updated at Posted at 2017-03-30

https://gist.github.com/rothgar/8793800
ここで議論されてますが、この方法がシンプルで、融通も効きそう。
テンプレートになっているので、他の記述も追加できるし。

- name: Generate /etc/hosts file
  template:
    src=files/hosts.j2
    dest=/etc/hosts
files/hosts.j2
# {{ ansible_managed }}
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

{% for item in play_hosts %}
{% set short_name = item.split('.') %}
{{ hostvars[item]['ansible_default_ipv4']['address'] }}  {{ item }} {{ short_name[0] }}
{% endfor %}
10
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?