Ansible

Configure any RESTCONF client from ansible

Ansible can be used to get, set and call functions in RESTCONF. You can use the Ansible RESTCONF module or you can use the Ansible uri module to make RESTCONF calls as I’ve done here.

Setup

Downloading FreeCONF example source code

git clone https://github.com/freeconf/examples fc-examples

Setup ansible

cd ansible
python3 -m venv venv
source ./venv/bin/activate
python -m pip install -r requirements.txt

file: requirements.txt

ansible==7.0.0
ansible-core==2.15.8

Running

Inventory file

all:
  hosts:
    car:
      http_port: 8090
      ansible_host: localhost

Get Config

Command: ansible-playbook -i hosts.yml get-config.yml

File: get-config.yml

---
- name: get configure
  hosts: car
  connection: httpapi
  gather_facts: False

  tasks:
  - name: get current speed
    uri:
      url: "http://{{ ansible_host }}:{{ http_port }}/restconf/data/car:?depth=1"
    register: results

  - debug:
      var: results.json

Example Output:

****
TASK [get current speed] *******************************************************
ok: [car]

TASK [debug] *******************************************************************
ok: [car] => {
    "results.json": {
        "lastRotation": 13100,
        "miles": 20900,
        "running": false,
        "speed": 300
    }
}

PLAY RECAP *********************************************************************
car                        : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Set Config

Command: ansible-playbook -i hosts.yml set-config.yml

File: set-config.yml

---
- name: configure
  hosts: car
  connection: httpapi
  gather_facts: False

  tasks:
  - name: change speed
    uri:
      url: "http://{{ ansible_host }}:{{ http_port }}/restconf/data/car:"
      method: PATCH
      body_format: json
      body: |
        {
            "speed":10
        }        

Run RPCs

Here we run the replaceTires RPC

---
- name: rpc
  hosts: car
  connection: httpapi
  gather_facts: False

  tasks:
  - name: replace tires
    uri:
      url: "http://{{ ansible_host }}:{{ http_port }}/restconf/data/car:replaceTires"
      method: POST