# Mapping Git branches to environments

**URL:** <https://forum.ansible.com/t/mapping-git-branches-to-environments/13063>\
**Category:** Ansible Project\
**Created:** [July 3, 2013, 10:45pm UTC](https://forum.ansible.com/t/mapping-git-branches-to-environments/13063 "2013-07-03T22:45:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joost\_Cassee](https://avatars.discourse-cdn.com/v4/letter/j/898d66/32.png) [@Joost\_Cassee](https://forum.ansible.com/u/Joost_Cassee)\
**Post date:** [July 3, 2013, 10:45pm UTC](https://forum.ansible.com/t/mapping-git-branches-to-environments/13063/1 "2013-07-03T22:45:31Z")

</div>

Hi,

I am looking into Ansible to define our infrastructure in, and one charming idea I got from other provisioning systems is to map Git branches to environments. For example, I could try out a change in the infrastructure in a development branch and merge back into master to apply this change to the production servers. Puppet can do this with r10k [1], and Salt has this baked in with its GitFS backend [2].

Is anyone doing something like this with Ansible?

[1] [https://github.com/adrienthebo/r10k](https://github.com/adrienthebo/r10k)  
[2] [http://docs.saltstack.com/topics/tutorials/gitfs.html](http://docs.saltstack.com/topics/tutorials/gitfs.html)

Regards,  
Joost

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [July 3, 2013, 11:01pm UTC](https://forum.ansible.com/t/mapping-git-branches-to-environments/13063/2 "2013-07-03T23:01:43Z")

</div>

Both of these tend to make my eyes bleed.

Generally, I don’t think there’s no reason for Ansible to need to because you could just execute an ansible-playbook command for each environment.

You could just check out your branch in different directories and you are good to go.

(However, this is pretty easy to do in AWX as you would just set up seperate project checkouts)

---

<div class="post-metadata">

**Author:** ![Joost\_Cassee](https://avatars.discourse-cdn.com/v4/letter/j/898d66/32.png) [@Joost\_Cassee](https://forum.ansible.com/u/Joost_Cassee)\
**Post date:** [July 4, 2013, 12:46am UTC](https://forum.ansible.com/t/mapping-git-branches-to-environments/13063/3 "2013-07-04T00:46:53Z")

</div>

> Both of these tend to make my eyes bleed.
> 
> Generally, I don’t think there’s no reason for Ansible to need to because you could just execute an ansible-playbook command for each environment.

But would not developing that playbook be easier in a separate branch than copied from one directory to another?

> You could just check out your branch in different directories and you are good to go.
> 
> (However, this is pretty easy to do in AWX as you would just set up seperate project checkouts)

Sure, I can set up some Git hooks myself too. That was my plan B. 🙂

Regards,  
Joost

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [July 4, 2013, 12:53am UTC](https://forum.ansible.com/t/mapping-git-branches-to-environments/13063/4 "2013-07-04T00:53:26Z")

</div>

Yeah I’m suggesting you would still be using a seperate branch, you’d just have it checked out in a few different places. Same repo, branched, mergeable, still…

Just not having to “git branch” from the checkout location to get to see the one you want when you have to work on it, but rather making it accessible via the current filesystem – thus Ansible need not know about it 🙂

---

<div class="post-metadata">

**Author:** ![Joost\_Cassee](https://avatars.discourse-cdn.com/v4/letter/j/898d66/32.png) [@Joost\_Cassee](https://forum.ansible.com/u/Joost_Cassee)\
**Post date:** [July 4, 2013, 1:05am UTC](https://forum.ansible.com/t/mapping-git-branches-to-environments/13063/5 "2013-07-04T01:05:46Z")

</div>

> Yeah I’m suggesting you would still be using a seperate branch, you’d just have it checked out in a few different places. Same repo, branched, mergeable, still…

Ah, check!

> Just not having to “git branch” from the checkout location to get to see the one you want when you have to work on it, but rather making it accessible via the current filesystem – thus Ansible need not know about it 🙂

Okay, I can see why you would want to keep this out of Ansible proper. If I would take the directory layout from the Best Practices docs [1], what would be the best place to integrate other branches?

[1] [http://www.ansibleworks.com/docs/bestpractices.html#directory-layout](http://www.ansibleworks.com/docs/bestpractices.html#directory-layout)

Regards,  
Joost

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [July 4, 2013, 4:18am UTC](https://forum.ansible.com/t/mapping-git-branches-to-environments/13063/6 "2013-07-04T04:18:47Z")

</div>

You’d actually branch the entire project, so it wouldn’t affect the hierarchy.

You could just check out the repo at two filesystem different points at different branches.

---

<div class="post-metadata">

**Author:** ![Dave\_Cottlehuber](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Dave\_Cottlehuber](https://forum.ansible.com/u/Dave_Cottlehuber)\
**Post date:** [July 4, 2013, 6:27am UTC](https://forum.ansible.com/t/mapping-git-branches-to-environments/13063/7 "2013-07-04T06:27:47Z")

</div>

While I like the concept of ansible getting its info via git, I think  
in general its linking something unnecessary into ansible. I'd rather  
have an HTTP accessible method where (for example) ansible retrieves  
playbooks etc, and writes facts back into e.g. into CouchDB. I can  
replicate securely between datacentres, and share inventory across  
separate secure infrastructures. I'm not sure if an HTTP arrangement  
like GET /ansible/playbooks … PUT /ansible/configs/:server would be  
acceptable for core ansible?

Anyway.

In git-contrib there's a module called git-new-workdir which allows  
sharing the same git index across multiple working dirs. It should  
come in a default install of any recent git in the last couple years.

On my OSX is set as a shell alias:

gnw='/usr/local/Cellar/git/\*/share/git-core/contrib/workdir/git-new-workdir'

usage: gnw \<repository\> \<new\_workdir\> [\<branch\>]

if your project is stored in /projects/runcible then you can have a  
separate checkout like so:

gnw /projects/runcible projects/pea-green-boat leafy-branch

Inside /projects/pea-green-boat it behaves just like a normal git  
clone, except that the index is shared from /projects/runcible.

A+  
Dave

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [July 4, 2013, 12:02pm UTC](https://forum.ansible.com/t/mapping-git-branches-to-environments/13063/8 "2013-07-04T12:02:59Z")

</div>

“I’d rather  
have an HTTP accessible method where (for example) ansible retrieves  
playbooks etc, and writes facts back into e.g. into CouchDB.”

No, Ansible pushes playbooks.

I don’t see any need for an http layer with it’s own security system here.

As for recording facts, you could definitely write your own callback system, though there is no reason for this to require another network connection.

I’m looking at building a system that keeps facts in a lookaside cache sometime in 1.3 or early 1.4.
