库存优先级

huangapple go评论63阅读模式
英文:

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

在当前情况下,解决问题的一种方法如下:

  1. 决定为所有服务器设置默认端口。在您的all文件中修改为default_port: 9001
  2. 决定port仅出现在特定组级别(因此不要将它放回all文件中),并且仅为覆盖默认端口的组定义。
  3. 修改您的任务如下:
    - 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'] }}"
    

请注意:ddefault的别名

英文:

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:

  1. decide there will be a default port for all servers. Modify your all file with default_port: 9001
  2. decide port will only be present at specific group level (so never put it back inside your all file) and only defined for groups overriding the default port.
  3. 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

huangapple
  • 本文由 发表于 2023年3月3日 22:41:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75628469.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定