SOLVED Start a jail via API

ig12

Cadet
Joined
Apr 25, 2020
Messages
4
Hi all

I started using the API v2 to create an ansible playbook to update a Jail (with my network setup out of strange reasons that requires some reconfiguration before and after the update).

Now I have issues starting up the Jail again after the update, maybe it is just me ...
If I do:
Code:
vars:
    freenas_apitoken: "{{ api_token }}"
    freenas_host: "{{ ansible_host }}"
    freenas_api_url: "{{ freenas_url + '/api/v2.0' }}"
    jail: "scarif"
    target_jail:
      jail: "{{ jail }}"


  - name: iocage | Start Jail
    uri:
        body: "{{ target_jail }}"
        body_format: json
        method: POST
        headers:
          Authorization: 'Bearer {{ freenas_apitoken }}'
        return_content: true
        status_code: 200
        url: "{{ freenas_api_url + '/jail/start' }}"
        validate_certs: no
    delegate_to: localhost


The API call is good, but the Jail is not started.
According to the swagger documentation although the body should just be a string.
But if I just pass a string I receive an error:

Code:
fatal: [10.0.0.1]: FAILED! => {
    "changed": false,
    "connection": "close",
    "content": "{\"message\": \"Expecting value: line 1 column 1 (char 0)\"}",
    "content_length": "56",
    "content_type": "text/plain; charset=utf-8",
    "date": "Wed, 17 Nov 2021 21:43:12 GMT",
    "elapsed": 0,
    "invocation": {
        "module_args": {
            "attributes": null,
            "backup": null,
            "body": "scarif",
            "body_format": "json",
            "client_cert": null,
            "client_key": null,
            "content": null,
            "creates": null,
            "delimiter": null,
            "dest": null,
            "directory_mode": null,
            "follow": false,
            "follow_redirects": "safe",
            "force": false,
            "force_basic_auth": false,
            "group": null,
            "headers": {
                "Authorization": "Bearer xxx",
                "Content-Type": "application/json"
            },
            "http_agent": "ansible-httpget",
            "method": "POST",
            "mode": null,
            "owner": null,
            "regexp": null,
            "remote_src": null,
            "removes": null,
            "return_content": true,
            "selevel": null,
            "serole": null,
            "setype": null,
            "seuser": null,
            "src": null,
            "status_code": [
                "200"
            ],
            "timeout": 30,
            "unix_socket": null,
            "unsafe_writes": null,
            "url": "https://10.0.0.1/api/v2.0/jail/start",
            "url_password": null,
            "url_username": null,
            "use_proxy": true,
            "validate_certs": false
        }
    },
    "json": {
        "message": "Expecting value: line 1 column 1 (char 0)"
    },
    "msg": "Status code was 400 and not [200]: HTTP Error 400: Bad Request",
    "redirected": false,
    "server": "nginx",
    "status": 400,
    "url": "https://10.0.0.1/api/v2.0/jail/start"
}


Can it be that this API is not working properly?
As well odd is that the stop structure is like I did it in the first try.

Thanks for your help, and sorry if it is just me not catching my own fault.

BR sebi
 

Samuel Tai

Never underestimate your own stupidity
Moderator
Joined
Apr 24, 2020
Messages
5,398
Shouldn't it be Bearer 1-{{ freenas_apitoken }}?
 

ig12

Cadet
Joined
Apr 25, 2020
Messages
4
The 1- is inside the variable.
and sorry for no posting the whole script, will do that at the end of this post. But I figured a way around my issue by testing various body formats and comparing it to the curl that the swagger documentation produced:

curl:
Code:
curl -X POST "http://10.11.1.21/api/v2.0/jail/start" \
-H  "accept: */*" \
-H "Authorization: Bearer 1-xxx" \
-H  "Content-Type: application/json" -d "\"scarif\""


The ansible task to match that is:

