include_playbook / include tasks from Bitbucket?

956 views
Skip to first unread message

TOB

unread,
Mar 20, 2019, 10:25:18 AM3/20/19
to Ansible Project
Hi everyone,

short summary: I have a playbook from where I wanna start other playbooks hosted on Bitbucket. In short words: Got a server and want to clone repository on Bitbucket where are all the other playbooks which I want to run on the remote server.

---
- name: Deploy
  hosts
: all
  become
: yes


  tasks
:
# INSTALL GIT ON REMOTE MACHINE
 
- name: Install GIT
    apt
:
      pkg
:
       
- git

# CLONE GIT REPO ON REMOTE MACHINE
 
- name: Connect to Bitbucket repo
    git
:
      repo
: ssh://user...@bitbucket.org/repository/repository.git
      accept_hostkey
: yes
      version
: master
      dest
: /home/username/git/
      key_file
: /home/username/.ssh/id_rsa

# FETCH YML FROM BITBUCKET
 
- name: Fetch yml from remote
    fetch
:
      remote_src
: yes
      src
: /home/username/git/playbookfolder/playbook.yml
      dest
: /
      flat: yes

  - name: Run yml
    include_tasks: "/
home/username/git/playbookfolder/playbook.yml"



It's cloning and saving the git repository on the host machine. But now I want to run a playbook / include tasks on it. It's trying to find the playbook on the Ansible controller but it should search on the remote server I cloned the repo to. Getting this one:

PLAY [Deploy] *****************************************************
TASK
[Gathering Facts] *********************************************************
ok
: [xxx.xxx.xxx.xxx]
TASK
[Install GIT] *************************************************************
ok
: [xxx.xxx.xxx.xxx] => {"cache_update_time": 1550376354, "cache_updated": false, "changed": false}
TASK
[Connect to Bitbucket repo] ***********************************************
changed
: [xxx.xxx.xxx.xxx] => {"after": "XXX", "before": "XXX", "changed": true, "remote_url_changed": false}
TASK
[Fetch yml from remote] ***************************************************
ok
: [xxx.xxx.xxx.xxx] => {"changed": false, "checksum": "xxxxxxxxx", "dest": "/playbook.yml", "file": "/home/username/git/playbookfolder/playbook.yml", "md5sum": "xxxxx"}
TASK
[Run yml] *****************************************************************
fatal
: [xxx.xxx.xxx.xxx]: FAILED! => {"reason": "Unable to retrieve file contents\nCould not find or access '/home/username/git/playbookfolder/playbook.yml' on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote_src option"}
PLAY RECAP
*********************************************************************
xxx
.xxx.xxx.xxx            : ok=4    changed=1    unreachable=0    failed=1  




I'm pretty sure I'm using the wrong syntax, but cannot figure out which is the right one. This one here is working but it's not doing the things I wrote in those different playbooks.

[...]
# FETCH UBUNTU / UPGRADE FROM BITBUCKET
  - name: Fetch yml from remote
    fetch:
      remote_src: yes
      src: /home/username/git/playbookfolder2/playbook1.yml
      dest: /
      flat: yes
      import_playbook: playbook1.yml

# FETCH INSTALL LAMP FROM BITBUCKET
  - name: Fetch and run other one
    fetch:
      remote_src: yes
      src: /home/username/git/playbookfolder2/playbook2.yml
      dest: /
      flat: yes
      import_playbook: playbook2.yml
[...]



Results here are following:
[...]
TASK [Fetch yml from remote] ***************************************************
15:19:22
34
ok: [xxx.xxx.xxx.xxx]
35
36
TASK [Fetch and run other one] **************************************************
15:19:23
37
ok: [xxx.xxx.xxx.xxx]
38
39
PLAY RECAP *********************************************************************
15:19:23
40

[...]


Thanks in advance for any help! :)

Michael Mullay

unread,
Mar 20, 2019, 10:29:45 AM3/20/19
to ansible...@googlegroups.com, TOB

There is a git module for this purpose which may work better for you.

https://docs.ansible.com/ansible/latest/modules/git_module.html#git-module

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/882fd5b7-6bd6-4198-9b01-fea8f5ef47a1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

TOB

unread,
Mar 20, 2019, 10:36:50 AM3/20/19
to Ansible Project
Hi Michael,

thanks for your answer. I'm already using it, it's in the first code block above.


Best

Michael Mullay

unread,
Mar 20, 2019, 10:49:54 AM3/20/19
to ansible...@googlegroups.com, TOB

Sorry, yes, too early for clear thinking. ;)

I was thinking of ansible-pull. I know Brian Coca that frequents this forum had a github or galaxy entry on the subject some time ago but I can't seem to find it. But this is a good reference (or you can just do a 'man ansible-pull'):

http://manpages.ubuntu.com/manpages/trusty/man1/ansible-pull.1.html

TOB

unread,
Mar 20, 2019, 11:05:03 AM3/20/19
to Ansible Project
Good morning then! :)

I'll take a look. The weird thing is following: The remote server is cloning the files (checked it manually if they're there) but with "include_tasks" or "import_playbook" I'm not able to execute the playbooks lying around there. 

Michael Mullay

unread,
Mar 20, 2019, 8:52:33 PM3/20/19
to ansible...@googlegroups.com, TOB

Yeah I have not tried to execute a playbook like that so I don't have anything to offer there. I've done your exact scenerio though using ansible-pull to run a playbook from my BitBucket repo.

TOB

unread,
Mar 21, 2019, 3:46:44 AM3/21/19
to Ansible Project
Okay, going to take a look on it - thanks. Do you know if it's possible to use pull on Ansible AWX?

Michael Mullay

unread,
Mar 21, 2019, 10:20:35 AM3/21/19
to ansible...@googlegroups.com
Yes, AWX let you use any kind of remote repo as a data source. Although I have not used it in a couple of years.

Brian Coca

unread,
Apr 22, 2019, 10:55:03 PM4/22/19
to Ansible Project
Ansible CANNOT read the files on the remote as part of the playbook,
you need to either check them out on the controller (git +
delegate_to: localhost) or 'fetch' them via the module with the same
name.

AWX will just checkout that repo onto the AWX machine and run Ansible
'locally' on those files, either way works
--
----------
Brian Coca

P. Varsha

unread,
Apr 23, 2019, 2:11:43 AM4/23/19
to ansible...@googlegroups.com
hello,

Once check your file location.

--
Reply all
Reply to author
Forward
0 new messages