Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to conditionally execute group of tasks on ansible?

Tags:

ansible

I want to conditionally execute a set of tasks. Is there any syntax available that would let me execute a group of tasks, where the condition is evaluated once per whole group (like in a if statement in programming languages)?

Take a look at the code snippets below. I know the difference is small, but the first code better expresses my intention without polluting the namespace with additional variables (user_home_result2).

Pseudocode of what I want to do:

- name: Capturing user's home directory
  shell: "getent passwd {{ user }} | awk -F: '{ print $6 }'"
  register: user_home_result

- set_fact: user_home={{ user_home_result.stdout }}

- when:  user_home != ''  

  - name: Setting up user {{ user }}
    user: >
      generate_ssh_key=yes
      name="{{ user }}"

  - name: Capturing user's home directory
    shell: "getent passwd {{ user }} | awk -F: '{ print $6 }'"
    register: user_home_result

  - set_fact: user_home={{ user_home_result.stdout }}

Walkaround:

- name: Capturing user's home directory
  shell: "getent passwd {{ user }} | awk -F: '{ print $6 }'"
  register: user_home_result

- set_fact: user_home={{ user_home_result.stdout }}

- name: Setting up user {{ user }}
  user: >
    generate_ssh_key=yes
    name="{{ user }}"
  when: user_home != ''

- name: Capturing user's home directory
  shell: "getent passwd {{ user }} | awk -F: '{ print $6 }'"
  register: user_home_result2
  when: user_home != ''

- set_fact: user_home={{ user_home_result2.stdout }}
  when: user_home != ''
like image 559
Adam Ryczkowski Avatar asked Oct 26 '15 14:10

Adam Ryczkowski


People also ask

Can we define multiple conditions in Ansible?

Defining multiple when conditions in AnsibleIf both conditions are true, then issue the reboot command using the Ansible reboot module. Otherwise, skip the reboot option.

How do I run a specific part of my playbook in Ansible?

The easiest way to run only one task in Ansible Playbook is using the tags statement parameter of the “ansible-playbook” command. The default behavior is to execute all the tags in your Playbook with --tags all .

How you can run your all tasks at once in Ansible?

If you want to run multiple tasks in a playbook concurrently, use async with poll set to 0. When you set poll: 0 , Ansible starts the task and immediately moves on to the next task without waiting for a result. Each async task runs until it either completes, fails or times out (runs longer than its async value).


2 Answers

You can put the tasks in a new yml file and use a conditional include:

# subtasks.yml
---
- name: Setting up user {{ user }}
  user: >
    generate_ssh_key=yes
    name="{{ user }}"

- name: Capturing user's home directory
  shell: "getent passwd {{ user }} | awk -F: '{ print $6 }'"
  register: user_home_result

- set_fact: user_home={{ user_home_result.stdout }}

And in the playbook:

- name: Capturing user's home directory
  shell: "getent passwd {{ user }} | awk -F: '{ print $6 }'"
  register: user_home_result

- set_fact: user_home={{ user_home_result.stdout }}

- include: subtask.yml
  when:  user_home != ''  
like image 122
Sebastian Stigler Avatar answered Oct 24 '22 14:10

Sebastian Stigler


As of version 2.1, ansible has blocks for logical task grouping. Blocks allow you to specify common things for a few tasks only once, including the when conditionals. Ex:

- block:

    - name: put a file somewhere
      copy: src=asdf dest=asdf

    - name: put another file somewhere
      template: src=asdf.j2 dest=asdf

  when: bool_is_true

The above is equivalent to attaching a when: bool_is_true to both of the tasks inside the block.

More information at https://docs.ansible.com/ansible/latest/user_guide/playbooks_blocks.html

like image 40
snetch Avatar answered Oct 24 '22 15:10

snetch