Ansible AWX - Using Python to create bulk users

Introduction

AWX provides an interface to create users via the front-end, command line through the administer tools as well as through the RESTful API.

What if you need to create multiple users. Rather than doing this manually I’ve wrote a Python script which automates the process (making use of the API).

The script carries out the following:

  • reads in a configuration file which stores authentication information, plus path to a users csv file
  • reads in a users.csv file which stores new users information
  • for each request sent to the API an OAuth2 token is sent in the request header: "Authorization: Bearer {OAUTH2 TOKEN}"
  • a HTTP POST is sent to endpoint: <AWX_HOST>/api/v2/users to create a new user

Configuration file

The config.json file.

    {
        "settings": [
            {
                "awx_server_url": "<replace with AWX server url>",
                "api_version": 2,
                "userid": "admin",
                "token": "<replace with OAuth2 token>",
                "user_csv": "users.csv"
            }
        ]
    }

I’ve used the admin user here to demonstrate the process, but you could use a custom user with equivalent admin permissions to create users.

Users file

To store a list of users i’ve used a csv file. It makes a list of user easy to edit in something like Excel.

    username,firstname,lastname,email,is_superuser,is_system_auditor,password
    user1,user_firstname,user_lastname,email@noreply.com,FALSE,FALSE,changeme
    user2,user_firstname,user_lastname,email@noreply.com,FALSE,FALSE,changeme
    user3,user_firstname,user_lastname,email@noreply.com,FALSE,FALSE,changeme
    user4,user_firstname,user_lastname,email@noreply.com,FALSE,FALSE,changeme
    user5,user_firstname,user_lastname,email@noreply.com,FALSE,FALSE,changeme
    user6,user_firstname,user_lastname,email@noreply.com,FALSE,FALSE,changeme
    user7,user_firstname,user_lastname,email@noreply.com,FALSE,FALSE,changeme

Python script usage

   usage: awx_users_admin.py [-h] [--userid USERID] [--config CONFIG]
   
   Ansible AWX User creator.
   
   optional arguments:
     -h, --help       show this help message and exit
     --userid USERID  Specify the userid to connect to the AWX API.
     --config CONFIG  Specify the config json file.

Running the script

    ./awx_users_admin.py --userid admin --config configuration.json

    2020-03-31 12:42:19:INFO > Creates AWX user accounts.
    2020-03-31 12:42:19:INFO > Authenticating user admin.
    2020-03-31 12:42:19:INFO > Reading in configuration file = configuration.json
    2020-03-31 12:42:19:INFO > Authentication settings found for userid = admin
    2020-03-31 12:42:19:INFO > Creating user, with username = user1
    2020-03-31 12:42:19:INFO > (OK) - Created.
    2020-03-31 12:42:19:INFO > Creating user, with username = user2
    2020-03-31 12:42:21:INFO > (OK) - Created.
    2020-03-31 12:42:21:INFO > Creating user, with username = user3
    2020-03-31 12:42:21:INFO > (OK) - Created.
    2020-03-31 12:42:21:INFO > Creating user, with username = user4
    2020-03-31 12:42:23:INFO > (OK) - Created.
    2020-03-31 12:42:23:INFO > Creating user, with username = user5
    2020-03-31 12:42:24:INFO > (OK) - Created.
    2020-03-31 12:42:24:INFO > Creating user, with username = user6
    2020-03-31 12:42:25:INFO > (OK) - Created.
    2020-03-31 12:42:25:INFO > Creating user, with username = user7
    2020-03-31 12:42:27:INFO > (OK) - Created.

The script and resources can be found in GitLab.

https://gitlab.com/spengler84/ansible-awx.git

Last updated on 31 Mar 2020
Published on 31 Mar 2020