How to bulk add hosts in Ansible AWX using API?

Hi,

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?

Thanks,
Stellar

Hello,

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

So on my browser, I go to:
https://myansible.com/api/v2/bulk/host_create/

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 https://myansible.com/v2/bulk/host_create/
{“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?

Thanks,
Stellar

Hello,

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 https://myansible.com/api/v2/inventories/2/hosts/ --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 https://myansible.com/api/v2/inventories/2/hosts/

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

Thanks,
Stellar

  1. Create bulk hosts by specifying hosts on command line

curl -k -u admin:myPassword -H “Content-Type: application/json” -X POST https://myansible.com/api/v2/bulk/host_create/ --data ‘{“inventory”: 2, “hosts”: [{“name”: “myhost01”},{“name”: “myhost02”}]}’

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

curl -k -u admin:myPassword -d @payload2.json -H “Content-Type: application/json” -X POST https://myansible.com/api/v2/bulk/host_create/

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!

Cheers!

Thanks,
Stellar

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 awx.awx.host module to create the hosts in the inventories that you created.

How can I add multiple hosts with “Description”, “Inventory” and “Variables” of each host using /api/v2/… other any other method?
I tried this template it didn’t work:
{
“name”: “host1”,
“description”: “This is a test host”,
“inventory”: 1,
“enabled”: true,
“variables”: “—\nfavcolor: blue”
}

Also, I have the same problem with adding multiple job template. I’d appreciate it if someone could help me please.

Many Thanks

Hello @Komiko,
Are you able to achieve this using the method @zgong describes in this thread?

@Komiko - you may also want to see if the official reference docs on using the bulk endpoint helps: 11. AWX API Reference Guide — Ansible AWX community documentation