Bharat Gupta
Bharat Gupta

Reputation: 127

how do I remove the blank line without any data?

How to remove empty lines in the output list for below playbook? then i need to store only mac address & port information in variable, can i remove it in ansible it self or do i need to use any other method?

Playbook,

- name: Print Converted Mac
  set_fact:
    mac_array: "{{ sw_mac.results | json_query('[*].stdout') }}"

- name: Query the mac on the switch
  ios_command:
    commands: show mac address-table | in {{ item }}
  with_items: "{{ mac_array }}"
  ignore_errors: yes
  register: mac_array_result

- debug:
    msg: "{{ item.stdout }}"
  with_items: "{{ mac_array_result.results }}"
  when: item.stdout | length > 1
  register: comout

- set_fact:
    mac_port: "{{ comout | json_query('results[*].item.stdout') | list }}"

Output: list of the mac_port

ok: [10.10.10.177] => {
    "ansible_facts": {
        "mac_port": [
            [
                "201    44a8.4227.c7ba    DYNAMIC     Gi1/0/13"
            ],
            [
                ""
            ],

            [
                ""
            ]
        ]
    },
    "changed": false
}

as requested Based on the ask here is the code which is the output of the comout.results variable for single item which has data in line, and others are without the lines. Now how can we only print item which is has data in line.

{
  "ansible_loop_var": "item",
  "changed": false,
  "item": {
    "ansible_facts": {
      "discovered_interpreter_python": "/usr/bin/python"
    },
    "ansible_loop_var": "item",
    "changed": false,
    "failed": false,
    "invocation": {
      "module_args": {
        "auth_pass": null,
        "authorize": null,
        "commands": [
          "show mac address-table | in 44a8.4227.c7ba"
        ],
        "host": null,
        "interval": 1,
        "match": "all",
        "password": null,
        "port": null,
        "provider": null,
        "retries": 10,
        "ssh_keyfile": null,
        "timeout": null,
        "username": null,
        "wait_for": null
      }
    },
    "item": "44a8.4227.c7ba",
    "stdout": [
      "201    44a8.4227.c7ba    DYNAMIC     Gi1/0/13"
    ],
    "stdout_lines": [
      [
        "201    44a8.4227.c7ba    DYNAMIC     Gi1/0/13"
      ]
    ]
  },
  "skip_reason": "Conditional result was False",
  "skipped": true
}

Upvotes: 2

Views: 2273

Answers (2)

Bharat Gupta
Bharat Gupta

Reputation: 127

Here is the code which works !!

- set_fact:
        mac_port: "{{ comout | json_query('results[*].item.stdout') | list }}"

    - set_fact:
        new_mac_port: |
          {% set new_mac_port = [] %}
          {% for p in mac_port %}
          {% if p[0] != "" %}
          {% set _ = new_mac_port.append(p[0]) %}
          {% endif %}
          {% endfor %}
          {{ new_mac_port }}
      run_once: yes

Upvotes: 2

hmharsh3
hmharsh3

Reputation: 355

As far as I understand, you want to remove empty lines, then you can use something line this,

---
  - hosts: localhost
    vars:
      mac_port:
        - "201    44a8.4227.c7ba    DYNAMIC     Gi1/0/13"
        - ""
        - ""
        - ""
        - ""
    tasks:
      - name: old data with empty new lines
        debug:
          var: mac_port

      - set_fact:
          new_mac_port: |
            {% set new_mac_port = [] %}
            {% for p in mac_port %}
            {% if p != "" %}
            {% set _ = new_mac_port.append(p) %}
            {% endif %}
            {% endfor %}
            {{ new_mac_port }}
        run_once: yes

      - name: filtered data without empty lines
        debug:
          var: new_mac_port

Now the value of run_once: yes will be

TASK [filtered data without empty lines] ***************************************************************************************************
ok: [localhost] => {
    "new_mac_port": [
        "201    44a8.4227.c7ba    DYNAMIC     Gi1/0/13"
    ]
}

Upvotes: 0

Related Questions