4

I would like to ask you how it is possible to implement such a case using Ansible.

My main goal is to nit git bare repository and clone it to the same machine (/var/www). My usual steps were:

1) git init —bare (running in /git/project-name)

2) git clone /git/project-name —no-hardlinks (running in в /var/www)

When I am willing to do this case using Ansible, then I cant implement the first step -- initialization if an empty git bare repository.

Ansible git module requires that 'repo' param should be filled with repository address, but how I can define it if I am just creating one?

git: repo=?? dest=/git/project-name bare=yes

Ivar Mahhonin
  • 41
  • 1
  • 2
  • From a quick look at the documentation it doesn't seem to be possible to create a new repository without a source. Why do you want to do this, are you sure this is what you want to achieve? – Vampire Aug 29 '16 at 11:10
  • 1
    My goal is to create centrel repository to where I will push my code from different machines and from whice I will clone all other repositories. Isn't that a right approach? – Ivar Mahhonin Aug 29 '16 at 11:30
  • 4
    Ansible can execute arbitrary commands, so if you know how to create a git repository from the command line you can implement that in an ansible playbook. The `git` module can only clone repositories, not create new ones from scratch. – larsks Aug 29 '16 at 12:07
  • Other than running git with `command` module, you can copy all the files it creates, or untar them. It's just a plain branch in a file system. Not sure if it would make much sense though. – techraf Aug 29 '16 at 12:50
  • Okay. I think I will end up with using command module. I will think about if it would be possible to write my own plugin for Ansible :) Thank you! – Ivar Mahhonin Aug 29 '16 at 13:33

1 Answers1

5

Here is a sample playbook for setting up a git server with a new bare repo:

---
- hosts: git.example.org
  become: true
  tasks:
    - user:
        name: git
        shell: /usr/bin/git-shell
        home: /srv/git

    - authorized_key:
        user: git
        state: present
        key: https://github.com/myuser.keys

    - command: git init --bare /srv/git/myrepo.git
      args:
        creates: /srv/git/myrepo.git/HEAD
      become_user: git

If you've already got a git user set up on your server, then you only need to focus on the command task. Enjoy!

bonh
  • 2,863
  • 2
  • 33
  • 37
  • 2
    You don't need the entire `file:` hunk or `chdir:` arg to the `git init --bare` command; just provide the target directory. From [`git init` docs](https://git-scm.com/docs/git-init): _If you provide a directory, the command is run inside it. If this directory does not exist, it will be created._ – ssteinerX Feb 16 '18 at 16:07