Find a value in a structured file with Ansible

522 views Asked by At

Here I have a small question about how to find a value in a structured file using Ansible. I've seen lineinfile but I'm not pretty sure that it will be helpful. If we assume that my file looks like this (in fact it's way way longer but for evident reasons I cannot post it here ^^)

################## junos.conf ##################

system {
    auto-snapshot;
    root-authentication {
        encrypted-password ## SECRET-DATA
    }
    services {
        ssh;
        netconf ssh;
        scp;
    }
    syslog {
        user * {
            any emergency;
        }
        file messages {
            any notice;
            authorization info;
        }
        file interactive-commands {
            interactive-commands any;
        }
    }
    processes {
        dhcp-service {
            traceoptions {
                file dhcp_logfile size 10m;
                level all;
                flag all;
            }
        }
    }
}
interfaces {
    irb {
        unit 0 {
            family inet {
                dhcp {
                    vendor-id Juniper-ex4300-48t;
                }
            }
        }
    }
    vme {
        unit 0 {
            family inet {
                address 172.0.0.1/24
            }
        }
    }
}
forwarding-options {
    storm-control-profiles default {
        all;
    }
}
vlans {
    default {
        vlan-id 1;
        l3-interface irb.0;
    }
}

It's a .conf file but it looks like a structured file. Imagine, I want to find a way to get the value interfaces->vme->unit 0->family inet within an Ansible playbook, how could I do ? Which parser could I use in Ansible ?

I've already read this page but I don't really know which parser to use and how to use it : https://docs.ansible.com/ansible/latest/network/user_guide/cli_parsing.html

Thanks, Max

1

There are 1 answers

1
U880D On

Regarding your question

Which parser could I use in Ansible?

If you are not able to export the config in a JSON structured format before (show config | display json), because it is in example not under your control, you may need to deal with the given structure.

It's a .conf file but it looks like a structured file.

Since the structure don't look like one of the available templates in Parsing semi-structured text with Ansible, you may need to look to an other option.

I assume you do not want to read in the whole file via file_lookup module and parse it fully within Ansible.

The Lookup Plugins seems also not to have the provided structure implemented. The INI lookup seems also not fit.

Imagine, I want to find a way to get the value interfaces->vme->unit 0->family inet within an Ansible playbook, how could I do?

If your configuration file has just that structure and it is not changing, which we don't know according

... in fact it's way way longer ...

the following approach might be working for a while:

- name: Read IP address from Junos config file
  shell:
    cmd: grep -o "address.*" /tmp/junos.conf | cut -f 2 -d " " | cut -f 1 -d "/"
  register: ip_address
  warn: false
  check_mode: false
  changed_when: false
  delegate_to: localhost
  tags: junos_conf

- name: Show IP address
  debug: 
    msg: "{{ ip_address }}"  
  tags: junos_conf

There is also the option to write an own Custom Plugin.

Thanks to

Links from comments