【问题标题】:Ansible Five random hosts from /etc/ansible/hostsAnsible 来自 /etc/ansible/hosts 的五个随机主机
【发布时间】:2018-07-13 22:29:30
【问题描述】:

我有这本剧本,它使用 max_index 工作,但总是从 /etc/ansible/hosts 中获取前 3 个主机,我需要从该文件中获取 3 个随机(而不是重复)主机。

playbook.yml

---
- hosts: ciscos
  connection: local
  gather_facts: false
  tasks:
    - group_by: key=limited_selection
      when: play_hosts.index(inventory_hostname) < max_index | int

- hosts: limited_selection
  gather_facts: no

/etc/ansible/hosts

[ciscos]
stagin ansible_host=10.xx.xx.1
stagin2 ansible_host=10.xx.xx.1
stagin3 ansible_host=10.xx.xx.1
stagin4 ansible_host=10.xx.xx.1
stagin5 ansible_host=10.xx.xx.1

【问题讨论】:

    标签: ansible ansible-2.x ansible-inventory


    【解决方案1】:

    解决方案

    您需要对组的元素进行洗牌,并首先选择三个。 Jinja2 的表达式是:

    (groups['ciscos'] | shuffle)[0:3]
    

    应该可以工作但有问题的实现

    您应该能够简单地过滤 hosts 声明中的组:

    - hosts: "{{ (groups['ciscos'] | shuffle)[0:3] }}"
      gather_facts: no
      tasks:
        - debug:
    

    但是结果是不确定的 - 尽管该剧显示为针对三个随机选择的主机运行,但任务有时会在 1、2、3 或 0 上执行:

    PLAY [[u'stagin2', u'stagin4', u'stagin5']] *******************************************************************************
    
    TASK [debug] **************************************************************************************************************
    ok: [stagin2] => {
        "msg": "Hello world!"
    }
    ok: [stagin5] => {
        "msg": "Hello world!"
    }
    

    解决方法(可行的实施)

    使用add_host 模块创建过滤组:

    - hosts: localhost
      connection: local
      gather_facts: no
      tasks:
        - add_host:
            name: "{{ item }}"
            groups: limited_selection
          loop:  "{{ (groups['ciscos'] | shuffle)[0:3] }}"
    
    - hosts: limited_selection
      gather_facts: no
      tasks:
        - debug:
    

    【讨论】:

    • 它不起作用致命:[stagin]:失败! => {"failed": true, "msg": "在可用的查找插件中查找名为 '{{ (groups['ciscos'] | shuffle)[0:3] }}' 的查找意外失败"}
    • 你运行的是哪个 Ansible 版本?
    • ansible --version ansible 2.2.1.0 配置文件 = /etc/ansible/ansible.cfg 配置的模块搜索路径 = 默认无覆盖
    • 使用当前版本怎么样?
    【解决方案2】:

    这样的事情呢?

    ---
    - hosts: ciscos
      gather_facts: False
      connection: local
    
      tasks:
    
        - name: Fact My Inventory
          set_fact:
            myinventory: "{{ ansible_play_batch | shuffle }}"
          run_once: True
          delegate_to: localhost
    
        - name: Fact limited_selection
          set_fact:
            limited_selection: "{{ myinventory[0:max_index|int] }}"
          run_once: True
          delegate_to: localhost
    
        - name: Create Inventory
          add_host:
            name: '{{ item }}'
            groups: limited_selection
          with_items: "{{ limited_selection }}"
          delegate_to: localhost
    
    - hosts: limited_selection
      gather_facts: no
    
      tasks:
        - name: Debug
          debug:
            msg: "I'm in the limited selection group!"
    

    注意play_hosts,因为它已被弃用。

    注意:出于学习目的并显示ansible_play_batchmax_index 变量,我将剧本保留为connection:ciscos 而不是localhost。最好使用 groups 而不是 delegate_to:localhost 进行 localhost 游戏

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多