authenticationconfigurationansiblejunos-automation

Using Ansible to set new Junos OS User


Im trying to create a new user for my vMX router running Junos OS. The user needs to be a super-user with the following credentials:

username: admin

password: admin123

Doing this directly from the command line is easy, I simply change to edit mode and type the following command

set system login user admin uid 3000 class super-user authentication plain-text-password

The console then prompts you to enter the password and then confirm it, so I enter the password as follows

admin123
admin123

I can then commit the changes and the user has been created. The problem lies when I try to repeat this process for 8 different vmx routers using ansible. I have set up the following playbook:

---
- name: Create admin user
  hosts: newvmx
  roles:
          - Juniper.junos
  connection: local
  gather_facts: no

  tasks:

          - name: create new user
            juniper_junos_config:
                    config_mode: "private"
                    load: "set"
                    lines:
                            - "set system login user admin123 uid 3000 class super-user authentication plain-text password"
                            - "admin123"
                            - "admin123"

But this returned the following error:


PLAY [Create admin user] ***************************************************************************************************************************************************************************************

TASK [create new user] **************************************************************************************************************************************************************************************
fatal: [vMX6]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "msg": "Failure loading the configuraton: ConfigLoadError(severity: error, bad_element: plain-text, message: error: syntax error\nerror: unknown command\nerror: unknown command)"}

I assume this is to do with the final part of the command, since when performing this directly on the machine, the user is prompted to enter the password, so when the lines are passed by ansible, the OS does not know what to do with the lines containing only the password.

I also tried the following playbook:

---
- name: Create admin user
  hosts: newvmx
  roles:
          - Juniper.junos
  connection: local
  gather_facts: no

  tasks:

          - name: create new user
            juniper_junos_config:
                    config_mode: "private"
                    load: "set"
                    lines:
                            - "set system login user admin uid 3000 class super-user authentication plain-text password admin123"

But that results in the syntax error below. So this was not a successful attempt.

PLAY [Create admin user] ***************************************************************************************************************************************************************************************

TASK [create new user] **************************************************************************************************************************************************************************************
fatal: [vMX10]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "msg": "Failure loading the configuraton: ConfigLoadError(severity: error, bad_element: plain-text, message: error: syntax error)"}

So is there any way in which I can pass this command to create a new user and set a password for the new user using ansible to speed up the process of repeating it for multiple devices?


Solution

  • Its comunicates over netconf session. and is not a user interactive session. Hence you can get the requried config in a file and load it usinf juniper_junos_config

    - name: create user
      hosts: all
      roles:
        - Juniper.junos
      connection: local
      gather_facts: no
      tasks:
        - name: Change Password
          juniper_junos_config:
            host: "{{ ansible_ssh_host }}"
            port: "{{ ansible_ssh_port }}"
            user: user1
            passwd: "{{ passwd }}"
            file: create_user.conf
            load: merge
    

    cat create_user.conf <<< define as per the config needed.

    system {
        -----
    }
    

    Let me know if you need futher help.