Skip to content

Instantly share code, notes, and snippets.

@mariocesar
Last active July 16, 2021 16:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mariocesar/8e674ec40dad6b94114d2a44d9151e23 to your computer and use it in GitHub Desktop.
Save mariocesar/8e674ec40dad6b94114d2a44d9151e23 to your computer and use it in GitHub Desktop.
Ansible for the frugal

Ansible for the frugal

I got this idea about using deploy scripts in the same way developer use ansible playbooks. I know is far from ideal, but I was able to make it work for my personal use.

So, the basics: Send a directory, execute a script in it, remove the directory when done.

For the future:

  • apt utils
  • service utils
  • run on multiple machines
  • populate the env with host facts (like ansible host facts)

For example, we could have a directory with a bash script and different provision files, in the same directory.

$ ls provision/
bootstrap.sh
nginx.conf
nginx.mydomain.conf

We can run bootstrap.sh in a remote server, and send all the directory so the provision files will work the same as running them locally.

tar cpf - provision/ | ssh ubuntu@192.168.1.99 "tar xpf - -C /tmp && cd /tmp/provision && bash /tmp/provision/bootstrap.sh && rm -rf /tmp/provision"

We can also make it a single function:

function play () {
  local remote=${2}
  local script=${1}
  local directory=$(dirname ${script})

  tar cpf - ${directory}/ | ssh -t ${remote} "
    tar xpf - -C /tmp &&
    cd /tmp/${directory} &&
    bash /tmp/${script} &&
    rm -rf /tmp/${directory}
  "
}

Use like this:

$ play provision/bootstrap.sh ubuntu@10.0.0.1
* base server provisioned [Ok]
* Installing tools [Ok]
$ play application-server/bootstrap.sh ubuntu@10.0.0.1
* Installing python3 [Ok]
* Creating virtualenv [Ok]
$ play database-server/bootstrap.sh ubuntu@10.0.0.1
* Installing postgresql [Ok]
* Creating users [Ok]
* Creating databases [Ok]
@colonelpopcorn
Copy link

All I can say is that I love the simplicity of this idea. +1

@aero
Copy link

aero commented Jul 15, 2021

Good idea.
But your above play() function not works.
It should be modifed like the following.

opening { is missing
Two typo. ${dirname} -> ${directory}
"tar ... &&" in heredoc not actually works.

function play () {
  local remote=${2}
  local script=${1}
  local directory=$(dirname ${script})

  tar cpf - ${directory}/ | ssh -t ${remote} "
    tar xpf - -C /tmp &&
    cd /tmp/${directory} &&
    bash /tmp/${script} &&
    rm -rf /tmp/${directory}
  "
}

@mariocesar
Copy link
Author

@aero thank you, I made the change :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment