【问题标题】:How can I configure /etc/hosts with ansible orchestration如何使用 ansible 编排配置 /etc/hosts
【发布时间】:2014-11-14 03:02:14
【问题描述】:

应用程序需要以下主机:

[foo-servers]
foo-server ansible_ssh_host=192.168.50.2 

[bar-servers]
bar-server ansible_ssh_host=192.168.50.3 

[mysql-servers]
mysql-server ansible_ssh_host=192.168.50.4 

[mongodb-servers]
mongodb-server ansible_ssh_host=192.168.50.5 

我需要在 foo 服务器和 bar 服务器上配置主机,因为它们需要访问 mysql 和 mongodb。为此,我引入了一个名为 hosts 的角色:

# roles/hosts/tasks/main.yml
---
- name: change hosts
template: src=hosts.j2 dest=/etc/hosts

# roles/hosts/templates/hosts.j2
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

{% for item in hostvars %}
{{ hostvars[item]['ansible_ssh_host'] }} {{ item }}
{% endfor %}

问题是当我运行时

ansible-playbook foo.yml 

目标上的 /etc/hosts 只包含当前主机的 ip 和主机名,在本例中为 foo-server。

我的问题是:

当我只针对其中一个运行 playbook 时,如何获取清单中的所有主机?

或者你能建议一些替代方案吗,因为我一开始就有错误的想法。

我想到的替代方案是:

使主机配置成为针对所有主机的单独剧本,例如

---
- name: Configuring hosts
  hosts: all
  user: root

  roles:
    - hosts

缺点是我需要在其他人之前运行这个剧本,这似乎不是使用角色的正确方法。

【问题讨论】:

    标签: ansible ansible-playbook


    【解决方案1】:

    确实这很奇怪,因为即使是文档也说:

    如果此时您还没有在任何游戏中与该主持人交谈过 剧本或剧本集,你可以得到变量,但是 你将无法看到事实。

    据我了解,如果尚未“查询”主机,您可能无法收集到事实,但您仍然应该看到清单中定义的变量(以及组/主机变量)。 也许你应该把它推到邮件列表中。

    与此同时,您可以使用groups['all'] 来代替您的主机来解决您的问题:

    127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
    
    {% for item in groups['all'] %}
    {{ hostvars[item]['ansible_ssh_host'] }} {{ item }}
    {% endfor %}
    

    【讨论】:

      【解决方案2】:
      127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
      ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
      
      {% for host in groups['all'] %}
      
      {{ hostvars[host].ansible_default_ipv4.address }} {{ host  }}   {{ hostvars[host].ansible_fqdn }}
      
      {% endfor  %}
      

      【讨论】:

        【解决方案3】:

        我遇到了同样的问题,这里是我为感兴趣的人提供的解决方案。

        hosts/dev.ini

        [controller]
        controller1 ansible_ssh_host=10.11.11.10
        controller2 ansible_ssh_host=10.11.11.11
        controller3 ansible_ssh_host=10.11.11.12
        
        [compute]
        compute1 ansible_ssh_host=10.11.11.13
        compute2 ansible_ssh_host=10.11.11.14
        compute3 ansible_ssh_host=10.11.11.15
        
        [block]
        block1 ansible_ssh_host=10.11.11.16
        block2 ansible_ssh_host=10.11.11.17
        
        [haproxy]
        haproxy1 ansible_ssh_host=10.11.11.18
        
        [nginx]
        nginx1 ansible_ssh_host=10.11.11.19
        
        [deployment]
        deployment ansible_ssh_host=10.11.11.20
        
        [all:vars]
        ansible_python_interpreter=/usr/bin/python3
        
        

        tasks/main.yml

        ---
        - name: Update /etc/hosts
          become: true
          blockinfile:
              path: /etc/hosts
              create: yes
              block: |
                127.0.0.1 localhost
        
                # The following lines are desirable for IPv6 capable hosts
                ::1 ip6-localhost ip6-loopback
                fe00::0 ip6-localnet
                ff00::0 ip6-mcastprefix
                ff02::1 ip6-allnodes
                ff02::2 ip6-allrouters
                ff02::3 ip6-allhosts
        
                {% for item in ansible_play_batch %}
                {{ hostvars[item].ansible_ssh_host }}   {{ item }}    
                {% endfor %}
        

        注意事项:

        • python 3.7.5 ansible 2.9.0
        • 我决定使用 blockinfile 而不是使用模板,因为 hostvars 上下文没有在模板中更新。另外我很着急:-)。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-02-01
          • 2014-10-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多