Upload bash script to Ubuntu machine deploymed by MAAS

Info + objective:

I’m using MAAS to deploy workstations with Ubuntu.

MAAS just deploys the machine with stock Ubuntu, and I then run a bash script I wrote to set up everything needed.

So far, I’ve ran that bash script manually on the newly deployed machines. Now, I’m trying to have MAAS run that script automatically.

 


 

What I did + error:

In the MAAS machine, I create the following file curtin file called /var/snap/maas/current/preseeds/curtin_userdata_ubuntu which contains the following:

write_files:
  bash_script:
    path: /root/script.sh
    content: |
      #!/bin/bash
      echo blabla
      ... very long bash script
    permissions: '0755'

late_commands:
  run_script: ["/bin/bash /root/script.sh"]

However, in the log, I see the following:

known-caiman cloud-init[1372]: Command: ['/bin/bash /root/script.sh']
known-caiman cloud-init[1372]: Exit code: -
known-caiman cloud-init[1372]: Reason: [Errno 2] No such file or directory: '/bin/bash /root/script.sh': '/bin/bash /root/script.sh'

 


 

Question

I’m not sure putting such a large bash script in the curtin file is a good idea. Is there a way to store the bash script on the MAAS machine, and have curtin upload it to the server, and then execute it? If not, Is it possible to fix the error I’m having?

Thanks ahead!

Hi,

you should split the command line:

late_commands:
  run_script: ['/bin/bash', '/root/script.sh']

Looking at docs I don’t see a clear alternative to storing the script in the curtin file. If you have a HTTP server around, you can store the script there and have curtin to download it using curl or wget, and then execute it.

Thanks for the response!

I actually just tested that, and got the following result:

nearby-bee cloud-init[1362]: Command: ['/bin/bash', '/root/script.sh']
nearby-bee cloud-init[1362]: Stdout: /bin/bash: /root/script.sh: No such file or directory

write_files create files at the target system, while the *_commands run in the ephemeral environment. To run a script in the target use this syntax:

late_commands:
  00-cmd: ["curtin", "in-target", "--", "/bin/bash", "/root/script.sh"]
1 Like

This worked! Thank you so much!

Sorry for nitpicking, but do you know if it’s still possible to upload a file from the MAAS server to the machines it deploys without http host, rather then write to the file?

Not that urgent as your solution made it work. Huge thanks for all the help.

you are welcome

I’m not aware of a method to upload a file to the machines other than using curtin files (either embedding the content or calling curl at the target to download it from somewhere else).

If you don’t want to run this script on every box ever deployed, you can supply the script as a base64 encoded string as the “user-data” parameter data in the API/CLI

It will be executed at the end of the build, locally on the box. This method allows the users to specify a different script per-machine.

Thanks for the response!

Are you referring to this box? If so, is it possible to supply with the CLI?

Hi Alex,

I wanted your input on similar scenario. I am deploying machines with ubuntu os on it. Also want to run a post install python script without any commands or manual intervention. Thank you for your help in advance!

If you want to run this script in every machine, you can follow the same path as the original post (write_files and late_commands). Just replace the bash interpreter with python, and check my answer about target execution.

If you need to install python modules before running your script, you can do it using late_commands also. Commands are executed in lexicographical order, so use appropriate names to ensure the modules are installed before trying to run the script.

1 Like

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

@rakhibansod, did alexsander’s suggestion solve your problem?

1 Like