Score:0

Add a list to specific lines of a (YAML) text file using python

dz flag

I have an Ansible module file for NetBox with this content:

- name: "Test NetBox modules"
  connection: local
  hosts: localhost
  gather_facts: False

  tasks:
    - name: Create device within NetBox with only required information
      netbox.netbox.netbox_device:
        netbox_url: http://netbox.local
        netbox_token: thisIsMyToken
        data:
          name: xxxx
          device_type: 
          device_role: 
          site: Main
        state: present


    - name: Create device within NetBox with only required information
      netbox.netbox.netbox_device:
        netbox_url: http://netbox.local
        netbox_token: thisIsMyToken
        data:
          name: xxxx
          device_type: 
          device_role: 
          site: Main
        state: present

    - name: Create device within NetBox with only required information
      netbox.netbox.netbox_device:
        netbox_url: http://netbox.local
        netbox_token: thisIsMyToken
        data:
          name: xxxx
          device_type: 
          device_role: 
          site: Main
        state: present

It has 3 tasks for adding devices. I have a python list like this : ['switch1', 'switch2', 'switch3']

my question is how can I add this elements of list in front of name: xxxx instead of xxxx regularly using python?

David Z avatar
es flag
Hi mohammad, and welcome to Ask Ubuntu! This is a pretty broad question, actually, since there are several different pieces involved in the task and it's not clear which one you are having trouble with. Could you add something to clarify that? For example, do you have a Python script and you're asking about how to run it? Or are you asking about how to read the file in Python? Or write it? Or how to replace a string with a different string? Or how to do different replacements in different parts of the file? Or so on....
Score:0
jp flag

I wouldn't use Python for this task (too much work and reinventing the wheel) ... I would use a specialized text processing tool that supports storing data sets e.g. arrays like awk:

awk -F'[ ]' 'BEGIN {
    devices[1] = "switch1"
    devices[2] = "switch2"
    devices[3] = "switch3"
}

/name: xxxx/ {
    i++
    $NF = devices[i]
}

{
    print
}' file.yaml

which will output:

- name: "Test NetBox modules"
  connection: local
  hosts: localhost
  gather_facts: False

  tasks:
    - name: Create device within NetBox with only required information
      netbox.netbox.netbox_device:
        netbox_url: http://netbox.local
        netbox_token: thisIsMyToken
        data:
          name: switch1
          device_type: 
          device_role: 
          site: Main
        state: present


    - name: Create device within NetBox with only required information
      netbox.netbox.netbox_device:
        netbox_url: http://netbox.local
        netbox_token: thisIsMyToken
        data:
          name: switch2
          device_type: 
          device_role: 
          site: Main
        state: present

    - name: Create device within NetBox with only required information
      netbox.netbox.netbox_device:
        netbox_url: http://netbox.local
        netbox_token: thisIsMyToken
        data:
          name: switch3
          device_type: 
          device_role: 
          site: Main
        state: present

or gawk to edit the file in place:

gawk -i inplace -F'[ ]' 'BEGIN {
        devices[1] = "switch1"
        devices[2] = "switch2"
        devices[3] = "switch3"
}

/name: xxxx/ {
        i++
        $NF = devices[i]
}

{
        print
}' file.yaml

Notice: You can still call awk or gawk from within a Python script with e.g. Python's subprocess or os modules.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.