indempotence best practices questions and playbook example

38 views
Skip to first unread message

Vince Skahan

unread,
Apr 16, 2015, 5:31:20 PM4/16/15
to ansible...@googlegroups.com

I'm a little confused about best practices for indempotence in Ansible.    Here's a quick playbook that does the following:
  • installs mysql-server on a minimal debian system, preseeding the questions (ugh) the package asks to set the mysql password
  • installs zoneminder
  • conditionally copies in a gzipped dumpfile of a pre-configured zoneminder setup, unzips it, restores it
  • and links the zoneminder stuff into the apache2 web configuration

Where I'm confused is the convoluted gymnastics needed to do things conditionally, only if there was something done above.   The playbook below works fine, but it sure seems like a lot of work to do to make each step indempotent.  Am I missing something in best practices or syntax ?

Note - yes I set a db password to 'root' in this example, and I'm keying off stuff stashed in /tmp existing or not.  Don't sweat that part, it's just an example.  I'm interested in better understanding best practices re: how to do the playbook structure and logic/flow.  Thanks.

 
---
 ### unfortunately, this one always comes up as changed
- name: mysql | set debconf values
  debconf: name='mysql-server-5.5' question={{ item.question}} value={{ item.value }} vtype={{ item.vtype }}
  with_items:
     - { question: 'mysql-server/root_password',       value: 'root', vtype: 'password' }
     - { question: 'mysql-server/root_password_again', value: 'root', vtype: 'password' }

- name: Install Mysql
  apt: package={{ item }} state=present
  with_items:
          - mysql-server
 
- name: install zoneminder
  apt: package={{ item }} state=present
  with_items:
     - zoneminder

### check to see if we ran before by looking for the extracted db dump 
- name: check for previous database dumpfile
  stat: path=/tmp/zm.dump
  register: dumpfile

### if the extracted file isn't there, we need to copy the gzipped file in 
- name: copy db dump into place
  copy: src=zm.dump.gz dest=/tmp
  when: dumpfile.stat.exists == False
  register: zipped_dumpfile

### if we took action in the previous step, we need to unzip what we copied in
- name: unzip db dump
  command: gunzip -f /tmp/zm.dump.gz
  when: zipped_dumpfile.changed == True
  register: dumpfile_unzipped
 
### and that also means we need to restore the db dump to put it into place
- name: restore configured database
  shell: mysql -uroot -proot zm < /tmp/zm.dump
  when: dumpfile_unzipped.changed == True
  notify: restart zoneminder

### similarly, we link zoneminder into the apache web and restart apache, only if needed 
- stat: path=/etc/apache2/sites-enabled/001-zm
  register: apache_zm_site

- name: symlink zoneminder into web
  file: src=/etc/zm/apache.conf dest=/etc/apache2/sites-enabled/001-zm state=link
  when: apache_zm_site.stat.exists == False
  notify: restart apache

Reply all
Reply to author
Forward
0 new messages