Having trouble creating an interface using the API

Hi all! We are switching from Satellite to Foreman and I am trying to port over our server provisioning tools. We have Foreman (Version 1.21.0) setup and I’m able to provision a server using the GUI (Oracle Linux), but I’m having some trouble with the interfaces trying to use the API. Sorry if I’m missing something simple, but I’m pretty new to this technology and I’ve been driving myself crazy.

The flow is generally:

  • Pick a host that has been DIscovered

  • Provision the host:

    curl -X PUT -s -k -u user:PASS -H ‘content-type: application/json’ -d ‘{“discovered_host”:{“environment_id”:1,“architecture_id”:1,“hostgroup_id”:2,“build”:“true”,“mac”:“dc:f4:aa:bb:10:aa”}}’ https://foreman.company.com/api/v2/discovered_hosts/macdcf4aabb10aa

This works fine. Then I try to configure the bond:

curl -X POST -s -k -u user:PASS -H 'content-type: application/json' -d '{"interface":{"name":"myserver.company.com","type":"Nic::Bond","ip":"","identifier":"bond0.152","primary":"true","provision":"false","virtual":"true","managed":"true","attached_to":"bond0","mode":"active-backup","attached_devices":"eth0,eth1","tag":152,"subnet_id":3,"domain_id":1,"bond_options":"mode=1 primary=eth0 arp_ip_target= arp_interval=1000"}}' https://foreman.company.com/api/v2/hosts/macdcf4aabb10aa.company.com/interfaces

And I get:

"error": {"id":null,"errors":{"primary":["host already has primary interface"]},"full_messages":["Primary host already has primary interface"]}

I’ve tried a couple of variations of this, based on things I’ve read and saw in the log, but I can’t seem to get by this and it works OK in Satellite so I feel like I must be missing something.

Any help would be appreciated.

Hi rhelfand,

I believe I’ve had similar issues, wanted to write something in python that lists discovered hosts and then provision selected host (configuring name, hostgroup, network bond, etc.) and I believe I managed to get it working (just now).

I had to break it down into four steps, not sure if there’s a better alternative.
DISCLAIMER: I just got this working, might be things listed below that’s not needed?

1. Provision the host

PUT request, API endpoint: “https://your-foreman-host/api/v2/discovered_hosts/{host_id}

This json_payload seems to work

    json_payload = {
                    "operatingsystem_name": "Name of your OS", 
                    "architecture_name": "x86_64",
                    "domain_name": "your.domain",
                    "name": "hostname",
                    "discovered_host": {
                        "operatingsystem_name": "Name of your OS",
                        "architecture_name": "x86_64",
                        "domain_name": "your.domain",
                        "name": "hostname",
                        "hostgroup_id": "ID of target hostgroup",

2. Zero out config on existing physical interfaces you wish to be part of the bond

PUT request, API endpoint “https://your-foreman-host/api/v2/hosts/{host_id}/interfaces/{interface id}”

json payload (basically zero out all config except identifier, managed, mac, mtu and type)

json_payload = { 
                    "interface": {
                        "subnet_id": "",
                        "subnet_name": "",
                        "subnet6_id": "",
                        "subnet6_name": "",
                        "domain_id": "",
                        "domain_name": "",
                        "managed": "true",
                        "identifier": "eno1",
                        "name": "",
                        "ip": "",
                        "ip6": "",
                        "mac": "aa:bb:cc:dd:ee:ff,
                        "mtu": "1500",
                        "fqdn": "",
                        "primary": "false",
                        "provision": "false",
                        "type": "interface",
                        "virtual": "false"

3. Create the bond

POST request, API endpoint: https://your-foreman-host/api/v2/hosts/{host_id}/interfaces

json payload

json_payload = { "interface": {
                            "subnet_id": 1,
                            "subnet_name": "my_subnet",
                            "domain_id": 1,
                            "domain_name": "my.domain",
                            "managed": "true",
                            "identifier": "bond0",
                            "name": "hostname",
                            "ip": "",
                            "mac": "aa.bb.cc.dd.ee.ff",
                            "mtu": 1500,
                            "fqdn": "hostname.my.domain",
                            "primary": "true",
                            "provision": "true",
                            "type": "bond",
                            "mode": "802.3ad",
                            "attached_devices": "eno1",
                            "bond_options": "miimon=10 lacp_rate=1",
                            "virtual": "true"

4. Mark the host for rebuild and reboot.

Hope some of this might help!