How to use Ansible to deploy with git

・0 min read

Basic Ansible example on deploying with git.

vars.yml

---
# Update this to your own settings
project_name: test
project_root: /var/www/test
project_repo: [email protected]:miguelmota/test-repo.git

handlers.yml:

---
-   name: restart web server
    action: command echo "do something"
    sudo_user: root

deploy.yml:

---
-   hosts: servers
    vars_files:
        - vars.yml
    gather_facts: false
    sudo: true
    sudo_user: root
    user: root

    tasks:
        - name: Pull sources from the repository.
          git: repo={{project_repo}} dest={{project_root}} version={{branch}}
          notify:
            - restart web server

    handlers:
      - include : handlers.yml

hosts:

# Replace 0.0.0.0 with your server ip

[remote:children]
production
staging

[servers:children]
production
staging
local

[production]
0.0.0.0 nickname=production vm=0 branch=stable

[staging]
0.0.0.0 nickname=staging vm=0 branch=staging

[local]
localhost nickname=local vm=0 branch=development

ansible:

#!/bin/bash

# Run with:
# ./ansible --limit staging deploy.yml

/usr/bin/env ansible-playbook -i hosts "$@"

On github at miguelmota/ansible-example.

Subscribe

Receive updates on new posts.