Cloud-init configuration post-boot-config script

Hi,

I am trying to run cloud-init file.

Some of the commands inside it are running fine but others are not.

Sample cloud-init template:-

#cloud-config
vyos_config_commands:
  - set system host-name '{{ x.value.name }}'
  - set system login user {{ default_vyos_username }} authentication plaintext-password '{{ default_vyos_password }}'
  - delete interfaces ethernet eth0 address
  - set interfaces ethernet eth0 address '{{ vyos_vm_mgmt_ipv4 }}/24'
  - set service ssh
write_files:
  - path: opt/vyatta/etc/config/scripts/vyos-postconfig-bootup.script
    owner: root:vyattacfg
    permissions: '0775'
    content: |
      #!/bin/vbash
      source /opt/vyatta/etc/functions/script-template
      configure
      set protocols bgp {{ as_num_data }} peer-group IPGW remote-as {{ as_num_data }}
      commit
      exit
      {% for network in networks %}
        {% if 'SDN' in networks[network]["name"] %}
           vtysh -c "conf t" -c "router bgp {{ as_num_data }}" -c "bgp listen range {{ networks[network]["ipv6"] }}/{{ networks[network]["prefix"] }} peer-group IPGW"
        {% endif %}
      {% endfor %}

Now this is how actual user-data file looked like.

#cloud-config
vyos_config_commands:
  - set system host-name 'j3norvmbgp01'
  - set system login user vyos authentication plaintext-password 'vyos'
  - delete interfaces ethernet eth0 address
  - set interfaces ethernet eth0 address '10.41.170.200/24'
  - set service ssh
write_files:
  - path: opt/vyatta/etc/config/scripts/vyos-postconfig-bootup.script
    owner: root:vyattacfg
    permissions: '0775'
    content: |
      #!/bin/vbash
      source /opt/vyatta/etc/functions/script-template
      configure
      set protocols bgp 65002 peer-group IPGW remote-as 65002
      commit
      exit
                         vtysh -c "conf t" -c "router bgp 65002" -c "bgp listen range 2001:5B0:FFFF:104a::/64 peer-group IPGW"
                         vtysh -c "conf t" -c "router bgp 65002" -c "bgp listen range 2001:5B0:FFFF:105a::/64 peer-group IPGW"

***Nothing under write_files is getting executed. It is able to setup hostname and IP address.

I have attached log file of cloud-init as well.
Please have a look and let me know.

Thanks!

cloud-init-output.txt (64.0 KB)

Not sure if this makes a difference but your “path” under write files is missing the “/” in front of the “opt”.

In the docs it is written as “/opt” vs. “opt”

thanks @keneshhagard for that!
I changed the path but still the part of code is not getting configured.

An update on this.

I have modified the cloudinit file by manually adding the values and with correct yaml, but still bgp listen range is not getting executed.

show bgp peer-group

I am not sure what wrong I am doing here!!

Is there a reason you aren’t using? Why are you trying to sea it via vtysh?
https://docs.vyos.io/en/latest/configuration/protocols/bgp.html

You are also missing “save”

commit
save <---- Config will not save if you don't have this
exit
 set protocols bgp listen range <prefix> peer-group <name> 

I am using versions 1.3

Set bgp listen range command is not included in it.

Now I am using 1.3 instead of 1.4 because 1.3 is the most stable version as of now.

And I have to use VyOS router on production, so I want a stable version.

Thanks for noticing that ‘save’ command.
But I don’t think it will help me atleast in configuring the vtysh command.

Just to rule out some obvious things here - Did you try executing the commands manually in the shell to confirm they propagate? The FRR conf’s are in /etc/frr.

Would try a few things:

  1. Manually execute after startup and restart the service
  2. Hard code into the FRR config as a last resort, test then work back to your command

Additionally - you might need to restart the service after making the changes to make sure FRR carries them over.

Yes @keneshhagard I have tried all the things you mentioned.
Command is correct and it’s getting executed correctly if entered manually.

The problem is when jinja template is getting conveyed to actual code then vtysh command moves a bit towards left side and does not align with other commands. So the other commands are getting executed except vtysh commands.
I am pretty sure that’s the issue.
What I am not sure if how to fix it.

I am running it on ansible it give me this error

2021-12-07 19:10:04,542 - util.py[WARNING]: Failed loading yaml blob. Invalid format at line 19 column 1: "while scanning a simple key
in “”, line 19, column 1:
vtysh -c “conf t” -c "router bgp …
^
could not find expected ‘:’
in “”, line 20, column 1:
vtysh -c “conf t” -c “router bgp …
^”
2021-12-07 19:10:04,543 -

And my code look like this.

#cloud-config
vyos_config_commands:

  • set system host-name ‘j3norvmbgp01’
  • set system login user vyos authentication plaintext-password ‘vyos’
  • delete interfaces ethernet eth0 address
  • set interfaces ethernet eth0 address ‘10.41.170.200/24’
  • set service ssh
    write_files:
  • path: /opt/vyatta/etc/config/scripts/vyos-postconfig-bootup.script
    owner: root:vyattacfg
    permissions: ‘0775’
    content: |
    #!/bin/vbash
    source /opt/vyatta/etc/functions/script-template
    configure
    set protocols bgp 65002 peer-group IPGW remote-as 65002
    echo “1” >> /tmp/test
    vtysh -c “conf t” -c “router bgp 65002” -c "bgp listen range 2001:5B0:FFFF:104a::/64 peer-group IPGW"
    vtysh -c “conf t” -c “router bgp 65002” -c "bgp listen range 2001:5B0:FFFF:105a::/64 peer-group IPGW"
    echo “2” >> /tmp/test
    commit
    exit

Check the bold output and the code.
If you look closely to the code the vtysh commands are left oriented compared to other commands.

Edit: After submitting this message I think the
editor here is autocorrecting the white spaces so they all look align in this message but in actual vtysh commands are a bit left oriented.

Thanks!

I figured out the issue and resolved it.

Hi @keneshhagard ,

I have fixed the issue.

In ansible module I had added ‘lstrip_blocks=true’. What it does it strips any whitelines of the left side.

So vtysh command was coming on left side.

When I removed it ,started working fine.

Thanks a lot for your effort!

Have a great day!!

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.