How to bulk add hosts in Ansible AWX using API?


I have installed the latest Ansible AWX in K3s, following GitHub - kurokobo/awx-on-k3s: An example implementation of AWX on single node K3s using AWX Operator, with easy-to-use simplified configuration with ownership of data and passwords.. It has K3s v1.28.7+k3s1, AWX Operator 2.13.1 and AWX 24.0.0.

On AWX Gui, I know how to add inventory and hosts.

Now I want to find out how to bulk add hosts, for example, 200 hosts.

At first, I tried “awx-manage”:

awx-manage inventory_import --inventory-name myInventory --source myHostsList

But I got error:
WARNING This command is not able to run on kubernetes-based deployment. This action should be done using the API.

Then I found this articles:

It talks about one could bulk add host as following:

Post to /api/v2/bulk/host_create

POST accepts inventory and hosts desired to create, responds with how many hosts created, and link to inventory

But I still can’t figure out what is the exact curl command to bulk add hosts.
Also does it mean I have to list all my 200 hosts out in the POST command? Will it accept filename with host list inside?



I figured it out. I didn’t realize I can do it on browser. Pretty cool!

So on my browser, I go to:

and fill in my inventory id and hosts to add:

“inventory”: 2,
“hosts”: [{“name”: “myhost01”},{“name”: “myhost02”}]

It works!

I guess now I need to figure out how to build the host list for 200 hosts.
Can POST accept filename with host name inside?

Also, I still want to know how to use cURL command line.

I tried this:
#curl -k -X POST
{“detail”:“Authentication credentials were not provided. To establish a login session, visit /api/login/.”}

So how to specify Authentication in cURL command for Ansible AWX?

Can someone help?



I keep trying and figured out.

  1. Create one host by specifying host on command line

curl -k -u admin:myPassword -H “Content-Type: application/json” -X POST --data ‘{“name”: “myhost01”}’

  1. Create one host by creating payload.json file
    {“name”: “myhost01”}

curl -k -u admin:myPassword -d @payload.json -H “Content-Type: application/json” -X POST

(to be continued, because as new user I can only put two links :slight_smile: )


  1. Create bulk hosts by specifying hosts on command line

curl -k -u admin:myPassword -H “Content-Type: application/json” -X POST --data ‘{“inventory”: 2, “hosts”: [{“name”: “myhost01”},{“name”: “myhost02”}]}’

  1. Create bulk hosts by creating payload.json file
    “inventory”: 2,
    {“name”: “myhost01”},
    {“name”: “myhost02”}

curl -k -u admin:myPassword -d @payload2.json -H “Content-Type: application/json” -X POST

This payload.json file content can also be used to do posts in a browser.

So now I just need to modify my host list file a little bit (add prefix and surffix to the hostname). Then I can use browser or curl command to add 200 hosts!



1 Like

Are you able to use the awx.awx Collection, and run Ansible locally to push the changes to your AWX?

There’s a similar topic here where I show how to create an inventory. You could then use module to create the hosts in the inventories that you created.