1

I'm unable to run bash scripts in "runcmd:" that aren't inline.

runcmd:
    - [ bash, -c, echo "=========hello world=========" >>foo1.bar ]
    - [ bash, -c, echo "=========hello world=========" >>foo2.bar ]
    - [ bash, -c, /usr/local/bin/foo.sh ]

The first two lines are successfully run on the deployed Ubuntu instance. However, the foo.sh doesn't seem to run.

Here is /usr/local/bin/foo.sh:

#!/bin/bash
echo "=========hello world=========" >>foosh.bar

foo.sh has executable permissions for root and resides on the MAAS server.

I've looked at the following but they don't seem to sort out my issue:

TylerH
  • 20,799
  • 66
  • 75
  • 101
  • What happens if you run that same command from the command line? – larsks Feb 02 '22 at 01:15
  • @larsks If I run the command in the foo.sh on the deployed server/OS, it runs properly. – Jamaica-Jan Feb 02 '22 at 20:56
  • But if you run the *same commandline* you have in your cloud-init configuration? `bash -c /usr/local/bin/foo.sh` – larsks Feb 02 '22 at 20:59
  • Yes...It doesn't run because the file foo.sh isn't there. I was under the impression that cloud-init would take it from the "maas server node" and run it on the deployed machine...any idea how to resolve that? – Jamaica-Jan Feb 03 '22 at 14:49

1 Answers1

2

Anything you run using runcmd must already exist on the filesystem. There is no provision for automatically fetching something from a remote host.

You have several options for getting files there. Two that come to mind immediately are:

  • You could embed the script in your cloud-init configuration using the write-files directive:

    write_files:
      - path: /usr/local/bin/foo.sh
        permissions: '0755'
        content: |
          #!/bin/bash
          echo "=========hello world=========" >>foosh.bar
    
    runcmd:
      - [bash, /usr/local/bin/foo.sh]
    
  • You could fetch the script from a remote location using curl (or similar tool):

    runcmd:
      - [curl, -o, /usr/local/bin/foo.sh, http://somewhere.example.com/foo.sh]
      - [bash, /usr/local/bin/foo.sh]
    
larsks
  • 277,717
  • 41
  • 399
  • 399