condition based on register match

69 views
Skip to first unread message

דודו דודו

unread,
Apr 27, 2020, 2:18:40 AM4/27/20
to Ansible Project
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:

  - 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"

Vladimir Botka

unread,
Apr 27, 2020, 2:43:47 AM4/27/20
to דודו דודו, ansible...@googlegroups.com
On Sun, 26 Apr 2020 23:18:40 -0700 (PDT)
דודו דודו <dudu.c...@gmail.com> wrote:

> 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

The module "mount" does the job and creates the directory if needed.
https://docs.ansible.com/ansible/latest/modules/mount_module.html

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

HTH,

-vlado

Vladimir Botka

unread,
Apr 27, 2020, 3:05:42 AM4/27/20
to דודו דודו, ansible...@googlegroups.com
On Sun, 26 Apr 2020 23:18:40 -0700 (PDT)
דודו דודו <dudu.c...@gmail.com> wrote:

> The task is running even that /dev/sdb1 is included in the register
>
> *Playbook:*
>
> - 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"

Correct syntax is below

- name: Mount task 1 - create directory
file:
path: /dev/sdb1
state: directory
when: "'/dev/sdb1' not in mount.stdout_lines"

The code implicates a loop

- name: Mount task 1 - create directory
file:
path: "{{ item.mount_path }}"
state: directory
loop: "{{ my_list_of_mountpoints }}"
when: "item.mount_path not in mount.stdout_lines"

(Ansible module "mount" does the job.)

HTH,

-vlado

Vladimir Botka

unread,
Apr 27, 2020, 3:09:31 AM4/27/20
to דודו דודו, ansible...@googlegroups.com
On Mon, 27 Apr 2020 09:05:26 +0200
Vladimir Botka <vbo...@gmail.com> wrote:

Errata:

> Correct syntax is below
>
> - name: Mount task 1 - create directory
> file:
> path: /dev/sdb1
path: "{{ mount_point_of_dev_sdb1 }}"

Vladimir Botka

unread,
Apr 27, 2020, 3:13:51 AM4/27/20
to דודו דודו, ansible...@googlegroups.com
On Mon, 27 Apr 2020 09:05:26 +0200
Vladimir Botka <vbo...@gmail.com> wrote:

Errata 2:

> The code implicates a loop
>
> - name: Mount task 1 - create directory
> file:
> path: "{{ item.mount_path }}"
> state: directory
> loop: "{{ my_list_of_mountpoints }}"
> when: "item.mount_path not in mount.stdout_lines"
when: "item.mount_device not in mount.stdout_lines"

דודו דודו

unread,
Apr 27, 2020, 5:21:29 AM4/27/20
to Ansible Project
Hi , 

That's helpful, but is there an option to match partial string ?
Reply all
Reply to author
Forward
0 new messages