Hello,
I’m trying to install Redis from source using Ansible, and here is the resulting playbook:
vars:
redis_version: 2.6.16
tasks:
-
name: Download Redis
shell: creates=/tmp/redis-$redis_version wget -O - http://download.redis.io/releases/redis-$redis_version.tar.gz | tar -xz -C /tmp
-
name: Install Redis
command: creates=/usr/local/bin/redis-server chdir=/tmp/redis-$redis_version make install
sudo: yes
-
name: Create user redis
user: name=redis system=yes home=/var/lib/redis shell=/bin/false
sudo: yes
-
name: Configure Upstart for Redis
copy: src=files/upstart_redis.conf dest=/etc/init/redis.conf
sudo: yes
notify: Restart Redis
handlers:
- name: Restart Redis
service: name=redis state=restarted
sudo: yes
upstart_redis.conf content is:
start on runlevel [2345]
stop on runlevel [!2345]
console log
respawn
setuid redis
exec /usr/local/bin/redis-server --bind 127.0.0.1 --dir /var/lib/redis
Any critics? Does this looks good/bad/improvable to you?
Cheers,
Nicolas
If you’re going to set sudo: yes on every task, set it on each play instead, and this will keep things shorter.
You are still using old style variables, and this is terrible, because it repeats this for other users. {{ redis_version }}
The wget should use the “get_url” module instead, and you can use “register” and “when: othertask.changed” to decide when to extract the tarball.
Don’t just configure the service in the handler, as if Redis is down and you rerun the script it won’t start it.
Stick a “state=started enabled=yes” in your task section also.
Your make install step since has the directory as a “creates” won’t work for upgrades. You’re probably ok with this, but making an OS package for the Redis version you are deploying may be a huge improvement.
Le 27/09/2013 16:39, Michael DeHaan a �crit :
The wget should use the "get_url" module instead, and you can use
"register" and "when: othertask.changed" to decide when to extract the
tarball.
Hi Michael,
could you be more specific of give an example about this
register/changed behaviour ? I also have unnecessary untar in my
playbooks and I'm looking for a way to do it clean, but I don't find
information easily in the documentation.
(sorry, this is off the topic "install redis")
Thanks.
ps : my redis installation looks like this :
- name: Install redis
apt: pkg=redis-server state=present
- name: Start redis
service: name=redis-server state=running enabled=yes
-- Jean-Philippe Caruana
Something like this:
-
get_url: url=http://example.com/tarball.tar.gz dest=/the/tarball.tar.gz
register: get_url_result
-
shell: tar -xvf /the/tarball.tar.gz
when: register.changed
-
shell: make install chdir=/whatever/directory
when: register.changed
This will only download the tarball when it is not already present (saving upstream bandwidth – always locally cache or have a copy of content and don’t have a hundred nodes hit the same upstream server! That’s not fair to the upstream and it’s also slow), but will only extract it when it has needed to be downloaded.
This will make your playbooks both faster and more repeatable.
Normally I do a -get_url in local action and then copy to all machines (if
its from external source, like redis site), this would clobber my bandwidth
only and not theirs.
but ... hmm ... -get_url: url=torrent:....
might be nice feature for when doing mass installs (might require special
deps to be added though)
Le 27/09/2013 17:33, Michael DeHaan a �crit :
Something like this:
- get_url: url=http://example.com/tarball.tar.gz dest=/the/tarball.tar.gz
register: get_url_result
- shell: tar -xvf /the/tarball.tar.gz
when: register.changed
- shell: make install chdir=/whatever/directory
when: register.changed
This will only download the tarball when it is not already present
(saving upstream bandwidth -- always locally cache or have a copy of
content and don't have a hundred nodes hit the same upstream server!
That's not fair to the upstream and it's also slow), but will only
extract it when it has needed to be downloaded.
This will make your playbooks both faster and more repeatable.
I think you meant
when: get_url_result.changed
Otherwise it fails :
TASK: [Extract Java archive]
Thank you Michael for your critical review of my playbook. My comments hereunder.
debian based distros all will default to starting a service when it is
installed., so enabled= is not always needed on install as it is the
default state, but it is prudent to add in case someone manually disables
it.
re: old style variables
It’s not strictly deprecated in that it doesn’t have a decided date yet, it’s just heavily discouraged.
It will likely generate a deprecation warning in future releases though as it’s valid in bash it may not.