Skip to content
This repository has been archived by the owner on Feb 13, 2023. It is now read-only.

Add ability to provide custom shell scripts to run after provisioning #174

Closed
geerlingguy opened this issue Jul 14, 2015 · 14 comments
Closed

Comments

@geerlingguy
Copy link
Owner

It would be great if a user could add a custom Ansible tasks file (or maybe even extra roles, and we add the roles directory to .gitignore), that would be run if present, ignored if not present.

Maybe call it extra_tasks.yml and have it included from the project root directory (if present). This isn't supported OOTB by Ansible, but this might be a viable approach (default to an empty file if the extra tasks file isn't present): http://stackoverflow.com/a/29583251/100134

@geerlingguy
Copy link
Owner Author

This will need a documentation page, since usage will not be 100% intuitive/discoverable.

@geerlingguy
Copy link
Owner Author

I did a good deal of testing for this on Ansible 2.0.0 (still not the stable release yet), and it looks like the following code should do the trick, since 2.x includes files during playbook runs, and doesn't precompile everything:

    # Include custom tasks file if it's present.
    - name: Check if custom_tasks.yml is present.
      stat: path=provisioning/tasks/custom-tasks.yml
      register: custom_tasks_file
      connection: local
      sudo: no

    - include: tasks/custom-tasks.yml
      when: custom_tasks_file.stat.exists

This would require the user place the custom_tasks.yml file into provisioning/tasks. Unfortunately, this kind of setup would also require the user to always run vagrant * commands from the root directory if using a custom-tasks.yml file. This is due to the difference in relative pathing between the stat module and the include module.

I may be able to use a better strategy, like using {{ ansible_playbook_location }} or something like that for a better relative path. It's a little wonky how this works, and ideally, I might want to put the file in the root next to config.yml, drupal.make.yml, etc.

@geerlingguy
Copy link
Owner Author

Postponed, because I can't see a way to do this until Ansible 2.x is publicly available. This will make a nice blog post though!

@geerlingguy
Copy link
Owner Author

Hmm... another thought I just had—I can include a user-provided shell script in the same way, but since it's not an Ansible include, it will work with any old version of Ansible, just via when... So I think I'll do that for now, then maybe also add the custom Ansible tasks list as well once 2.0 is stable/out for a while.

@oxyc
Copy link
Collaborator

oxyc commented Oct 15, 2015

Once you upgrade to 2.0, it would be nice if we removed the #{dir} prefix from the config.yml as well, and pass Dir.pwd on to ansible for including the file.

That would make it possible to use this repo as a git submodule or otherwise an unmodified tree together with 7e87005

@geerlingguy geerlingguy changed the title Add ability for user to provide custom tasks to run at end of provisioning Add ability for user to provide custom shell script to run at end of provisioning Dec 11, 2015
@geerlingguy
Copy link
Owner Author

Getting extremely close to the finish line here; instead of an ansible playbook, I'm just allowing users to add a list of shell scripts to run after provisioning is complete. I'm already going to use this for a few of my instances where I need to copy Solr search core configuration in place and restart solr as part of setup!

@geerlingguy geerlingguy changed the title Add ability for user to provide custom shell script to run at end of provisioning Add ability to provide custom shell scripts to run after provisioning Dec 11, 2015
@geerlingguy
Copy link
Owner Author

Woohoo! See the docs for information on how to provide scripts to run post-provision. This is working great, and allows zero, one, or as many shell scripts you want!

@oxyc
Copy link
Collaborator

oxyc commented Dec 13, 2015

Woo awesome 👍

@fubarhouse
Copy link

Looks good!

Quick question though (I haven't looked at the code yet), do these scripts execute directly on the virtual machine itself, or do they execute on the host to allow the commands to run on the host to affect the virtual machine indirectly?

geerlingguy added a commit that referenced this issue Dec 18, 2015
@geerlingguy
Copy link
Owner Author

@fubarhouse - I added in the example scripts since they were missing—but they execute within the VM. You reference shell scripts present on your host, then those scripts will be run directly on the VM after all the other provisioning steps.

@fubarhouse
Copy link

Excellent, thanks again!

@cdmo
Copy link
Contributor

cdmo commented Jun 30, 2016

@geerlingguy is there a reason why scripts/ is on .gitignore? I'm planning on removing it for my own custom fork of DrupalVM, but just wanted to make sure I didn't miss the point...

@geerlingguy
Copy link
Owner Author

@cdmo - Mostly so scripts that people would add wouldn't be tracked inside Drupal VM's repo directly—for upstream projects, you probably want to remove that from gitignore (same with a config.yml and Vagrantfile.local, if you're sharing the VM with a team).

@subhojit777
Copy link
Contributor

The custom script documentation has been moved to http://docs.drupalvm.com/en/latest/extending/scripts/

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants