Ansible:从列表中获取输入以外的其他数字

Ansible: Get other number than the input out of a list

我有一组三个数字:19、20 和 21,除了输入中的一个,我需要得到其中一个。我的方法是使用 jinja-difference 过滤器,它工作正常,但我不知道如何用差异中的变量(在我的示例中为“hostnumber”)替换硬编码数字(在我的示例中为“19”) -filer.

示例:

ansible-playbook get_other_host.yml -e "hostname=abcd0015"

---
- hosts: all
  gather_facts: false
  tasks:

    - name: Extract the hostnumber
      set_fact:
        hostnumber: "{{ (hostname[6:] | int) + 4 }}"

# Output is: 19
    - name: The hostnumber is
      debug:
        msg: "{{ hostnumber }}"

# Output is: 20 and 21
    - name: The other hosts are
      debug:
        msg: "{{ [19,20,21] | difference([19]) }}"

# Output is: 20 or 21
    - name: One other host is
      debug:
        msg: "{{ [19,20,21] | difference([19]) | random }}"

Jinja2 可以访问所有变量,包括之前设置的 hostnumber;它可能已经是 int,但是在已经是 int 的变量上使用 | int 是无害的

- debug:
    msg: "{{ [19,20,21] | difference([hostnumber | int]) }}"