I have an ansible playbook which moves .network and .netdev files to the /etc/systemd/network/ directory. Theses files are generated automatically and I can't know their names or the name/number of the interfaces that will be (automatically) created.

The .network and .netdev contains the name of the interfaces that are created :

  • 00-wg0.netdev
  • 00-wg0.network
  • 10-wg1.netdev
  • 10-wg1.network
  • 20-wg2.netdev
  • 20-wg2.network
  • [...]

How can I (using ansible) :

  • Get a list of the interfaces names like wg0 or wg1... using the files which are located in /etc/systemd/network/ ?
  • Restart the interfaces (set them down, then up) using this list ?


  • Why do you want to use those files? Try running the setup module. There should be a fact called ansible_interfaces listing the available interfaces.
    – Panki
    Jan 31, 2020 at 9:54
  • because when I just copied the .network and .netdev the interfaces doesn't exists yet so I can't know their names except in the files name. Furthermore, I would like to avoid restarting systemd because this would break the SSH connection (I think)
    – amprxc2
    Jan 31, 2020 at 10:09

1 Answer 1


To read the files you can use lookup plugin:

  file_contents: "{{lookup('file', 'path/to/file.txt')}}"

To restart network interface you can use service module:

- name: Restart network service for vlan interface
    name: network
    state: restarted
    args: '{{ item }}'
  with_items: '{{ var_with_iface_list }}'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .