cloud director configuration post deployment using ansible uri

68 Views Asked by At

I am trying to automate the cloud director deployment and post configuration using ansible. Using URI module to pass the nfs storage, db and account details but it failed with "msg: 'Status code was 400 and not [200]: HTTP Error 400: BAD REQUEST'". Here is the code snippet I am using to

- name: configure cloud director post deployment
  uri:
    url: 'https://{{ vcd_ip }}:5480/api/1.0.0/systemSetup'
    method: POST
    body: '{"applianceType":"primary","storage":{ "nfs":"nfs_ip:/share"},"appliance":{"dbPassword":{{ passwd }},"ceip":"checked"},"installation":{"name":"VCD10.3","id":"1"},"sysAdmin":{"username":"administrator","password":{{ passwd }},"fullName":"xxxx","email":"[email protected]"}}'
    headers:
      Accept: 'application/*;version=35.2'
      Content-Type: application/json
      Authorization: 'Basic xcfgrtyunvdryrtpiy='
    validate_certs: false
  register: result

Below is the output

  json:
    error: '400 Bad Request: Failed to decode JSON object: Expecting value: line 1 column 99 (char 98)'
  msg: 'Status code was 400 and not [200]: HTTP Error 400: BAD REQUEST'
  redirected: false
  server: nginx
  status: 400

Have also tried with Bearer authorization with the session token I have collected using this URL https://vcd_ip:5480/api/1.0.0/sessions

1

There are 1 best solutions below

0
Vladimir Botka On

Fix the JSON format and expand the substitutions. Put it into the vars and use Folded Style. It's easier to read. For example,

    - debug:
        msg: "{{ json }}"
      vars:
        json: >-
          {"applianceType": "primary",
           "storage": {"nfs": "nfs_ip:/share"},
           "appliance": {"dbPassword": '{{ passwd }}',
                         "ceip": "checked"},
           "installation": {"name": "VCD10.3",
                            "id": "1"},
           "sysAdmin": {"username": "administrator",
                        "password": '{{ passwd }}',
                        "fullName": "xxxx",
                        "email": "[email protected]"}
          }
        passwd: my_passwd

Note: You can use the YAML format as well

      vars:
        json:
          appliance:
            ceip: checked
            dbPassword: '{{ passwd }}'
          applianceType: primary
          installation:
            id: '1'
            name: VCD10.3
          storage:
            nfs: nfs_ip:/share
          sysAdmin:
            email: [email protected]
            fullName: xxxx
            password: '{{ passwd }}'
            username: administrator
        passwd: my_passwd

gives

  msg:
    appliance:
      ceip: checked
      dbPassword: my_passwd
    applianceType: primary
    installation:
      id: '1'
      name: VCD10.3
    storage:
      nfs: nfs_ip:/share
    sysAdmin:
      email: [email protected]
      fullName: xxxx
      password: my_passwd
      username: administrator

In your case, you might want to try

- name: configure cloud director post deployment
  uri:
    url: 'https://{{ vcd_ip }}:5480/api/1.0.0/systemSetup'
    method: POST
    body: "{{ json|to_json }}"
    ...