Code:
- name: iocage | Start Jail
    uri:
        body: "\"{{ jail }}\""
        body_format: json
        method: POST
        headers:
          Authorization: 'Bearer {{ freenas_apitoken }}'
        return_content: true
        status_code: 200
        url: "{{ freenas_api_url + '/jail/start' }}"
        validate_certs: no
    delegate_to: localhost


The formatting of the body was not right and due to that it was not accepted by TrueNas.
Still think it is strange that start and stop of a Jail have a different format. but at least it both works :smile:

Thanks for the support anyway

and here the playbook in case somebody want to to something similar:

Code:
- hosts: host
  become: true
  vars_files: ./vault
  vars:
    freenas_url: 'https://10.0.0.21'
    freenas_user: root
    freenas_apitoken: "{{ api_token }}"
    freenas_host: "{{ ansible_host }}"
    freenas_api_url: "{{ freenas_url + '/api/v2.0' }}"
    freenas_global_config:
      ipv4gateway: "10.0.0.1"
      nameserver1: "10.0.0.1"
    freenas_global_config_default:
      ipv4gateway: "10.0.0.2"
      nameserver1: "10.0.0.2"
    jail: "scarif"
    target_jail:
      jail: "{{ jail }}"


  tasks:
  - name: network | Updating Network Global Configuration -> DNS and default Interface to 10.0.0.1
    uri:
        body: "{{ freenas_global_config }}"
        body_format: json
        method: PUT
        headers:
          Authorization: 'Bearer {{ freenas_apitoken }}'
        return_content: true
        status_code: 200
        url: "{{ freenas_api_url + '/network/configuration' }}"
        validate_certs: no
    delegate_to: localhost
  - name: iocage | Stop Jail
    uri:
        body: "{{ target_jail }}"
        body_format: json
        method: POST
        headers:
          Authorization: 'Bearer {{ freenas_apitoken }}'
        return_content: true
        status_code: 200
        url: "{{ freenas_api_url + '/jail/stop' }}"
        validate_certs: no
    delegate_to: localhost
  - name: iocage | reconfigure for update
    shell: "{{ item }}"
    with_items:
        - "iocage snapshot -n plex_backup_{{ ansible_date_time.date}}_{{ ansible_date_time.time}} {{ jail }}"
        - "iocage set resolver='nameserver 10.0.0.1' {{ jail }}"
        - "iocage set nat=1 {{ jail }}"
  - name: register freebsd version and patch
    shell: "freebsd-version"
    register: freebsdversion
  - name: iocage | update && upgrade
    shell: "{{ item }}"
    with_items:
        - "iocage upgrade -r {{ freebsdversion.stdout }} {{ jail }}"
        - "iocage update {{ jail }}"
  - name: iocage | reconfigure for run
    shell: "{{ item }}"
    with_items:
        - "iocage set nat=0 {{ jail }}"
        - "iocage set resolver='nameserver 10.0.0.2' {{ jail }}"
        - "iocage set ip4_addr='vnet0|10.0.0.22/24' {{ jail }}"
        - "iocage set defaultrouter='10.0.0.1' {{ jail }}"
  - name: iocage | Start Jail
    uri:
        body: "\"{{ jail }}\""
        body_format: json
        method: POST
        headers:
          Authorization: 'Bearer {{ freenas_apitoken }}'
        return_content: true
        status_code: 200
        url: "{{ freenas_api_url + '/jail/start' }}"
        validate_certs: no
    delegate_to: localhost
  - name: network | Updating Network Global Configuration -> DNS and default Interface to 10.0.0.2
    uri:
        body: "{{ freenas_global_config_default }}"
        body_format: json
        method: PUT
        headers:
          Authorization: 'Bearer {{ freenas_apitoken }}'
        return_content: true
        status_code: 200
        url: "{{ freenas_api_url + '/network/configuration' }}"
        validate_certs: no
    delegate_to: localhost
 
Top