Ansible角色断言在基于清单组中主机数量的var上失败

时间:2019-11-01 17:28:44

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

团队, 使用角色不直接写剧本。 我正在尝试计算某些组下清单中的主机数,然后声明该值。但是在输出中得到未定义的值响应。另外,还有更好的方法吗?

      - name: "Ensure KUBECONFIG"
        shell: echo $KUBECONFIG
        register: kubeconfig_exists
        failed_when: kubeconfig_exists.rc != 0
      - debug:
          var: kubeconfig_exists.stdout_lines

      - name: "Find $PATH where Ansible looks for binaries on target system"
        register: echo_path
        shell: echo $PATH
      - debug:
          var: echo_path.stdout_lines

      - debug:
        vars:
            gpu_count: "{{ groups['k8s_gpu_nodes'] | length }}"
            cpu_count: "{{ groups['k8s_cpu_nodes'] | length }}"
      - assert:
               that:
                  - "gpu_count | int <= 1"
                  - "cpu_count | int >= 1"
               msg: "Assure k8s_nodes are not empty"

输出:

TASK [3_validations_on_ssh : Ensure KUBECONFIG] ************
changed: [localhost]

TASK [3_validations_on_ssh : debug] ******************
ok: [localhost] => {
    "msg": "Hello world!"
}

TASK [3_validations_on_ssh : assert] ****************
fatal: [localhost]: FAILED! => {"msg": "The conditional check 'gpu_count | int <= 1' failed. The error was: error while evaluating conditional (gpu_count | int <= 1): 'gpu_count' is undefined"}

inventory.txt

target1 ansible_host='{{ target1_hostip }}' ansible_ssh_pass='{{ target1_pass }}'
[k8s_gpu_nodes]
host1
host2
[k8s_cpu_nodes]
host3
host4

2 个答案:

答案 0 :(得分:0)

这是一本纠正了单个任务的剧本:

---
- name: Check nodes in groups
  hosts: localhost
  gather_facts: false

  tasks:
    - name: Check everything is ok
      vars:
        gpu_count: "{{ groups['k8s_gpu_nodes'] | length }}"
        cpu_count: "{{ groups['k8s_cpu_nodes'] | length }}"
      assert:
        that:
          - gpu_count | int > 0
          - cpu_count | int > 0
        msg: "Assure k8s_nodes are not empty"

当我使用您上面的清单来播放此剧本时,结果如下:

$ ansible-playbook -i inventories/hosts.ini playbook.yml

PLAY [Check nodes in groups] ****************************************************************************************************************************************************************************

TASK [Check everything is ok] ***************************************************************************************************************************************************************************
ok: [localhost] => {
    "changed": false,
    "msg": "All assertions passed"
}

PLAY RECAP **********************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

答案 1 :(得分:0)

我想出了如何通过声明vars并计算其长度来从角色本身内部实现这一目标

  - name: Assert local inventory if node groups are empty Master, etcd, cpu, apilb, mon, gpu
    assert:
      that:
        - "master_count | int > 0"
        - "gpu_count | int > 0"

      msg: "Assure k8s-nodes groups are not empty"
    vars:
      master_count: "{{ groups['kube-master'] | length }}"
      gpu_count: "{{ groups['kube-gpu-node'] | length }}"
    ignore_errors: no