如何在ansible中编写条件任务

时间:2015-11-10 18:01:53

标签: ansible

我一直试图设置一些安莎角色和剧本。

目前我面临的最大挑战是有条件地执行任务。我的角色一直在发展 - 主要是get it to work。这是我必须去的地方,但似乎有点尴尬,我想知道是否有人能指出我better way

我知道设置变量可以进入/vars/main.yml,但是来自更有经验的用户的任何指导都会很棒。

说穿了,这是一个真正的麻烦,以安装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

1 个答案:

答案 0 :(得分:0)

如果你必须这样做:

将您的角色分解为3个单独的任务,将它们包含在角色文件夹中的main.yml中,并处理整个任务的条件。例如:

/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 

关于ansible的好处在于,它在整个游戏过程中保持变量注册,角色基本上是一个游戏&#34;,因此您可以将角色分成各种任务来清理它。如果你发现自己滥用&#34;何时&#34;条件,它最有可能将角色分成单独的任务。