英文:
Inventory precedences
问题
我正在使用Ansible变量。我有这个配置:
all.yml
---
port: 9001
inventory.yml
---
group1:
  hosts:
    server1:
      ansible_host: www.server1.com
    server2:
      ansible_host: www.server2.com
  vars:
    port: 9002
   
group2:
  hosts:
    server5:
      ansible_host: www.server5.com
group3:
  hosts:
    server7:
      ansible_host: www.server7.com
我创建了一个Playbook,my-playbook.yml
- hosts: localhost
  gather_facts: no
  tasks:
    - name: Netcat server 5
      shell: nc -zv {{ hostvars[item].ansible_host }} {{ port }}
      with_items: "{{ groups['group2'] }}"
    - name: Netcat server 1 only
      shell: nc -zv {{ hostvars['server1']['ansible_host'] }} {{ hostvars[item].port }}
      with_items: "{{ groups['group1'] }}"
当我运行我的Ansible Playbook 时:
ansible-playbook -i inventory/inventory.yml playbook.yml
我期望对于 server5 使用端口 9001(位于 all.yml 中),对于 server1 使用端口 9002(位于Inventory中)。
然而,我看到它对于两者都使用了端口 9001。
我做错了什么?
英文:
I am working with Ansible variables. I have this
all.yml
---
port: 9001
inventory.yml
---
group1:
  hosts:
    server1:
      ansible_host: www.server1.com
    server2:
      ansible_host: www.server2.com
  vars:
    port: 9002
   
group2:
  hosts:
    server5:
      ansible_host: www.server5.com
group3:
  hosts:
    server7:
      ansible_host: www.server7.com
I built a playbook, my-playbook.yml
- hosts: localhost
  gather_facts: no
  tasks:
    - name: Netcat server 5
      shell: nc -zv {{ hostvars[item].ansible_host }} {{ port }}
      with_items: "{{ groups['group2'] }}"
    - name: Netcat server 1 only
      shell: nc -zv {{ hostvars['server1']['ansible_host'] }} {{ hostvars[item].port }}
      with_items: "{{ groups['group1'] }}"
When I run my ansible playbook:
ansible-playbook -i inventory/inventory.yml playbook.yml
I am expecting to do use port 9001 for server5 (which is in all.yml) and port 9002 for server1 (which is in the inventory).
However, I can see it's grabbing port 9001 for both.
What am I doing wrong?
答案1
得分: 3
你遇到了一个组优先问题。在全局范围内,Ansible中的优先级问题可能会变得非常棘手,特别是当您尝试在不同位置定义变量时。这正是文档所提到的原因:
我们建议您在一个地方定义每个变量:找出在哪里定义变量,并保持简单。有关示例,请参见Tips on where to set variables。
在当前情况下,解决问题的一种方法如下:
- 决定为所有服务器设置默认端口。在您的
all文件中修改为default_port: 9001。 - 决定
port仅出现在特定组级别(因此不要将它放回all文件中),并且仅为覆盖默认端口的组定义。 - 修改您的任务如下:
- name: Netcat server 5 shell: nc -zv {{ hostvars[item].ansible_host }} {{ port | d(default_port) }} with_items: "{{ groups['group2'] }}" - name: Netcat server 1 only shell: nc -zv {{ hostvars['server1']['ansible_host'] }} {{ hostvars[item].port | d(default_port) }} with_items: "{{ groups['group1'] }}" 
请注意:d是default的别名。
英文:
You are hitting a group precedence problem. Globally, precedence problems in Ansible can get really tricky especially when you are trying to define variables in different places. This is exactly why the documentation states:
>  We suggest that you define each variable in one place: figure out where to define a variable, and keep it simple. For examples, see Tips on where to set variables.
In the current case, this is one way to solve the problem:
- decide there will be a default port for all servers. Modify your 
allfile withdefault_port: 9001 - decide 
portwill only be present at specific group level (so never put it back inside yourallfile) and only defined for groups overriding the default port. - modify your task(s) as follows:
- name: Netcat server 5 shell: nc -zv {{ hostvars[item].ansible_host }} {{ port | d(default_port) }} with_items: "{{ groups['group2'] }}" - name: Netcat server 1 only shell: nc -zv {{ hostvars['server1']['ansible_host'] }} {{ hostvars[item].port | d(default_port) }} with_items: "{{ groups['group1'] }}" 
Note if necessary: d is an alias to default
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论