# How to bulk add hosts in Ansible AWX using API?

**URL:** <https://forum.ansible.com/t/how-to-bulk-add-hosts-in-ansible-awx-using-api/4481>\
**Category:** Get Help\
**Tags:** kubernetes, awx\
**Created:** [March 20, 2024, 3:45pm UTC](https://forum.ansible.com/t/how-to-bulk-add-hosts-in-ansible-awx-using-api/4481 "2024-03-20T15:45:50Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![zgong](https://avatars.discourse-cdn.com/v4/letter/z/b5a626/32.png) [@zgong](https://forum.ansible.com/u/zgong)\
**Post date:** [March 20, 2024, 3:45pm UTC](https://forum.ansible.com/t/how-to-bulk-add-hosts-in-ansible-awx-using-api/4481/1 "2024-03-20T15:45:51Z")

</div>

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.](https://github.com/kurokobo/awx-on-k3s). 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:

> <https://github.com/ansible/awx/pull/13462>
>
> \##### SUMMARY
> Replaces #13358 and #13369 
> 
> \##### ISSUE TYPE
> 
> - New or Enha…nced Feature
> 
> 
> \##### COMPONENT NAME
> 
> - API
> 
> 
> \##### ADDITIONAL INFORMATION
> 
> 
> 
> \- With this change you could launch a bulk job as following:
> 
> Post to \`/api/v2/bulk/job\_launch\`
> \`\`\`
> {
> "name": "Bulk Job Launch",
> "jobs": \[
> {"unified\_job\_template": 9, "identifier":"foo", "limit": "kansas", "credentials": \[1\], "inventory": 1, "execution\_environment": 3},
> {"unified\_job\_template": 7, "identifier":"bar", "limit": "kansas", "credentials": \[1\], "inventory": 1, "execution\_environment": 3},
> {"unified\_job\_template": 7, "limit": "kansas", "credentials": \[1\], "inventory": 1, "execution\_environment": 3}
> \],
> "inventory": 2
> }
> \`\`\`
> 
> The above will launch a workflow job with 3 nodes in it.
> 
> \- One could also bulk add host as following:
> 
> Post to \`/api/v2/bulk/host\_create\`
> \`\`\`
> {"inventory": 1, "hosts": \[{"name": "foo-0-2022-12-22 01:29:08.995393"}, {"name": "foo-1-2022-12-22 01:29:08.995393"}, {"name": "foo-2-2022-12-22 01:29:08.995393"}, {"name": "foo-3-2022-12-22 01:29:08.995393"}, {"name": "foo-4-2022-12-22 01:29:08.995393"}, {"name": "foo-5-2022-12-22 01:29:08.995393"}, {"name": "foo-6-2022-12-22 01:29:08.995393"}, {"name": "foo-7-2022-12-22 01:29:08.995393"}, {"name": "foo-8-2022-12-22 01:29:08.995393"}, {"name": "foo-9-2022-12-22 01:29:08.995393"}, {"name": "foo-10-2022-12-22 01:29:08.995393"}, {"name": "foo-11-2022-12-22 01:29:08.995393"}, {"name": "foo-12-2022-12-22 01:29:08.995393"}, {"name": "foo-13-2022-12-22 01:29:08.995393"}, {"name": "foo-14-2022-12-22 01:29:08.995393"}, {"name": "foo-15-2022-12-22 01:29:08.995393"}, {"name": "foo-16-2022-12-22 01:29:08.995393"}, {"name": "foo-17-2022-12-22 01:29:08.995393"}, {"name": "foo-18-2022-12-22 01:29:08.995393"}, {"name": "foo-19-2022-12-22 01:29:08.995393"}, {"name": "foo-20-2022-12-22 01:29:08.995393"}, {"name": "foo-21-2022-12-22 01:29:08.995393"}, {"name": "foo-22-2022-12-22 01:29:08.995393"}, {"name": "foo-23-2022-12-22 01:29:08.995393"}, {"name": "foo-24-2022-12-22 01:29:08.995393"}, {"name": "foo-25-2022-12-22 01:29:08.995393"}, {"name": "foo-26-2022-12-22 01:29:08.995393"}, {"name": "foo-27-2022-12-22 01:29:08.995393"}, {"name": "foo-28-2022-12-22 01:29:08.995393"}, {"name": "foo-29-2022-12-22 01:29:08.995393"}, {"name": "foo-30-2022-12-22 01:29:08.995393"}, {"name": "foo-31-2022-12-22 01:29:08.995393"}, {"name": "foo-32-2022-12-22 01:29:08.995393"}, {"name": "foo-33-2022-12-22 01:29:08.995393"}, {"name": "foo-34-2022-12-22 01:29:08.995393"}, {"name": "foo-35-2022-12-22 01:29:08.995393"}, {"name": "foo-36-2022-12-22 01:29:08.995393"}, {"name": "foo-37-2022-12-22 01:29:08.995393"}, {"name": "foo-38-2022-12-22 01:29:08.995393"}, {"name": "foo-39-2022-12-22 01:29:08.995393"}, {"name": "foo-40-2022-12-22 01:29:08.995393"}, {"name": "foo-41-2022-12-22 01:29:08.995393"}, {"name": "foo-42-2022-12-22 01:29:08.995393"}, {"name": "foo-43-2022-12-22 01:29:08.995393"}, {"name": "foo-44-2022-12-22 01:29:08.995393"}, {"name": "foo-45-2022-12-22 01:29:08.995393"}, {"name": "foo-46-2022-12-22 01:29:08.995393"}, {"name": "foo-47-2022-12-22 01:29:08.995393"}, {"name": "foo-48-2022-12-22 01:29:08.995393"}, {"name": "foo-49-2022-12-22 01:29:08.995393"}, {"name": "foo-50-2022-12-22 01:29:08.995393"}, {"name": "foo-51-2022-12-22 01:29:08.995393"}, {"name": "foo-52-2022-12-22 01:29:08.995393"}, {"name": "foo-53-2022-12-22 01:29:08.995393"}, {"name": "foo-54-2022-12-22 01:29:08.995393"}, {"name": "foo-55-2022-12-22 01:29:08.995393"}, {"name": "foo-56-2022-12-22 01:29:08.995393"}, {"name": "foo-57-2022-12-22 01:29:08.995393"}, {"name": "foo-58-2022-12-22 01:29:08.995393"}, {"name": "foo-59-2022-12-22 01:29:08.995393"}, {"name": "foo-60-2022-12-22 01:29:08.995393"}, {"name": "foo-61-2022-12-22 01:29:08.995393"}, {"name": "foo-62-2022-12-22 01:29:08.995393"}, {"name": "foo-63-2022-12-22 01:29:08.995393"}, {"name": "foo-64-2022-12-22 01:29:08.995393"}, {"name": "foo-65-2022-12-22 01:29:08.995393"}, {"name": "foo-66-2022-12-22 01:29:08.995393"}, {"name": "foo-67-2022-12-22 01:29:08.995393"}, {"name": "foo-68-2022-12-22 01:29:08.995393"}, {"name": "foo-69-2022-12-22 01:29:08.995393"}, {"name": "foo-70-2022-12-22 01:29:08.995393"}, {"name": "foo-71-2022-12-22 01:29:08.995393"}, {"name": "foo-72-2022-12-22 01:29:08.995393"}, {"name": "foo-73-2022-12-22 01:29:08.995393"}, {"name": "foo-74-2022-12-22 01:29:08.995393"}, {"name": "foo-75-2022-12-22 01:29:08.995393"}, {"name": "foo-76-2022-12-22 01:29:08.995393"}, {"name": "foo-77-2022-12-22 01:29:08.995393"}, {"name": "foo-78-2022-12-22 01:29:08.995393"}, {"name": "foo-79-2022-12-22 01:29:08.995393"}, {"name": "foo-80-2022-12-22 01:29:08.995393"}, {"name": "foo-81-2022-12-22 01:29:08.995393"}, {"name": "foo-82-2022-12-22 01:29:08.995393"}, {"name": "foo-83-2022-12-22 01:29:08.995393"}, {"name": "foo-84-2022-12-22 01:29:08.995393"}, {"name": "foo-85-2022-12-22 01:29:08.995393"}, {"name": "foo-86-2022-12-22 01:29:08.995393"}, {"name": "foo-87-2022-12-22 01:29:08.995393"}, {"name": "foo-88-2022-12-22 01:29:08.995393"}, {"name": "foo-89-2022-12-22 01:29:08.995393"}, {"name": "foo-90-2022-12-22 01:29:08.995393"}, {"name": "foo-91-2022-12-22 01:29:08.995393"}, {"name": "foo-92-2022-12-22 01:29:08.995393"}, {"name": "foo-93-2022-12-22 01:29:08.995393"}, {"name": "foo-94-2022-12-22 01:29:08.995393"}, {"name": "foo-95-2022-12-22 01:29:08.995393"}, {"name": "foo-96-2022-12-22 01:29:08.995393"}, {"name": "foo-97-2022-12-22 01:29:08.995393"}, {"name": "foo-98-2022-12-22 01:29:08.995393"}, {"name": "foo-99-2022-12-22 01:29:08.995393"}\]}
> \`\`\`
> \## Definition of Done
> 
> \### Bulk Add Host
> 
> \#### General API:
> 
> \- \[x\] OPTIONS request responds with description of schema accepted
> \- \[x\] POST accepts inventory and hosts desired to create, responds with how many hosts created, and link to inventory
> \- \[x\] Have POST return id's of the created hosts
> \- \[x\] Create ActivityStream entry/entries to create
> 
> \#### RBAC
> \- \[x\] org admin for org on inventory can add hosts
> \- \[x\] inventory admin at org level for org of inventory can add hosts
> \- \[x\] superuser can add hosts, bypass rbac
> \- \[x\] inventory admin of particular inventory can add hosts
> 
> \#### CLI/collection/awxkit
> 
> \- \[x\] add collection module for bulk inventory add
> \- \[x\] add cli support for bulk inventory add
> \- \[x\] add awxkit support for bulk inventory add
> 
> \#### Testing
> 
> \- \[x\] add awx functional test asserting on number of queries it takes
> \- \[x\] add awx functional test for RBAC
> \- \[x\] add integration tests for api
> \- \[x\] add integration tests for collection
> \- \[x\] add integration tests for cli
> 
> \### Bulk Job Launch
> 
> \#### At WorkflowJob level:
> \- \[x\] reach feature parity with things you can normally set on a WorkflowJobTemplate that get inherited by the WorkflowJob
> \- \[x\] "name" (done)
> \- \[x\] "description": "",
> \- \[x\] "extra\_vars": "",
> \- \[x\] "organization": (see below in RBAC section)
> \- \[x\] "inventory": null,
> \- \[x\] "limit": "",
> \- \[x\] "scm\_branch": ""
> \- \[x\] "skip\_tags": "",
> \- \[x\] "job\_tags": ""
> 
> \#### At workflow job node level:
> \- \[x\] implement RBAC and appropriate relating of other related objects
> \- \[x\] inventory
> \- \[x\] credentials
> \- \[x\] instance groups
> \- \[x\] execution environment
> \- \[x\] labels
> \- \[x\] implement schema check and assignment of other character/integer fields
> \- \[x\] 'limit'
> \- \[x\] 'char\_prompts',
> \- \[x\] 'diff\_mode',
> \- \[x\] 'extra\_data',
> \- \[x\] 'forks',
> \- \[x\] 'job\_slice\_count',
> \- \[x\] 'job\_tags',
> \- \[x\] 'job\_type'
> \- \[x\] 'scm\_branch',
> \- \[x\] 'skip\_tags',
> \- \[x\] 'survey\_passwords',
> \- \[x\] 'timeout',
> \- \[x\] 'verbosity',
> 
> \#### RBAC
> \- \[x\] add "is\_bulk\_job" field to WorkflowJob model
> \- \[x\] associate the user organization to the WorkflowJob if they only have one org, or allow them to specify an organization they belong to as the org (unless they are sysadmin, in which case they can assocaite any org). If user has multiple organizations, return a validation error and tell them they need to pick one
> \- \[x\] Anyone who launched a bulk job can see the one they launched
> \- \[x\] An org admin for the Organization on a bulk job can see that bulk job
> \- \[x\] open up RBAC access for WorkflowJob and WorkflowJobNodes to allow normal users view bulk jobs they launched in detail view. They won't see them in the general job list view
> \- \[x\] check that relaunch is prohibited except for super user. we get this for free since its "orphaned" (has no wfjt)
> \- \[x\] record workflow job launch in activity stream.
> 
> \#### Maintainablity
> \- \[x\] figure out if there is a way we can share some code between access.py and the validation method to reduce risk of drift between the two
> \- \[x\] figure out if we can chunk up the validate method into some more sensible helper methods to make it a bit more readable/have logical sections. E.g. "\_validate\_permissions", "\_objectify\_attrs\_for\_create"
> \- \[x\] Add a configuration for the MAX bulk host and MAX bulk job launch 
> 
> \#### General API functionality
> \- \[x\] provide /api/v2/bulk/job\_launch
> \- \[x\] list of individual nodes will be accessed via workflow\_job\_nodes related item on the workflow job returned
> \- \[x\] return something sensible to a GET to /api/v2/bulk/job\_launch. Use other launch endpoints as a reference. They appear to return a json blob with all the default values of the items you could launch
> \- \[x\] in the DjangoRestFramework API browser thing, in the "POST" form, it appears there is someway you can populate it with sample POST data. Provide something sensible for /api/v2/bulk\_jobs/launch
> \- \[x\] Provide some sensible OPTIONS response to /api/v2/bulk/job\_launch
> 
> \#### Tests
> \- \[x\] awxkit part to expose the api/v2/bulk
> \- \[x\] awxkit part to expose api/v2/bulk/job\_launch
> \- \[x\] Add tests that do some validation that # of queries stays reasonable
> \- \[x\] Test with LOTS of unified\_job\_templates in existence in the system and regular users. Concern is RBAC check queries could return a lot of pk's -- question is, where does this become a problem. I did nominal testing with ~ 10k job templates, didn't appear to be an issue but worth poking at some more.
> \- \[x\] Add tests that do some validation of RBAC access
> \- \[x\] Verify current behavior of launching jobs with "promptable" items that are either not allowed by the job template or you don't have access to.

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

---

<div class="post-metadata">

**Author:** ![zgong](https://avatars.discourse-cdn.com/v4/letter/z/b5a626/32.png) [@zgong](https://forum.ansible.com/u/zgong)\
**Post date:** [March 20, 2024, 5:42pm UTC](https://forum.ansible.com/t/how-to-bulk-add-hosts-in-ansible-awx-using-api/4481/2 "2024-03-20T17:42:18Z")

</div>

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/](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/](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

---

<div class="post-metadata">

**Author:** ![zgong](https://avatars.discourse-cdn.com/v4/letter/z/b5a626/32.png) [@zgong](https://forum.ansible.com/u/zgong)\
**Post date:** [March 20, 2024, 7:23pm UTC](https://forum.ansible.com/t/how-to-bulk-add-hosts-in-ansible-awx-using-api/4481/3 "2024-03-20T19:23:32Z")

</div>

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

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

Thanks,  
Stellar

---

<div class="post-metadata">

**Author:** ![zgong](https://avatars.discourse-cdn.com/v4/letter/z/b5a626/32.png) [@zgong](https://forum.ansible.com/u/zgong)\
**Post date:** [March 20, 2024, 7:26pm UTC](https://forum.ansible.com/t/how-to-bulk-add-hosts-in-ansible-awx-using-api/4481/4 "2024-03-20T19:26:30Z")

</div>

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/](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/](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

---

<div class="post-metadata">

**Author:** ![Dustin](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dustin/32/2573_2.png) [@Dustin](https://forum.ansible.com/u/Dustin)\
**Post date:** [March 20, 2024, 10:39pm UTC](https://forum.ansible.com/t/how-to-bulk-add-hosts-in-ansible-awx-using-api/4481/5 "2024-03-20T22:39:19Z")

</div>

Are you able to use the [awx.awx](https://docs.ansible.com/ansible/latest/collections/awx/awx/index.html) Collection, and run Ansible locally to push the changes to your AWX?

There’s a similar topic [here](http://forum.ansible.com/t/connect-to-api-ansible-awx/3717) where I show how to create an inventory. You could then use [awx.awx.host](https://docs.ansible.com/ansible/latest/collections/awx/awx/host_module.html#ansible-collections-awx-awx-host-module) module to create the hosts in the inventories that you created.

---

<div class="post-metadata">

**Author:** ![Komiko](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@Komiko](https://forum.ansible.com/u/Komiko)\
**Post date:** [July 21, 2024, 2:28pm UTC](https://forum.ansible.com/t/how-to-bulk-add-hosts-in-ansible-awx-using-api/4481/6 "2024-07-21T14:28:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![djyasin](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/djyasin/32/114_2.png) [@djyasin](https://forum.ansible.com/u/djyasin)\
**Post date:** [July 24, 2024, 6:51pm UTC](https://forum.ansible.com/t/how-to-bulk-add-hosts-in-ansible-awx-using-api/4481/7 "2024-07-24T18:51:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tvo318](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/tvo318/32/3406_2.png) [@tvo318](https://forum.ansible.com/u/tvo318)\
**Post date:** [July 25, 2024, 5:25pm UTC](https://forum.ansible.com/t/how-to-bulk-add-hosts-in-ansible-awx-using-api/4481/8 "2024-07-25T17:25:41Z")

</div>

@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](https://ansible.readthedocs.io/projects/awx/en/latest/rest_api/api_ref.html#/Bulk)

---

<div class="post-metadata">

**Author:** ![Komiko](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@Komiko](https://forum.ansible.com/u/Komiko)\
**Post date:** [July 30, 2024, 10:31pm UTC](https://forum.ansible.com/t/how-to-bulk-add-hosts-in-ansible-awx-using-api/4481/9 "2024-07-30T22:31:09Z")

</div>

Hello @djyasin  
Sorry for my late reply. Unfortunately I’ve faced a problem on my awx server and I can’t test it now. It may take a couple of days to resolve the problem. I’ll give it a go as soon as I can fix the server issue and will post the result here.  
Thank you

---

<div class="post-metadata">

**Author:** ![Komiko](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@Komiko](https://forum.ansible.com/u/Komiko)\
**Post date:** [July 30, 2024, 10:32pm UTC](https://forum.ansible.com/t/how-to-bulk-add-hosts-in-ansible-awx-using-api/4481/10 "2024-07-30T22:32:28Z")

</div>

I’ll read it.  
Thank you @tvo318

---

<div class="post-metadata">

**Author:** ![binbashroot](https://avatars.discourse-cdn.com/v4/letter/b/ad7895/32.png) [@binbashroot](https://forum.ansible.com/u/binbashroot)\
**Post date:** [July 30, 2024, 11:58pm UTC](https://forum.ansible.com/t/how-to-bulk-add-hosts-in-ansible-awx-using-api/4481/11 "2024-07-30T23:58:36Z")

</div>

Use the bulk\_host\_create role in the redhat cop controller\_configuration collection. See: [GitHub - redhat-cop/controller\_configuration: A collection of roles to manage Ansible Controller and previously Ansible Tower](https://github.com/redhat-cop/controller_configuration). Use ansible (or awx) to populate your awx configuration.

---

<div class="post-metadata">

**Author:** ![jhoney12](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@jhoney12](https://forum.ansible.com/u/jhoney12)\
**Post date:** [August 22, 2024, 10:49am UTC](https://forum.ansible.com/t/how-to-bulk-add-hosts-in-ansible-awx-using-api/4481/12 "2024-08-22T10:49:22Z")

</div>

To bulk add hosts in Ansible AWX via the API, you can use the /api/v2/bulk/host\_create/ endpoint with a properly formatted JSON payload. For authentication, include -u admin:myPassword in your cURL command. If you have a large list of hosts, prepare a JSON file with the inventory ID and host details, and then use the --data @payload.json option with cURL. This method is efficient for managing numerous hosts, much like handling multiple shipments with a tool like [TraceShipments](https://traceshipments.com/).

---

<div class="post-metadata">

**Author:** ![GitGorgon](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/gitgorgon/32/4710_2.png) [@GitGorgon](https://forum.ansible.com/u/GitGorgon)\
**Post date:** [September 12, 2024, 6:52am UTC](https://forum.ansible.com/t/how-to-bulk-add-hosts-in-ansible-awx-using-api/4481/13 "2024-09-12T06:52:28Z")

</div>

Why do AWX developers want to make the simplest, most important tasks so difficult? Just blows my mind.

---

<div class="post-metadata">

**Author:** ![GitGorgon](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/gitgorgon/32/4710_2.png) [@GitGorgon](https://forum.ansible.com/u/GitGorgon)\
**Post date:** [September 12, 2024, 6:55am UTC](https://forum.ansible.com/t/how-to-bulk-add-hosts-in-ansible-awx-using-api/4481/14 "2024-09-12T06:55:53Z")

</div>

Can someone please explain to me what the point of AWX is?  
It’s meant to be a GUI for Ansible, right?  
But you can’t use the GUI to import a simple inventory file.  
Instead, we’re expected to post json to an API endpoint.  
What a disgrace.

---

<div class="post-metadata">

**Author:** ![Hiney](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/hiney/32/5197_2.png) [@Hiney](https://forum.ansible.com/u/Hiney)\
**Post date:** [October 18, 2024, 3:45am UTC](https://forum.ansible.com/t/how-to-bulk-add-hosts-in-ansible-awx-using-api/4481/15 "2024-10-18T03:45:45Z")

</div>

The whole thing is bloated and hard. The only thing it brings to the table over using bash scripts is splicing, and that’s achievable yourself. If i need four Execution Node, then I could use four servers with podman.

frustrated,  
H

---

<div class="post-metadata">

**Author:** ![pdin](https://avatars.discourse-cdn.com/v4/letter/p/df788c/32.png) [@pdin](https://forum.ansible.com/u/pdin)\
**Post date:** [September 11, 2025, 7:31am UTC](https://forum.ansible.com/t/how-to-bulk-add-hosts-in-ansible-awx-using-api/4481/16 "2025-09-11T07:31:13Z")

</div>

Has anyone done bulk imports with the API while adding variables: to hosts. I need to load a **couple 100 hosts** with multiple variables. I can only get **one** variable to load. I have this valid JSON that does not work when using the API. This is one way of the multiples attempts I have tried:  
{  
“inventory”: 11,  
“hosts”: [  
{  
“name”: “abc0017”,  
“variables”: [  
“vm\_name: Dev FM Core 2”,  
“primary\_ip\_address: 10.10.100.53”,  
“os\_vmware\_tools: Ubuntu Linux (64-bit)”,  
“ansible\_connection: ssh”,  
“ansible\_port: 99”  
]  
}  
]  
}

And I have been trying variations on this for a while ☹ with no success. Any ideas?? Thanks in advance!  
Pdin

---

<div class="post-metadata">

**Author:** ![dbrennand](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dbrennand/32/7830_2.png) [@dbrennand](https://forum.ansible.com/u/dbrennand)\
**Post date:** [September 11, 2025, 10:55am UTC](https://forum.ansible.com/t/how-to-bulk-add-hosts-in-ansible-awx-using-api/4481/18 "2025-09-11T10:55:05Z")

</div>

You could use AWX’s Inventory Sources functionality to add many hosts to AWX.

[18. Inventories — Ansible AWX community documentation](https://ansible.readthedocs.io/projects/awx/en/24.6.1/userguide/inventories.html#inventory-sources)

This assumes your project is in some SCM and AWX has connectivity to it.

I’ve personally used the `Sourced from a Project` method where you specify your inventory in an SCM (GitHub, GitLab etc.) and point it to the repository and AWX will handle registering all the hosts, groups and their variables for you.

Here is an example. My project’s inventory on GitHub: [home-ops/ansible/inventory at main · dbrennand/home-ops](https://github.com/dbrennand/home-ops/tree/main/ansible/inventory). I then use this playbook [home-ops/ansible/playbooks/playbook-awx.yml at main · dbrennand/home-ops](https://github.com/dbrennand/home-ops/blob/main/ansible/playbooks/playbook-awx.yml#L91) which uses the `awx.awx` collection modules to register the Inventory and associate the Inventory Source with said Inventory.

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [September 11, 2025, 11:35am UTC](https://forum.ansible.com/t/how-to-bulk-add-hosts-in-ansible-awx-using-api/4481/19 "2025-09-11T11:35:05Z")

</div>

This worked for me, hitting the `/api/v2/bulk/host_create/` AWX API endpoint:

```json
{
    "inventory": 7,
    "hosts": [{
      "name": "abc0017",
      "variables": "{\"vm_name\": \"Dev FM Core 2\", \"primary_ip_address\": \"10.10.100.53\", \"os_vmware_tools\": \"Ubuntu Linux (64-bit)\", \"ansible_connection\": \"ssh\", \"ansible_port\": 99}"
      }
    ]
}

```

The confusion comes from `variables:` needing to be a string containing `json` which, because it’s embedded within a json value, must be properly escaped. Apparently.

---

<div class="post-metadata">

**Author:** ![pdin](https://avatars.discourse-cdn.com/v4/letter/p/df788c/32.png) [@pdin](https://forum.ansible.com/u/pdin)\
**Post date:** [September 11, 2025, 1:57pm UTC](https://forum.ansible.com/t/how-to-bulk-add-hosts-in-ansible-awx-using-api/4481/20 "2025-09-11T13:57:14Z")

</div>

It worked in my AWX. Now to get all the data formatted correctly. Thanks **so** much!

---

<div class="post-metadata">

**Author:** ![pdin](https://avatars.discourse-cdn.com/v4/letter/p/df788c/32.png) [@pdin](https://forum.ansible.com/u/pdin)\
**Post date:** [September 11, 2025, 1:59pm UTC](https://forum.ansible.com/t/how-to-bulk-add-hosts-in-ansible-awx-using-api/4481/21 "2025-09-11T13:59:35Z")

</div>

Thanks for the suggestion. It’s early in the POC and yes, this SCM connection will be setup later.
