0
votes

I currently want to select specifically the Windows adapter name ONLY from the ansible facts. So my problem is that I cannot retrieve this value only.

Ansible 2.8.2_1 with Winrm & Kerberos Authentication are running on the server.

I've tried to launch this playbook :

- hosts: win_clients
  gather_facts: true
  strategy: free
  tasks:
  - name: Get Ansible network facts
    debug:
      msg: "{{ ansible_facts['interfaces'] }}"

and it works fine but I have all the informations about the interfaces. I just want the "connection_name".

When I put this line in the playbook : msg: "{{ ansible_facts['interfaces']['connection_name'] }}"

It shows this message at the execution :

FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'connection_name'\n\nThe error appears to be in '/home/sopra/git/rnd-windows/automation/playbooks/Dns/test.yaml': line 5, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: Get Ansible network facts\n ^ here\n"}

I don't understand because the variable "connection_name" is well defined.

Can somebody help me? Thanks. Have a good day !

2

2 Answers

0
votes

If you want to list the connection_name, use below, as ansible_facts['interfaces'] is an array

- hosts: win_clients
  gather_facts: true
  strategy: free
  tasks:
  - name: Get Ansible network facts
    debug:
      msg: "{{ item.connection_name }}"
    with_items:
      - "{{ ansible_facts['interfaces'] }}"
0
votes

Thank you very much for your support. I did resolve my problem. Basically, my playbook consists in changing the DNS configuration if the occurrence of one (old) DNS's IP if found.

# tasks file for configureDnsServerAddresses
# Get the configuration state about DNS
# If one occurrence of 'old_dnsserver' is found...
- name: Get DNS configuration
  win_shell: ipconfig /all | findstr {{ old_dnsserver }}
  register: check_old_dns
  changed_when: false
# '.rc' means the return code
  failed_when: check_old_dns.rc != 0 and check_old_dns.rc != 1
# ... it will be replaced by 2 new ones
- name: Change the DNS only if the IP {{ old_dnsserver }} is found
  win_dns_client:
    adapter_names: "{{ item.connection_name }}"
    ipv4_addresses:
    - "{{ dnsserver1 }}"
    - "{{ dnsserver2 }}"
# Array based on Ansible facts
  with_items:
    - "{{ ansible_facts['interfaces'] }}"
# Apply only if 'check_old_dns' is not empty, which means that the old DNS is found
  when: check_old_dns.stdout | length > 0

This playbook is role-based, so the variables are stored in a "defaults" folder. The code mentioned above was intended for testing purposes.