@MoscowStyle

Ansible grep json?

Что не так делаю?

отрабатывает модуль и получает требуемые результаты
- name: Get service facts
     ansible.builtin.setup:
       filter: 
         - 'processor'
         - 'memtotal_mb'
     register: devices


получаю небольшой вывод
{
    "memtotal_mb": 1548054,
    "processor": [
        "0",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "1",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "2",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "3",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "4",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "5",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "6",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "7",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "8",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "9",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "10",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "11",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "12",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "13",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "14",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "15",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "16",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "17",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "18",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "19",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "20",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "21",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "22",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "23",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "24",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "25",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "26",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "27",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "28",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "29",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "30",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "31",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "32",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "33",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "34",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "35",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "36",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "37",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "38",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "39",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "40",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "41",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "42",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "43",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "44",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "45",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "46",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "47",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "48",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "49",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "50",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "51",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "52",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "53",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "54",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "55",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "56",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "57",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "58",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "59",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "60",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "61",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "62",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz",
        "63",
        "GenuineIntel",
        "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz"
    ]
}


Из вывода мне нужно оставить только строки

"memtotal_mb": 1548054
"processor" "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz"

Делаю запрос {Ram:memtotal_mb, Cpu:processor[2]} и он прекрасно отрабатывает в том же https://play.jmespath.org/

{
  "RAM": 1548054,
  "CPU": "Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz"
}

Теперь пытаюсь это сделать в ансибле
-  name: Поиск по заданным данным
    set_fact:
         devicenames: "{{ devices | to_nice_json| json_query(jmesquery) }}"
     vars:
          jmesquery: '{Mem:memtotal_mb, Cpu:processor[2]}'

- name: Записываем результат в файл
    ansible.builtin.lineinfile:
        create: yes
        line: "{{ item | to_nice_json}}"
        path: /home/rootuser/ansible/!final_result_files/{{ inventory_hostname }}_hardware_info2_FINAL.json
     delegate_to: localhost
      with_items: "{{ devicenames }}"

На выходе имею пустые данные:

"Ram"
"Cpu"

Что делаю не так?
  • Вопрос задан
  • 119 просмотров
Решения вопроса 1
@mezhuev
Системный администратор
Нет смысла заранее формировать JSON, можно сразу сохранить из YAML:
- name: Get hardware facts
  ansible.builtin.setup:
    filter: 
      - processor
      - memtotal_mb
  register: devices

- name: Save hardware facts
  ansible.builtin.copy:
    content: '{{ devicenames | to_nice_json }}'
    dest: /home/rootuser/ansible/!final_result_files/{{ inventory_hostname }}_hardware_info2_FINAL.json
  delegate_to: localhost
  vars:
    devicenames:
      Cpu: '{{ devices.ansible_facts.ansible_processor[2] }}'
      Mem: '{{ devices.ansible_facts.ansible_memtotal_mb }}'

Но на самом деле эту операцию можно выполнить в один шаг:
- name: Save hardware facts
  ansible.builtin.copy:
    content: '{{ devicenames | to_nice_json }}'
    dest: /home/rootuser/ansible/!final_result_files/{{ inventory_hostname }}_hardware_info2_FINAL.json
  delegate_to: localhost
  vars:
    devicenames:
      Cpu: '{{ ansible_processor[2] }}'
      Mem: '{{ ansible_memtotal_mb }}'
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы