On Wed, 25 Nov 2020 06:32:16 -0800 (PST)
Hiero-nymo <[email protected]> wrote:

> - name: check which interfaces exist
>   shell: find /sys/class/net -type l -not -lname '*virtual*' -printf '%f\n'
>   register: ifconfig_result
> 
> - name: check which interface is active
>   shell: ifconfig -a "{{ item }}"|head -1|grep -v UP || /bin/true
>   loop: "{{ ifconfig_result.stdout_lines }}"
>   register: active
> 
> - name: debug
>   debug:
>     msg: "{{ item }}"
>   loop: "{{ active.results }}"
> ...
> Perhaps, there's another easiest solution??????

Ansible module "setup" collects this kind of info. See
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/setup_module.html

For example, the playbook below

  - hosts: localhost
    tasks:
      - debug:
          var: ansible_interfaces
      - debug:
          var: ansible_facts.eth0
      - debug:
          var: ansible_facts.wlan0
      - debug:
          msg: "{{ ansible_facts|dict2items|
                   json_query('[?value.active].key') }}"

gives

  ok: [localhost] => {
      "ansible_interfaces": [
          "lo",
          "eth0",
          "wlan0"
      ]
  }

  ok: [localhost] => { "ansible_facts.eth0": {
          "active": true,
          "device": "eth0",
          "features": {
              "esp_hw_offload": "off [fixed]",
          ...

  ok: [localhost] => {
      "ansible_facts.wlan0": {
          "active": false,
          "device": "wlan0",
          "features": {
              "esp_hw_offload": "off [fixed]",
          ...

  ok: [localhost] => {
      "msg": [
          "lo",
          "eth0"
      ]
  }

-- 
Vladimir Botka

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/20201125163231.30f41960%40gmail.com.

Attachment: pgpHDiCdKJ5Ch.pgp
Description: OpenPGP digital signature

Reply via email to