3

I'm using the following ansible playbook to deploy my applications and I would like to add a tagging role so I can automatically add/update a tag to mark the current commit as the one that has been deployed.

Attempt

My current attempt is as follow, based on How can I move a tag on a git branch to a different commit?:

---
- name: Removes the tag in local repository.
  shell: git tag -d {{git_deploy_tag}}
  tags: [tagging]

- name: Removes the tag in remote repository.
  shell: git push origin :refs/tags/{{git_deploy_tag}}
  tags: [tagging]

- name: Adds the tag to different commit (HEAD).
  shell: git tag {{git_deploy_tag}} HEAD
  tags: [tagging]

- name: Pushes the changes to the remote repository.
  shell: git push origin {{git_branch}} --tags
  tags: [tagging]

Problem

This role is run on the remote host that doesn't have access to the git repository, and I intend to keep it so. I was unable to run the role on my local machine following Run command on the Ansible host.

Question

How do I run the tagging role locally (other roles should run on the remote). Fabric script have a local() method

Community
  • 1
  • 1
Édouard Lopez
  • 40,270
  • 28
  • 126
  • 178
  • Does the answer given work for you? If so, please accept it. – tedder42 Oct 25 '15 at 17:48
  • @tedder42 nope, I was not able to make it work I'll update when I get time to work again on this task – Édouard Lopez Oct 26 '15 at 16:31
  • I think it's important to release tagged software, but my current thinking (and setup) is that its better to tag a release after successful testing in Staging (outside Ansible), and make Ansible refuse to release untagged software (look at git describe for that) – Danimal Jul 29 '16 at 17:04

2 Answers2

3

Add your localhost to the ansible inventory and split your playbook in multiple plays, what you want to be run locally first and what you want to run remotely.

What follows ins an example only, but might work as a base for you. Also be aware ansible runs tasks in parallel, there is an option to chose how many parallel tasks to run, you need executed one task at time.

---
- hosts: local

tasks:
  - name: Removes the tag in local repository.
    shell: git tag -d {{git_deploy_tag}}
    tags: [tagging]

- hosts: remote

tasks:
  - name: Removes the tag in remote repository.
    shell: git push origin :refs/tags/{{git_deploy_tag}}
    tags: [tagging]
E. Celis
  • 717
  • 7
  • 17
1

The following works for me for a git task in Ansible 1.9 / 2.0

- name: Checkout a single file from project directory (into a tar archive)
  connection: local
  local_action:
    command git archive
        --remote={{ local_git_repo }}
        HEAD
        knownGoodSet.cfg
        -o /tmp/knownGoodSet.tar
  sudo: no

You will have a different git command of course, but I believe you need all of the following to get it working:

  • connection: local
  • local_action: command ... instead of shell: ...
  • sudo: no
Danimal
  • 1,208
  • 10
  • 21