Как напечатать конкретный c контент поверх al oop в Ansible - PullRequest
1 голос
/ 03 августа 2020

Я немного застрял с Ansible, пытаясь напечатать некоторые переменные из возвращаемого F5.

Я пытаюсь распечатать, какие узлы настроены в определенном пуле. Я могу получить весь результат:

TASK [f5_getpoolmembers : Ver IPs de un pool] ***********************************************************************************
ok: [localhost] => (item=[u'pool_createdby_ansible_f5']) => 
  msg:
  - pool_createdby_ansible_f5
  - - address: 10.10.10.34
      connection_limit: 0
      description: prova_ansible_pool
      dynamic_ratio: 1
      ephemeral: 'no'
      fqdn_autopopulate: 'yes'
      full_path: /Common/10.10.10.34:80
      inherit_profile: 'yes'
      logging: 'no'
      monitors: []
      name: 10.10.10.34:80
      partition: Common
      priority_group: 0
      rate_limit: 'no'
      ratio: 1
      real_session: user-enabled
      real_state: unchecked
      state: present
    - address: 10.100.250.5
      connection_limit: 0
      dynamic_ratio: 1
      ephemeral: 'no'
      fqdn_autopopulate: 'no'
      full_path: /Common/10.100.250.5:80
      inherit_profile: 'yes'
      logging: 'no'
      monitors: []
      name: 10.100.250.5:80
      partition: Common
      priority_group: 0
      rate_limit: 'no'
      ratio: 1
      real_session: user-enabled
      real_state: unchecked
      state: present

с этим кодом:

- name: "Ver IPs de un pool"
  debug:
    msg:
      - "{{item.name}}"
      - "{{item.members}}"
  loop: "{{pool_facts.ltm_pools}}"
  loop_control:
    label:
      - "{{item.name}}"
  when: item.name is search "ansible"

Проблема в том, что я пытаюсь распечатать только name для каждого узла в пуле. Я не могу найти способ сделать это. Пробовал много вариантов, таких как вложенные или подэлементы, но не сработало. Если я попытаюсь напечатать только name, я просто получу первый узел:

TASK [f5_getpoolmembers : Ver IPs de un pool] ***********************************************************************************
ok: [localhost] => (item=[u'pool_createdby_ansible_f5']) => 
  msg:
  - pool_createdby_ansible_f5
  - 10.10.10.34:80

Делаем это:

- name: "Ver IPs de un pool"
  debug:
    msg:
      - "{{item.name}}"
      - "{{item.members[0].name}}"

Я знаю, что это неправильно, поэтому я ' м здесь, чтобы получить несколько советов.

Редактировать: Добавлена ​​полная структура бассейна:

    ltm_pools:
    - active_member_count: 0
      all_avg_queue_entry_age: 0
      all_max_queue_entry_age_ever: 0
      all_max_queue_entry_age_recently: 0
      all_num_connections_queued_now: 0
      all_num_connections_serviced: 0
      all_queue_head_entry_age: 0
      allow_nat: 'yes'
      allow_snat: 'yes'
      availability_status: unknown
      available_member_count: 3
      client_ip_tos: pass-through
      client_link_qos: pass-through
      current_sessions: 0
      enabled_status: enabled
      full_path: /Common/pool_createdby_ansible_f5
      ignore_persisted_weight: 'no'
      lb_method: ratio-member
      member_count: 3
      members:
      - address: 10.10.10.34
        connection_limit: 0
        description: prova_ansible_pool
        dynamic_ratio: 1
        ephemeral: 'no'
        fqdn_autopopulate: 'yes'
        full_path: /Common/10.10.10.34:80
        inherit_profile: 'yes'
        logging: 'no'
        monitors: []
        name: 10.10.10.34:80
        partition: Common
        priority_group: 0
        rate_limit: 'no'
        ratio: 1
        real_session: user-enabled
        real_state: unchecked
        state: present
      - address: 10.100.250.5
        connection_limit: 0
        dynamic_ratio: 1
        ephemeral: 'no'
        fqdn_autopopulate: 'no'
        full_path: /Common/10.100.250.5:80
        inherit_profile: 'yes'
        logging: 'no'
        monitors: []
        name: 10.100.250.5:80
        partition: Common
        priority_group: 0
        rate_limit: 'no'
        ratio: 1
        real_session: user-enabled
        real_state: unchecked
        state: present
      - address: 10.55.55.4
        connection_limit: 0
        dynamic_ratio: 1
        ephemeral: 'no'
        fqdn_autopopulate: 'no'
        full_path: /Common/10.55.55.4:80
        inherit_profile: 'yes'
        logging: 'no'
        monitors: []
        name: 10.55.55.4:80
        partition: Common
        priority_group: 0
        rate_limit: 'no'
        ratio: 1
        real_session: user-enabled
        real_state: unchecked
        state: present
      minimum_active_members: 0
      minimum_up_members: 0
      minimum_up_members_action: failover
      minimum_up_members_checking: 'no'
      name: pool_createdby_ansible_f5
      pool_avg_queue_entry_age: 0
      pool_max_queue_entry_age_ever: 0
      pool_max_queue_entry_age_recently: 0
      pool_num_connections_queued_now: 0
      pool_num_connections_serviced: 0
      pool_queue_head_entry_age: 0
      priority_group_activation: 0
      queue_depth_limit: 0
      queue_on_connection_limit: 'no'
      queue_time_limit: 0
      reselect_tries: 0
      server_ip_tos: pass-through
      server_link_qos: pass-through
      server_side_bits_in: 0
      server_side_bits_out: 0
      server_side_current_connections: 0
      server_side_max_connections: 0
      server_side_pkts_in: 0
      server_side_pkts_out: 0
      server_side_total_connections: 0
      service_down_action: none
      slow_ramp_time: 10
      status_reason: The children pool member(s) either don't have service checking enabled, or service check results are not available yet
      total_requests: 0

