1

As described in the ansible documentation for routeros I need to add +cet512w to my ansible_user. But I do not want to hardcode the user in every routeros play[book] so I use {{ ansible_user }} everywhere. Is it possible to concat that +cet512w string to the ansible_user variable in only the routeros play[book]s by setting the variable? Or would it be somehow possible to concat it in group_vars or in inventory? If yes, then how?

following does not work:

- name: RouterOS - my playbook
  hosts: routeros
  vars:
    - ansible_connection: ansible.netcommon.network_cli
    - ansible_network_os: community.network.routeros
    - ansible_user: "{{ ansible_user + '+cet512w' }}"

and gives this error msg: FAILED! => {"msg": "An unhandled exception occurred while templating '{{ ansible_user }} + '+cet512w''. Error was a <class 'ansible.errors.AnsibleError'>, original message: An unhandled exception occurred while templating '{{ ansible_user }} + '+cet512w''. Error was a <class 'ansible.errors.AnsibleError'>, original message: An unhandled exception...

Christoph Lösch
  • 645
  • 7
  • 22

1 Answers1

2

You can put it into the pre_tasks

- hosts: all

  pre_tasks:

    - set_fact:
        ansible_user: "{{ (ansible_connection == 'ansible.netcommon.network_cli')|
                          ternary(ansible_user ~ '+cet512w', ansible_user) }}"

  tasks:

    - debug:
        var: ansible_user

For example, given the below inventory

[test]
host_A
router_A

[test:vars]
ansible_user=admin

[routers]
router_A

[routers:vars]
ansible_connection=ansible.netcommon.network_cli
ansible_network_os=community.network.routeros

The play gives (abridged)

TASK [debug] ****************************************************************
ok: [host_A] => 
  ansible_user: admin
ok: [router_A] => 
  ansible_user: admin+cet512w
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63