Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

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 %}
masa-tu
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away