Configuring LDP
The example below creates an Ansible playbook to configure the LDP protocol. 
ocnos_ldp.j2
First create a template for the LDP configuration:
osboxes@osboxes:~/playbooks$ cat templates/ocnos_ldp.j2
router ldp
  {% for peer in ldp.peers %}
  targeted-peer ipv4 {{ peer.address }}
  exit
  {% endfor %}
  advertise-labels for only_lo to any
  exit
 
{% for interface in ldp_interfaces %}
interface {{interface.ldp_interface}}
  enable-ldp {{interface.ldp_protocol}}
  label-switching
{% endfor %}
 
sw2.yml
Next, provide the LDP configuration details in the appropriate host_vars file:
osboxes@osboxes:~/playbooks$ cat host_vars/sw2.yml
ldp:
 peers:
   - address: 1.1.1.1
   - address: 3.3.3.3
 
ldp_interfaces:
   - { ldp_interface: eth1, ldp_protocol: ipv4 }
   - { ldp_interface: eth2, ldp_protocol: ipv4 }
 
Here is the overall directory structure of the Ansible playbook and associated files:
osboxes@osboxes:~/playbooks$ tree
.
    ansible.cfg
    backup
    group_vars
          ocnos.yml
    hosts-net
    host_vars
          sw2.yml
    ldp-playbook.yml
    showldp-playbook.yml
    templates
           ocnos_ldp.j2   
ansible.cfg
The following is the content of ansible.cfg file that points to the hosts-net inventory file.
osboxes@osboxes:~/playbooks$ cat ansible.cfg
[defaults]
inventory = hosts-net
host_key_checking = False
retry_files_enabled = False
interpreter_python = auto
osboxes@osboxes:~/playbooks
host-net
The following is the content of the hosts-net inventory file.
osboxes@osboxes:~/playbooks$ cat hosts-net
[ocnos]
sw2 ansible_host=10.12.9.105
osboxes@osboxes:~/playbooks$
ocnos.yml
The following is the content of ocnos.yml in the group_vars folder.
osboxes@osboxes:~/playbooks$ cat group_vars/ocnos.yml
ansible_connection: network_cli
ansible_network_os: ipinfusion.ocnos.ocnos
ansible_become: yes
ansible_become_method: enable
ansible_ssh_user: ocnos
ansible_ssh_pass: ocnos
osboxes@osboxes:
ldp-playbook.yml
The following is the playbook to push the configuration with cli_config module using the template created earlier:
(ansible) osboxes@osboxes:~/playbooks$ cat ldp-playbook.yml
---
 
  - hosts: ocnos
    gather_facts: no
 
    tasks:
 
      - name: configure LDP config on OcNOS
        cli_config:
          config: "{{ lookup('template', 'templates/{{  ansible_network_os }}_ldp.j2') }}"
 
Running the Playbook
The following is the configuration on the OcNOS device before executing the Ansible Playbook:
OcNOS#show running-config ldp
!
!
OcNOS#
 
Execute the Ansible Playbook. Below are the logs that display:
(ansible) osboxes@osboxes:~/playbooks$ ansible-playbook ldp-playbook.yml
 
PLAY [ocnos]***************************************************************************
 
TASK [configure LDP config on OcNOS]***************************************************
changed: [sw2]
 
PLAY RECAP*****************************************************************************
sw2                        : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 
 
Next, check the configurations on the OcNOS device, which should show the LDP configurations.
OcNOS#show running-config ldp
!
router ldp
 targeted-peer ipv4 1.1.1.1
  exit-targeted-peer-mode
 targeted-peer ipv4 3.3.3.3
  exit-targeted-peer-mode
 advertise-labels for only_lo to any
!
!
interface eth2
 enable-ldp ipv4
!
interface eth1
 enable-ldp ipv4
!
OcNOS#
showldp-playbook.yml
The playbook below shows how to check the runtime status of the LDP protocol through the show ldp session command. It is assumed that the neighboring nodes are configured accordingly to get the LDP session up and running:
osboxes@osboxes:~/playbooks$ cat showldp-playbook.yml
---
 
  - hosts: ocnos
    gather_facts: no
 
    tasks:
     - name: show LDP config ocnos
       cli_command:
         command: show ldp session
       register: result
 
     - name: debug
       debug:
         msg: "{{ result.stdout_lines }}"
osboxes@osboxes:~/playbooks$
 
When you run this playbook, the example below is its output. Parsing of the show command output needs to be done to determine if the runtime status of the protocol is correct:
(ansible) osboxes@osboxes:~/playbooks$ ansible-playbook showldp-playbook.yml
 
PLAY [ocnos]**************************************************************************
 
TASK [show LDP config ocnos]***********************************************************
ok: [sw2]
 
TASK [debug]**************************************************************************
ok: [sw2] => {
    "msg": [
        "Peer IP Address           IF Name    My Role    State      KeepAlive  UpTime",
        "3.3.3.3                   eth2       Passive   OPERATIONAL   30    03:58:20",
        "1.1.1.1                   eth1       Active    OPERATIONAL   30    03:58:20"
    ]
}
 
PLAY RECAP****************************************************************************
sw2                        : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 
 
(ansible) osboxes@osboxes:~/playbooks$