How to copy a script into a container and run it?

After creating a container I have a list of tasks I would like to run.

Is the recommended way simply to copy the script into the node fs or is there a better/correct way to do it?

Here is my shell version:

sudo cp initial-node-config.sh /var/lib/lxc/$N/rootfs/usr/local/sbin/
sudo lxc-attach -n $N -- /usr/local/sbin/initial-node-config.sh $I $N

Ansible version:

  - name: Copy node init script
    ansible.builtin.copy:
      src: initial-node-config.sh
      dest: "/var/lib/lxc/{{ item.hostname }}/rootfs/usr/local/sbin/initial-node-config.sh"
      owner: root
      group: root
      mode: '0700'
    with_items: "{{ nodes }}"
    notify: Execute node init script

  handlers:
  - name: Execute node init script
    community.general.lxc_container:
      name: "{{ item.hostname }}"
      container_command: "/usr/local/sbin/initial-node-config.sh {{ item.ip }} {{ item.hostname }}"
    with_items: "{{ nodes }}"

@chlor
LXD supports cloud-init

config:
  cloud-init.vendor-data: |
  #cloud-config
    packages:
      - abc
      - xyz
    run_cmd:
      - [echo, hello world]
...

if you want to get into Canonicals Application orchestrator / operator tool… you point it at LXD

lxd init <when setting up you must disable IPV6>

then

sudo snap install juju --classic
juju bootstrap localhost <follow all defaults>

once you have juju installed you can start doing deployments of entire containers and “charms” … and I stick mostly to bash based charms but they have examples of more advanced things in their charmhub community

juju deploy ubuntu < this will deploy a instance of ubuntu to the LXD host configured >

I usually do a watch -c juju status --color to watch the progress of the instantiation

from there if you just had a script you want to copy and run you can juju scp <machine #>