# Onetemplate edit via script

**URL:** https://forum.opennebula.io/t/onetemplate-edit-via-script/602
**Category:** Product Support
**Created:** [April 14, 2015, 3:45pm UTC](https://forum.opennebula.io/t/onetemplate-edit-via-script/602 "2015-04-14T15:45:46Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![timm](https://avatars.discourse-cdn.com/v4/letter/t/8e8cbc/32.png) [@timm](https://forum.opennebula.io/u/timm)
#### Post date: [April 14, 2015, 3:45pm UTC](https://forum.opennebula.io/t/onetemplate-edit-via-script/602/1 "2015-04-14T15:45:46Z")

</div>

I am using OpenNebula 4.8 and have a lot of templates that are the same  
except for the image that they launch and the IP address on which they run.  
Obviously I can do a onetemplate clone and then use onetemplate update with vi as  
my editor to make the changes by hand. But I am trying to find a way to script it.

In OpenNebula 3.2 you could "onetemplate show \> /tmp/template"  
and then use sed to make the changes. That doesn’t quite work in open nebula 4.8  
because you have to add some stuff from the headers back into the template  
(the NAME) to make it a legal template again.

Is there a way from the command line to set the EDITOR variable to some script  
with arguments and then use onetemplate update to pipe to that script?

---

<div class="post-metadata">

### Author: ![alfeijoo](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.opennebula.io/alfeijoo/32/8586_2.png) [@alfeijoo](https://forum.opennebula.io/u/alfeijoo)
#### Post date: [April 15, 2015, 1:51pm UTC](https://forum.opennebula.io/t/onetemplate-edit-via-script/602/2 "2015-04-15T13:51:37Z")

</div>

Hi.

My Little hack to do that is change $EDITOR var and set to some command.

for example.

my template 404

onetemplate show 404

```
CONTEXT=[
  NETWORK="YES",
  SSH_PUBLIC_KEY="$USER[SSH_PUBLIC_KEY]" ]
CPU="1"
DISK=[
  IMAGE="install raw",
  IMAGE_UNAME="oneadmin" ]
DISK=[
  IMAGE="UBUNTU-SERVER-CD",
  IMAGE_UNAME="oneadmin" ]
GRAPHICS=[
  KEYMAP="es",
  LISTEN="0.0.0.0",
  TYPE="VNC" ]
HYPERVISOR="kvm"
MEMORY="1024"
NIC=[
  NETWORK="Default-Private",
  NETWORK_UNAME="sistemas" ]
OS=[
  ARCH="x86_64",
  BOOT="cdrom" ]
SUNSTONE_CAPACITY_SELECT="YES"
SUNSTONE_NETWORK_SELECT="YES"

```

Now i want to change the OS ARCH (in this case use the random text “CHANGETEXT”)

To do that i define the EDITOR and set the SED comand. Then i Execute the update.

The comand is that.

[user@opennebula ~]$ **export EDITOR=“sed -i ‘s/x86\_64/CHANGETEXT/’”; onetemplate update 404**

Now, my template 404 will be:

```
CONTEXT=[
  NETWORK="YES",
  SSH_PUBLIC_KEY="$USER[SSH_PUBLIC_KEY]" ]
CPU="1"
DISK=[
  IMAGE="install raw",
  IMAGE_UNAME="oneadmin" ]
DISK=[
  IMAGE="UBUNTU-SERVER-CD",
  IMAGE_UNAME="oneadmin" ]
GRAPHICS=[
  KEYMAP="es",
  LISTEN="0.0.0.0",
  TYPE="VNC" ]
HYPERVISOR="kvm"
MEMORY="1024"
NIC=[
  NETWORK="Default-Private",
  NETWORK_UNAME="sistemas" ]
OS=[
  ARCH="CHANGETEXT",
  BOOT="cdrom" ]
SUNSTONE_CAPACITY_SELECT="YES"
SUNSTONE_NETWORK_SELECT="YES"
VCPU="1"

```

Be careful, try before in random template… sed command is not your friend the most of cases hehe.

---

<div class="post-metadata">

### Author: ![atodorov\_storpool](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.opennebula.io/atodorov_storpool/32/5327_2.png) [@atodorov\_storpool](https://forum.opennebula.io/u/atodorov_storpool)
#### Post date: [April 15, 2015, 3:40pm UTC](https://forum.opennebula.io/t/onetemplate-edit-via-script/602/3 "2015-04-15T15:40:12Z")

</div>

I am using ‘onetemplate update --append filename’. It is for appending attributes, but if attribute exists it will replace it.  
Here is example:

> $ onetemplate show 1  
> TEMPLATE 1 INFORMATION  
> ID : 1  
> NAME : CentOS 7 - KVM system-ssh  
> USER : oneadmin  
> GROUP : oneadmin  
> REGISTER TIME : 04/14 11:03:24

> PERMISSIONS  
> OWNER : um-  
> GROUP : —  
> OTHER : —

> TEMPLATE CONTENTS  
> CONTEXT=[  
> NETWORK=“YES”,  
> SSH\_PUBLIC\_KEY=“$USER[SSH\_PUBLIC\_KEY]” ]  
> CPU=“1”  
> DISK=[  
> IMAGE=“CentOS-7”,  
> IMAGE\_UNAME=“oneadmin” ]  
> FROM\_APP=“53e7bf928fb81d6a69000002”  
> FROM\_APP\_NAME=“CentOS 7 - KVM”  
> GRAPHICS=[  
> LISTEN=“0.0.0.0”,  
> TYPE=“vnc” ]  
> LOGO=“images/logos/centos.png”  
> MEMORY=“768”  
> NIC=[  
> NETWORK=“vmnet1”,  
> NETWORK\_UNAME=“oneadmin” ]  
> OS=[  
> ARCH=“x86\_64” ]  
> SUNSTONE\_CAPACITY\_SELECT=“YES”  
> SUNSTONE\_NETWORK\_SELECT=“YES”

To change OS/ARCH attribute to “i386” Create update.txt file and update the template:

> $ echo ‘OS=[ARCH=“i386”]’ \> update.txt  
> $ onetemplate update --append 1 update.txt

Now you have:

> $ onetemplate show 1  
> TEMPLATE 1 INFORMATION  
> ID : 1  
> NAME : CentOS 7 - KVM system-ssh  
> USER : oneadmin  
> GROUP : oneadmin  
> REGISTER TIME : 04/14 11:03:24

> PERMISSIONS  
> OWNER : um-  
> GROUP : —  
> OTHER : —

> TEMPLATE CONTENTS  
> CONTEXT=[  
> NETWORK=“YES”,  
> SSH\_PUBLIC\_KEY=“$USER[SSH\_PUBLIC\_KEY]” ]  
> CPU=“1”  
> DISK=[  
> IMAGE=“CentOS-7”,  
> IMAGE\_UNAME=“oneadmin” ]  
> FROM\_APP=“53e7bf928fb81d6a69000002”  
> FROM\_APP\_NAME=“CentOS 7 - KVM”  
> GRAPHICS=[  
> LISTEN=“0.0.0.0”,  
> TYPE=“vnc” ]  
> LOGO=“images/logos/centos.png”  
> MEMORY=“768”  
> NIC=[  
> NETWORK=“vmnet1”,  
> NETWORK\_UNAME=“oneadmin” ]  
> OS=[  
> ARCH=“i386” ]  
> SUNSTONE\_CAPACITY\_SELECT=“YES”  
> SUNSTONE\_NETWORK\_SELECT=“YES”

BR,  
Anton

---

<div class="post-metadata">

### Author: ![alfeijoo](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.opennebula.io/alfeijoo/32/8586_2.png) [@alfeijoo](https://forum.opennebula.io/u/alfeijoo)
#### Post date: [April 15, 2015, 4:07pm UTC](https://forum.opennebula.io/t/onetemplate-edit-via-script/602/4 "2015-04-15T16:07:45Z")

</div>

WoW @atodorov_storpool fantastic 😃 better like mine as far.

---

<div class="post-metadata">

### Author: ![timm](https://avatars.discourse-cdn.com/v4/letter/t/8e8cbc/32.png) [@timm](https://forum.opennebula.io/u/timm)
#### Post date: [April 17, 2015, 7:41pm UTC](https://forum.opennebula.io/t/onetemplate-edit-via-script/602/5 "2015-04-17T19:41:08Z")

</div>

Thanks for the ideas. I am aware of the --append option, I have tried it and it works but I have not used it because I then don’t have any persistent record of how I started each virtual machine… it is easier to have a fully formed template for each VM that other staff can then use to help relaunch it later if necessary.

It is good to know the sed trick works… I was hoping for something like that.  
Thank you for all your responses.

Steve Timm
