ansible Синтаксическая ошибка подсчета хостов в инвентарных группах - PullRequest
1 голос
/ 01 ноября 2019

Team,

Я не могу обойти эту проблему синтаксиса даже после поиска в Интернете. Любой намек?

мне нужно посчитать количество хостов во всех группах на данный момент два в файле инвентаризации.

задача

#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"

ИЛИ

*Вывод 1011 *

одинаков для всех опций, указанных выше.

#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 голосов
/ 01 ноября 2019

я успешно проверил его в моей среде 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
0 голосов
/ 01 ноября 2019

Единственный способ, которым я избавился от синтаксической ошибки, - переключиться на нижеприведенное. Но я хочу понять причину здесь и не слепо соглашаться. но даже с этим у меня есть ошибка в выводе, что 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"}

0 голосов
/ 01 ноября 2019

Убедитесь, что вы соблюдаете синтаксис YAML , особенно в отношении отступа.

Например: - nam e должно начинаться с начала строки, без места перед ним.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...