Reputation: 331
I've been trying to setup some ansible roles and playbooks.
The biggest challenge for me at the moment is the conditional running of tasks. My role has been evolving - primarily to get it to work
. This is where I have got to, but it seems a bit awkward, and I was wondering if anyone could point me towards a better way
.
I know the setup vars could go in /vars/main.yml
, but any guidance from more experienced users would be great.
To put it bluntly, this has been a real slog to get ansible to install emacs-latest.
---
- name: Setup vars
set_fact:
emacs_requires_upgrade: false
emacs_installed: false
emacs_version: 0
autoconf_upgrade_required: false
- name: Check for emacs
command: which emacs
changed_when: false
failed_when: false
register: emacs_installed
- name: Check emacs version
shell: emacs --version | awk 'NR == 1' | cut -d " " -f 3
register: emacs_version
when: emacs_installed|success
- name: Emacs requires upgrade
set_fact:
emacs_requires_upgrade: true
when:
emacs_installed | failed or emacs_version | version_compare('24.5', '<')
# yum tools required to build emacs
- name: Ensure build tools are present
yum: pkg={{ item }} state=latest
with_items:
- gcc
- autoconf
- automake
- texinfo
- ncurses
- ncurses-devel
when: emacs_requires_upgrade
# need to establish if autoconf needs to be upgraded (emacs requires 2.68 i think)
- name: stat autoconf
stat: path=/usr/bin/autoconf
register: autoconf
when: emacs_requires_upgrade
- name: Check autoconf version
shell: autoconf --version | awk 'NR == 1' | cut -d " " -f 4
register: autoconf_version
when: emacs_requires_upgrade and autoconf.stat.exists
- name: Autoconf version
debug:
var: "{{ autoconf_version.stdout }}"
when: emacs_requires_upgrade and autoconf.stat.exists
- name: Autoconf requires upgrade
set_fact:
autoconf_upgrade_required: true
when:
emacs_requires_upgrade and
{{ autoconf_version.stdout }} | version_compare('2.68','<')
- name: Move old autoconf
shell: mv /usr/bin/autoconf /usr/bin/autoconf.old
when:
emacs_requires_upgrade and
autoconf.stat.exists and
autoconf_upgrade_required
- name: Check for autoconf previous download
stat: path=/usr/local/share/autoconf-2.69
register: autoconf_src
when:
emacs_requires_upgrade and
autoconf_upgrade_required
- name: Install latest autoconf from source
unarchive: src=http://ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz dest=/usr/local/share copy=no
when:
emacs_requires_upgrade and
autoconf_upgrade_required
- name: Build Autoconf 2.69
shell: "{{ item }}"
args:
chdir: "/usr/local/share/autoconf-2.69"
with_items:
- ./configure
- make
- make install
- ln -s /usr/local/share/autoconf-2.69/bin/autoconf /usr/bin/autoconf
when:
emacs_requires_upgrade and
autoconf_upgrade_required
- name: Check for emacs previous download
stat: path=/usr/local/share/emacs-24.5
register: emacs_src
when:
emacs_requires_upgrade
- name: Install latest emacs from source
unarchive: src=http://mirror.sdunix.com/gnu/emacs/emacs-24.5.tar.gz dest=/usr/local/share copy=no
when:
emacs_requires_upgrade and
not emacs_src.stat.exists
- name: Stat old emacs
stat: path=/usr/bin/emacs
register: emacs_in_bin
when:
emacs_requires_upgrade
- name: Move old emacs
command: mv /usr/bin/emacs /usr/bin/emacs.old
when:
emacs_in_bin.stat.exists
- name: run autogen.sh
shell: "{{ item }}"
args:
chdir: /usr/local/share/emacs-24.5
with_items:
- ./autogen.sh
- ./configure --with-x=no
- make
- make install
- ln -s /usr/local/share/emacs-24.5/src/emacs /usr/bin/emacs
Upvotes: 0
Views: 9696
Reputation: 19
If you have to do it this way:
Break your role out into 3 separate tasks, include them into the main.yml in your roles folder, and handle your conditional for an entire task. Example:
/path/to/role/tasks/:
main.yml
- include: check_emacs_version.yml
- include: check_auto_conf.yml
- include: install_software.yml
-- when:check_emacs_version == true
The nice thing about ansible is that is holds variable registration throughout a play and roles are basically a "play", so you can split apart your role into various tasks to clean it up. If you find yourself abusing the "when" conditionals, it's most likely time to split the role into separate tasks.
Upvotes: 1