0

I'm working on an ansible project for network troubleshooting, and I'm trying to set the ansible_network_os variable automatically. I have an OS-agnostic python script that uses paramiko to run a show ver command on routers, then returns a variable corresponding to the detected OS. The problem comes when I try to actually set the variable in the playbook.

Since ansible_network_os has not been set yet, the hosts variable needs to be set to localhost for this task. In the task, I'm looping over the network devices' IP addresses (item[0]) and the corresponding variables returned from the python script (item[1]). Using the zip() function, they're combined into one list for simultaneous traversal:

  - name: Setting network variable...
    set_fact:
      hostvars[{{item[0]}}].ansible_network_os: "{{ item[1] }}"
    loop: "{{ groups['targets']|zip(device_os.stdout_lines)|list }}"

This gives the error msg: The variable name 'hostvars[172.16.1.X].ansible_network_os' is not valid. Variables must start with a letter or underscore character, and contain only letters, numbers and underscores. where X is any number 1-10 that corresponds to the device's internal IP.

I understand what the problem is, but I'm struggling to find a solution or workaround. What is the best way to set the value of a hostvars var dynamically like this?

Seth R.
  • 15
  • 7
  • 1
    It sounds suspiciously like you're trying to reinvent [`- setup:`](https://docs.ansible.com/ansible/2.10/collections/ansible/builtin/setup_module.html); AIUI, if invoking the whole `setup:` process bothers you, then using its `filter: "ansible_network_os"` option will restrict it to just that one discovered hostvar – mdaniel Feb 23 '21 at 16:34
  • 1
    @mdaniel I think I'm a bit confused. ```setup``` is used to gather facts about remote hosts, right? Calling it from the localhost to loop and gather facts about the network devices works, but there is no variable in the results that has the network os information. Basically what I'm saying is I tried this and there is no ```ansible_network_os``` in the results, unless I'm doing something wrong. I believe ```ansible_network_os``` has to be set manually, which is why I'm trying to automatically define it in the task described above rather than defining it in the inventory file or vars file. – Seth R. Feb 23 '21 at 17:15
  • I don't have a Cisco device to provide further guidance, but [yesterday's discussion about `ansible_network_os`](https://stackoverflow.com/questions/66307735/ansible-run-cisco-commands-without-knowledge-of-ansible-network-os-variable#comment117259254_66307735) was pleased with the use of `setup:` so :shrug: I guess. I don't know what you're saying about `localhost` and your code snippet makes no such mention, so good luck with that, too – mdaniel Feb 24 '21 at 05:03
  • @mdaniel well thanks anyway for trying. I tried setting ```gather_facts``` to yes as you suggested in the other post, but it still fails with the error ```Unable to automatically determine host network os. Please manually configure ansible_network_os value for this host```. Thanks again, I'll keep searching for a solution! – Seth R. Feb 24 '21 at 16:36

0 Answers0