1 Ответ

0 голосов
/ 03 августа 2020

Вам действительно нужно будет использовать subelements. Когда вы используете фильтр subelements('dictionary_key'), вы получаете:

  • item.0: элемент из списка вашего l oop
  • item.1: элемент из вложенного списка внутри dictionary_key каждого item.0 элемента

Учитывая playbook:

- hosts: all
  gather_facts: no  
        
  tasks:
    - debug: 
        msg: 
          - "{{ item.0.name }}"
          - "{{ item.1.name }}"
      loop_control:
        label:
          - "{{ item.0.name }}"
      loop: "{{ pools_facts.ltm_pools | subelements('members') }}"
      when: "'ansible' in item.0.name"
      vars:
        pools_facts: 
          ltm_pools:
            - active_member_count: 0
              all_avg_queue_entry_age: 0
              all_max_queue_entry_age_ever: 0
              all_max_queue_entry_age_recently: 0
              all_num_connections_queued_now: 0
              all_num_connections_serviced: 0
              all_queue_head_entry_age: 0
              allow_nat: 'yes'
              allow_snat: 'yes'
              availability_status: unknown
              available_member_count: 3
              client_ip_tos: pass-through
              client_link_qos: pass-through
              current_sessions: 0
              enabled_status: enabled
              full_path: /Common/pool_createdby_ansible_f5
              ignore_persisted_weight: 'no'
              lb_method: ratio-member
              member_count: 3
              members:
                - address: 10.10.10.34
                  connection_limit: 0
                  description: prova_ansible_pool
                  dynamic_ratio: 1
                  ephemeral: 'no'
                  fqdn_autopopulate: 'yes'
                  full_path: /Common/10.10.10.34:80
                  inherit_profile: 'yes'
                  logging: 'no'
                  monitors: []
                  name: 10.10.10.34:80
                  partition: Common
                  priority_group: 0
                  rate_limit: 'no'
                  ratio: 1
                  real_session: user-enabled
                  real_state: unchecked
                  state: present
                - address: 10.100.250.5
                  connection_limit: 0
                  dynamic_ratio: 1
                  ephemeral: 'no'
                  fqdn_autopopulate: 'no'
                  full_path: /Common/10.100.250.5:80
                  inherit_profile: 'yes'
                  logging: 'no'
                  monitors: []
                  name: 10.100.250.5:80
                  partition: Common
                  priority_group: 0
                  rate_limit: 'no'
                  ratio: 1
                  real_session: user-enabled
                  real_state: unchecked
                  state: present
                - address: 10.55.55.4
                  connection_limit: 0
                  dynamic_ratio: 1
                  ephemeral: 'no'
                  fqdn_autopopulate: 'no'
                  full_path: /Common/10.55.55.4:80
                  inherit_profile: 'yes'
                  logging: 'no'
                  monitors: []
                  name: 10.55.55.4:80
                  partition: Common
                  priority_group: 0
                  rate_limit: 'no'
                  ratio: 1
                  real_session: user-enabled
                  real_state: unchecked
                  state: present
              minimum_active_members: 0
              minimum_up_members: 0
              minimum_up_members_action: failover
              minimum_up_members_checking: 'no'
              name: pool_createdby_ansible_f5
              pool_avg_queue_entry_age: 0
              pool_max_queue_entry_age_ever: 0
              pool_max_queue_entry_age_recently: 0
              pool_num_connections_queued_now: 0
              pool_num_connections_serviced: 0
              pool_queue_head_entry_age: 0
              priority_group_activation: 0
              queue_depth_limit: 0
              queue_on_connection_limit: 'no'
              queue_time_limit: 0
              reselect_tries: 0
              server_ip_tos: pass-through
              server_link_qos: pass-through
              server_side_bits_in: 0
              server_side_bits_out: 0
              server_side_current_connections: 0
              server_side_max_connections: 0
              server_side_pkts_in: 0
              server_side_pkts_out: 0
              server_side_total_connections: 0
              service_down_action: none
              slow_ramp_time: 10
              status_reason: The children pool member(s) either don't have service checking enabled, or service check results are not available yet
              total_requests: 0

Это дает результат:

PLAY [all] *********************************************************************************************************

TASK [debug] *******************************************************************************************************
ok: [localhost] => (item=['pool_createdby_ansible_f5']) => {
    "msg": [
        "pool_createdby_ansible_f5",
        "10.10.10.34:80"
    ]
}
ok: [localhost] => (item=['pool_createdby_ansible_f5']) => {
    "msg": [
        "pool_createdby_ansible_f5",
        "10.100.250.5:80"
    ]
}
ok: [localhost] => (item=['pool_createdby_ansible_f5']) => {
    "msg": [
        "pool_createdby_ansible_f5",
        "10.55.55.4:80"
    ]
}

PLAY RECAP *********************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...