# how to run an if/else test in a task

**URL:** <https://forum.ansible.com/t/how-to-run-an-if-else-test-in-a-task/32973>\
**Category:** Ansible Project\
**Created:** [May 19, 2020, 2:23pm UTC](https://forum.ansible.com/t/how-to-run-an-if-else-test-in-a-task/32973 "2020-05-19T14:23:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hieronymus](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/hieronymus/32/1624_2.png) [@hieronymus](https://forum.ansible.com/u/hieronymus)\
**Post date:** [May 19, 2020, 2:23pm UTC](https://forum.ansible.com/t/how-to-run-an-if-else-test-in-a-task/32973/1 "2020-05-19T14:23:27Z")

</div>

Hi Teams,

I’m looking for to do a test before to execute a task. I need to checkup if my computer has already joined the domain  
before to join it. otherwise, the adcli will failed.  
When computer isn’t in domain, the klist will return a “not found” message and at this time the “join active directory” must be running…  
Something goes wrong because this task will not be runned.  
Here’re the portion of task:

`  
[…]

- name: checking if domain already joined  
shell: /bin/bash -c “/usr/bin/klist -k|grep -i {{ ansible\_hostname }}”  
register: ad\_join\_status  
ignore\_errors: true

- name: join active directory  
shell: /bin/bash -c “/usr/sbin/realm join --one-time-password={{ ad\_server\_onetime }} {{ ad\_server\_domain }}”  
register: ad\_active  
args:  
executable: /usr/bin/bash  
when: ad\_join\_status == “not found”  
[…]  
`

Do you have any Idea?  
Is my test ok or should be another one?  
Thanks and best regards, J.

---

<div class="post-metadata">

**Author:** ![IaV1](https://avatars.discourse-cdn.com/v4/letter/i/8edcca/32.png) [@IaV1](https://forum.ansible.com/u/IaV1)\
**Post date:** [May 19, 2020, 4:54pm UTC](https://forum.ansible.com/t/how-to-run-an-if-else-test-in-a-task/32973/2 "2020-05-19T16:54:01Z")

</div>

In the when clause use something like ad\_join\_status.stdout\_lines[0]

to check what it needs to be add

- debug  
var: ad\_join\_status

and see what is there

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [May 19, 2020, 4:56pm UTC](https://forum.ansible.com/t/how-to-run-an-if-else-test-in-a-task/32973/3 "2020-05-19T16:56:14Z")

</div>

> Hi Teams,
> 
> I'm looking for to do a test before to execute a task. I need to checkup if my computer has already joined the domain  
> before to join it. otherwise, the adcli will failed.  
> When computer isn't in domain, the klist will return a "not found" message and at this time the "join active directory"  
> must be running...  
> Something goes wrong because this task will not be runned.  
> Here're the portion of task:
> 
> \>  
> [...]  
> -name:checking ifdomain already joined  
> shell:/bin/bash -c "/usr/bin/klist -k|grep -i {{ ansible\_hostname }}"  
> register:ad\_join\_status  
> ignore\_errors:true
> 
> -name:join active directory  
> shell:/bin/bash -c "/usr/sbin/realm join --one-time-password={{ ad\_server\_onetime }} {{ ad\_server\_domain }}"  
> register:ad\_active  
> args:  
> executable:/usr/bin/bash  
> when:ad\_join\_status =="not found"  
> [...]  
> \>

Hello Jerome,

Ansible is not a glorified tool for running shell scripts on the target :-/

At any rate, ad\_join\_status is not a string. Checkout ad\_join\_status.stdout (string) or ad\_join\_status.stdout\_lines (list).

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![hieronymus](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/hieronymus/32/1624_2.png) [@hieronymus](https://forum.ansible.com/u/hieronymus)\
**Post date:** [May 20, 2020, 10:23am UTC](https://forum.ansible.com/t/how-to-run-an-if-else-test-in-a-task/32973/4 "2020-05-20T10:23:14Z")

</div>

Thanks for your reply and tips.

So, has requested I’ve checked out the stdout message:

I’ve received only message when the domain already joined, like:

`ok: [localhost] => { "ad_join_status.stdout_lines": [" 3 server_name@domain", " 3 server_name@domain", " 3 server_name@domain", " 3 host/server_name@domain", " 3 host/server_name@domain", [...]`

Otherwise, the klist cannot start because it doesn’t found the file : /etc/krb5.keytab :

`

# /usr/bin/klist -k|grep -i server\_name

klist: Key table file ‘/etc/krb5.keytab’ not found while starting keytab  
`

And in this case nothing appears in stdout:

`  
ok: [localhost] =\>

ad\_join\_status.stdout: ‘’  
`

That’s means, I should use an another test for checking domain joining… ☹

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [May 20, 2020, 10:38am UTC](https://forum.ansible.com/t/how-to-run-an-if-else-test-in-a-task/32973/5 "2020-05-20T10:38:34Z")

</div>

> Thanks for your reply and tips.  
> So, has requested I've checked out the stdout message:
> 
> I've received only message when the domain already joined, like:
> 
> \>  
> ok:[localhost]=\>{  
> "ad\_join\_status.stdout\_lines":[  
> " 3 server\_name@domain",  
> " 3 server\_name@domain",  
> " 3 server\_name@domain",  
> " 3 host/server\_name@domain",  
> " 3 host/server\_name@domain",  
> [...]  
> \>
> 
> Otherwise, the klist cannot start because it doesn't found the file : /etc/krb5.keytab :  
> \>  
> # /usr/bin/klist -k|grep -i server\_name  
> klist:Keytable file '/etc/krb5.keytab'notfound whilestarting keytab  
> \>
> 
> And in this case nothing appears in stdout:  
> \>  
> ok:[localhost]=\>
> 
> ```
> ad\_join\_status\.stdout:&#39;&#39;
> 
> ```
> 
> \>
> 
> That's means, I should use an another test for checking domain joining... ☹

Right, so far this isn't really Ansible related. Determine a command which reliably tells you  
whether the domain is already joined \*before\* you are trying to automate it.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke
