0
votes

So basically i want to check somehow if the /dev/xvdb1 is mounted to /var

If answer is yes:

  1. don't create dir
  2. don't copy files
  3. don't mount /var etc

If the answer is no:

  1. proceed with everything

    - name: check if /var is mounted
      shell: df -hT | grep /var
      register: df
    
    - name: Create /mnt/newvar directory
      file: 
        path: "{{ newvar_dir }}"
        state: directory
      when: not df.stdout_lines
    
    - name: "Get UUID for partition"
      shell: "lsblk -no UUID /dev/xvdb1"
      register: volume_uuids
    
    - name: Mount /mnt/newvar to /dev/xvdb1
      mount:
        path: "{{ newvar_dir }}"
        src: "UUID={{ item }}"
        fstype: "{{ volume_filesystem_type }}"
        opts: "defaults,noauto"
        state: mounted
      with_items: 
        - "{{ volume_uuids.stdout_lines }}"
      when: not df.stdout_lines
    

Error: fatal: [x.x.x.x]: FAILED! => {"changed": true, "cmd": "df -hT | grep /var", "delta": "0:00:00.005132", "end": "2019-03-10 03:07:22.485343", "msg": "non-zero return code", "rc": 1, "start": "2019-03-10 03:07:22.480211", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}

The issue is: if there is no output from df command, ansible brakes.

2

2 Answers

0
votes

You could you ansible_mounts fact to see if the mount exists and set_fact to define a new variable, instead of using df and registering results.

See below for reference:

- name: Check if /var mount exists
  set_fact:
    is_var_mounted: true
  when: item.mount == "/var"
  with_items: "{{ ansible_mounts }}"

- name: Do everything you want to do if not mounted
  debug:
     msg: "Doing Things"
  when: is_var_mounted is not defined

- name: Skip steps if mounted
  debug:
     msg: "Skipping"
  when: is_var_mounted is defined
0
votes

Very probably /var is not a mount-point and the command returns 1.

> df -hT | grep /var
> echo $?
1

This is the reason for

Error: fatal: ... "msg": "non-zero return code", "rc": 1,

Simple condition would be

when: /var in ansible_mounts|json_query('[].mount')