Search code examples
ansiblevagrant

Run playbook from git repo cloned during playbook execution


I cannot seem to figure this out from the ansible documentation.

I have a playbook X that I want to reuse in various contexts. Like any good software engineer, I put it in a repo in source control (git). So I want my other playbooks to be able to grab and include it, how do I accomplish that? I could include the repo with X as a subtree, but that's not ideal.

Let's say I have a git repo A that has an ansible playbook X. I also have a git repo B that has an ansible playbook Y. What I want is to during the execution of X, clone B and then run playbook Y. This seems like the kind of thing that should be easy to google, and the fact that it isn't makes me wonder if I'm going about this all wrong.

Here's what I've tried in playbook X:

- name: Clone B
      git:
        repo: 'http://{{ git_user }}:{{ git_pass }}@somehost/B.git'
        dest: /tmp/B

- name: Run Y
      include_tasks: /tmp/B/Y.yml
      remote_src: yes

Even though I have remote_src set to yes it keeps telling me it can't find /tmp/B/Y.yml on the Ansible Controller, so it seems to be looking on my local box rather than the remote. Repo B is correctly cloned to /tmp on the remote (confirmed via ssh).


Solution

  • This can get accomplished through a combination of the git, fetch, and include_tasks modules:

    - name: Clone B on the remote
      git:
        repo: 'http://{{ git_user }}:{{ git_pass }}@somehost/B.git'
        dest: /tmp/B
    
    # This copies the specified file from the remote to the current dir
    - name: Fetch yml from remote
      fetch:
        src: /tmp/B/Y.yml
        dest: ./
        flat: yes
    
    - name: Run Y
      include_tasks: Y.yml
    

    Note that Y.yml must be a plain list of tasks. Since I also want to be able to run it both standalone and included in the project in repo A I put a playbook in it's repo that just includes and runs it.

    Also in reference to the misleading error message with my first approach (see the comments on the question), it looks like they've already patched it.