# condition based on register match

**URL:** <https://forum.ansible.com/t/condition-based-on-register-match/32787>\
**Category:** Ansible Project\
**Tags:** rhel\
**Created:** [April 27, 2020, 6:18am UTC](https://forum.ansible.com/t/condition-based-on-register-match/32787 "2020-04-27T06:18:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dudu.c](https://avatars.discourse-cdn.com/v4/letter/d/278dde/32.png) [@dudu.c](https://forum.ansible.com/u/dudu.c)\
**Post date:** [April 27, 2020, 6:18am UTC](https://forum.ansible.com/t/condition-based-on-register-match/32787/1 "2020-04-27T06:18:40Z")

</div>

I have a task that mounts devices in a Linux machine.  
I wish to run the task only if the device is not mounted, therefore I’m running a mount command and save the output in a register.

On the mount task, I’m trying to use **when** by searching the device in the register output - and I’m failing to do so.  
The task is running even that /dev/sdb1 is included in the register

**Playbook:**

```auto
  - name: check mount
    shell: mount | awk '{print $1}'
    register: mount

  - debug:
      msg: " {{ mount.stdout_lines }}"

  - name: Mount task 1 - create directory
    file:
      path: "{{item.mount_path}}"
      state: directory
    when: mount.stdout_lines != "/dev/sdb1"

```

**Register output**

sysfs\nproc\ndevtmpfs\nsecurityfs\ntmpfs\ndevpts\ntmpfs\ntmpfs\ncgroup\npstore\ncgroup\ncgroup\ncgroup\ncgroup\ncgroup\ncgroup\ncgroup\ncgroup\ncgroup\ncgroup\nconfigfs\n/dev/mapper/rhel-root\nselinuxfs\nsystemd-1\ndebugfs\nmqueue\nhugetlbfs\n/dev/sda1\n/dev/mapper/rhel-home\ntmpfs\n/ **dev/sdb1** \n/dev/sdc1\n/dev/sdd1"

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [April 27, 2020, 6:43am UTC](https://forum.ansible.com/t/condition-based-on-register-match/32787/2 "2020-04-27T06:43:29Z")

</div>

The module "mount" does the job and creates the directory if needed.  
[https://docs.ansible.com/ansible/latest/modules/mount\_module.html](https://docs.ansible.com/ansible/latest/modules/mount_module.html)

&nbsp;&nbsp;"state: If mounted, the device will be actively mounted and appropriately  
&nbsp;&nbsp;configured in fstab. If the mount point is not present, the mount point  
&nbsp;&nbsp;will be created."

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [April 27, 2020, 7:05am UTC](https://forum.ansible.com/t/condition-based-on-register-match/32787/3 "2020-04-27T07:05:26Z")

</div>

Correct syntax is below

&nbsp;&nbsp;&nbsp;- name: Mount task 1 - create directory  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: /dev/sdb1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: directory  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: "'/dev/sdb1' not in mount.stdout\_lines"

The code implicates a loop

&nbsp;&nbsp;&nbsp;- name: Mount task 1 - create directory  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: "{{ item.mount\_path }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: directory  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ my\_list\_of\_mountpoints }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: "item.mount\_path not in mount.stdout\_lines"

(Ansible module "mount" does the job.)

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [April 27, 2020, 7:09am UTC](https://forum.ansible.com/t/condition-based-on-register-match/32787/4 "2020-04-27T07:09:13Z")

</div>

Errata:

> Correct syntax is below
> 
> &nbsp;&nbsp;&nbsp;- name: Mount task 1 - create directory  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: /dev/sdb1

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: "{{ mount\_point\_of\_dev\_sdb1 }}"

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [April 27, 2020, 7:13am UTC](https://forum.ansible.com/t/condition-based-on-register-match/32787/5 "2020-04-27T07:13:33Z")

</div>

Errata 2:

> The code implicates a loop
> 
> &nbsp;&nbsp;&nbsp;- name: Mount task 1 - create directory  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: "{{ item.mount\_path }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: directory  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ my\_list\_of\_mountpoints }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: "item.mount\_path not in mount.stdout\_lines"

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: "item.mount\_device not in mount.stdout\_lines"

---

<div class="post-metadata">

**Author:** ![dudu.c](https://avatars.discourse-cdn.com/v4/letter/d/278dde/32.png) [@dudu.c](https://forum.ansible.com/u/dudu.c)\
**Post date:** [April 27, 2020, 9:21am UTC](https://forum.ansible.com/t/condition-based-on-register-match/32787/6 "2020-04-27T09:21:29Z")

</div>

Hi ,

That’s helpful, but is there an option to match partial string ?
