Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking for multiple conditions using "when" on single task in ansible

I want to evaluate multiple condition in ansible using when, here is my playbook:

- name: Check that the SSH Key exists
   local_action:
     module: stat
     path: "/home/{{ login_user.stdout }}/{{ ssh_key_location }}"
   register: sshkey_result

 - name: Generating a new SSH key for the current user it's not exists already
   local_action:
      module: user
      name: "{{ login_user.stdout }}"
      generate_ssh_key: yes 
      ssh_key_bits: 2048
   when: sshkey_result.rc == 1 and  ( github_username is undefined or github_username |lower == 'none' )

here is my var file for reference:

---
vpc_region: eu-west-1
key_name: my_github_key
ssh_key_location: .ssh/id_rsa.pub

When I try to execute this playbook, I am getting this error:

TASK: [test | Check that the SSH Key exists] **********************************
ok: [localhost -> 127.0.0.1]

 TASK: [test | Generating a new SSH key for the current user it's not exists already] ***
 fatal: [localhost] => error while evaluating conditional: sshkey_result.rc == 1 and  ( github_username is undefined or github_username |lower == 'none' )

        FATAL: all hosts have already failed -- aborting

Can somebody point me out that how we can use multiple conditions with ansible on single task.

Thanks

like image 788
Arbab Nazar Avatar asked Nov 22 '15 16:11

Arbab Nazar


People also ask

How do I run a parallel task 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).

Which keyword do you use to create a conditional task in Ansible?

To implement conditions in Ansible, we use the when keyword. The keyword takes Boolean expressions based on a value or a variable from previous tasks or facts gathered from the remote hosts. This guide will teach you how to implement conditions in Ansible playbooks using the when keyword.

How do you run task only once in Ansible?

Ansible run_once parameter is used with a task, which you want to run once on first host. When used, this forces the Ansible controller to attempt execution on first host in the current hosts batch, then the result can be applied to the other remaining hosts in current batch.


Video Answer


4 Answers

You can use like this.

when: condition1 == "condition1" or condition2 == "condition2"

Link to official docs: The When Statement.

Also Please refer to this gist: https://gist.github.com/marcusphi/6791404

like image 129
Dhanasekaran Anbalagan Avatar answered Oct 17 '22 06:10

Dhanasekaran Anbalagan


Adding to https://stackoverflow.com/users/1638814/nvartolomei answer, which will probably fix your error.

Strictly answering your question, I just want to point out that the when: statement is probably correct, but would look easier to read in multiline and still fulfill your logic:

when: 
  - sshkey_result.rc == 1
  - github_username is undefined or 
    github_username |lower == 'none'

https://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html#the-when-statement

like image 38
user2066480 Avatar answered Oct 17 '22 06:10

user2066480


The problem with your conditional is in this part sshkey_result.rc == 1, because sshkey_result does not contain rc attribute and entire conditional fails.

If you want to check if file exists check exists attribute.

Here you can read more about stat module and how to use it.

like image 5
nvartolomei Avatar answered Oct 17 '22 07:10

nvartolomei


Also you can use default() filter. Or just a shortcut d()

- name: Generating a new SSH key for the current user it's not exists already
  local_action:
    module: user
    name: "{{ login_user.stdout }}"
    generate_ssh_key: yes 
    ssh_key_bits: 2048
  when: 
    - sshkey_result.rc == 1
    - github_username | d('none') | lower == 'none'
like image 1
ALex_hha Avatar answered Oct 17 '22 06:10

ALex_hha