Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用ansible编排配置/etc/hosts_Ansible_Ansible Playbook - Fatal编程技术网

如何使用ansible编排配置/etc/hosts

如何使用ansible编排配置/etc/hosts,ansible,ansible-playbook,Ansible,Ansible Playbook,应用程序需要以下主机: [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-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服务器

我的问题是:

当我只对其中一个主机运行playbook时,如何获取资源清册中的所有主机

或者你能建议一些替代方案吗?因为我一开始的想法是错误的

我想到的备选方案是:

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

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

  roles:
    - hosts

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

事实上,这是非常有趣的,因为即使是文档也会说:

如果,在这一点上,你还没有在任何游戏中与该主持人交谈 剧本或剧本集,你可以得到变量,但是 你将看不到事实

我从中了解到,若主机还并没有被“查询”,那个么您可能无法获得收集到的事实,但您仍然应该看到清单(和组/主机变量)中定义的变量。 也许你应该把它推到邮件列表上

同时,您可以使用
组['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 %}

我也有同样的问题,这里是我的解决方案,供感兴趣的人参考

hosts/dev.ini tasks/main.yml 注:

  • python 3.7.5 ansible 2.9.0
  • 我决定使用blockinfle而不是使用模板,因为模板中没有更新hostvars上下文。加上我当时很匆忙:-)
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  %}
[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

---
- 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 %}