库存组中的主机计数错误语法

时间:2019-11-01 07:17:20

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

团队

即使在线搜索后,我也无法解决此语法问题。有什么提示吗?

我需要在清单文件中现在计算所有组中的主机数。

任务

#Validate cluster nodes to inventory specification
      - assert:
          that:
            - "groups['k8s_gpu_nodes'] | length >= 1"
            - "groups['k8s_cpu_nodes'] | length >= 1"
          msg: "Assure k8s_nodes are not empty"

#Count the hosts in groups in inventory
      - name: Inventory validation
        hosts: localhost
        gather_facts: false
        vars:
          GPU_COUNT: "{{ groups['k8s_gpu_nodes'] | length }}"
          CPU_COUNT: "{{ groups['k8s_cpu_nodes'] | length }}"
        tasks:
          - assert:
              that:
                - "GPU_COUNT | int <= 1"
                - "CPU_COUNT | int <= 1"

OR

      - name: Inventory validation
        vars:
          GPU_COUNT: "{{ groups['k8s_gpu_nodes'] | length }}"
          CPU_COUNT: "{{ groups['k8s_cpu_nodes'] | length }}"

对于上面尝试的任何选项,输出相同。

#Count the hosts in groups in inventory
      - name: Inventory validation
        ^ here

没有名字的任务:

#Count the hosts in groups in inventory
      - hosts: localhost
        gather_facts: false
        vars:
          GPU_COUNT: "{{ groups['k8s_gpu_nodes'] | length }}"
          CPU_COUNT: "{{ groups['k8s_cpu_nodes'] | length }}"
        tasks:
          - assert:
              that:
                - "GPU_COUNT | int <= 1"
                - "CPU_COUNT | int <= 1"

ERROR! unexpected parameter type in action: <type 'bool'>

The error appears to be in '/home/du/ansible/roles/3_validations_on_ssh/tasks/main.yaml': line 45, column 9, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

#Count the hosts in groups in inventory
      - hosts: localhost
        ^ here

库存

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

3 个答案:

答案 0 :(得分:0)

确保您尊重 YAML syntax ,尤其是缩进方面。

例如:SELECT *,COUNT(Time) ,CASE WHEN COUNT(Time) = 1 THEN CASE WHEN Time > '00:00:00' && Time <= '12:15:00' THEN Time END ELSE MIN(Time) END as time_in, CASE WHEN COUNT(Time) = 1 THEN CASE WHEN Time > '12:15:00' && Time <= '23:59:59' THEN Time END ELSE NULLIF(MAX(Time), MIN(Time)) END as time_out FROM (SELECT * FROM attendancedata INNER JOIN nonacadamic ON attendancedata.EnrolledID = nonacadamic.emp_id WHERE nonacadamic.emp_id = '".$_POST["emp_id"]."' AND Date LIKE '$currentMonth%' UNION ALL SELECT * FROM tempattendancedata INNER JOIN nonacadamic ON attendancedata.EnrolledID = nonacadamic.emp_id WHERE nonacadamic.emp_id = '".$_POST["emp_id"]."' AND Date LIKE '$currentMonth%') t1 GROUP BY t1.EnrolledID, t1.Date e应该从行首开始,行前没有空格。

答案 1 :(得分:0)

我在ansible2.8环境中成功对其进行了测试..检查是否在主机,变量,任务,断言等处缺少任何缩进,等等。将其粘贴到我已经测试过的缩进中。.

- hosts: localhost
  gather_facts: false
  vars:
      GPU_COUNT: "{{ groups['k8s_gpu_nodes'] | length }}"
      CPU_COUNT: "{{ groups['k8s_cpu_nodes'] | length }}"
  tasks:
     - assert:
         that:
            - "GPU_COUNT | int >= 1"
            - "CPU_COUNT | int >= 1"
         msg: "Assure k8s_nodes are not empty"

输出:

TASK [assert] ***************************************************************************************************************************
fatal: [localhost]: FAILED! => changed=false
  assertion: GPU_COUNT | int >= 1
  evaluated_to: false
  msg: Assure k8s_nodes are not empty

输出:当我将两者都更改为大于或等于

TASK [assert] ******************
ok: [localhost] => changed=false
  msg: All assertions passed

答案 2 :(得分:0)

摆脱语法错误的唯一方法是切换到下面。但是我想了解这里的原因,而不是盲目同意。 但是即使这样,我在输出中仍存在错误,即GPU_COUNT超出范围。但是,至少我解决了我原来的语法问题。

      - 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 : 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"}