If you try to resize a lvol to the current size return a changed=False and don't fail.

This addresses GH-5165 and adds the ability to check if a lvol exists.

The tests for this don't fit nicely into the current integration tests so they are below.

```
---

- name: remove any existing lv=one of vg=main
  lvol: lv=one vg=main state=absent

- name: remove any existing lv=two of vg=main
  lvol: lv=two vg=main state=absent

- name: check to see if lv=one of vg=main exists
  lvol: lv=one vg=main state=present
  ignore_errors: true
  register: lvol_result0

- name: Assert that we will get a "No size given."
  assert:
    that:
      - "'No size given.' in lvol_result0.msg"

- name: create lv=one of vg=main sized 30g
  lvol: lv=one size=30g vg=main state=present
  register: lvol_result1

- name: Assert that we made changes."
  assert:
    that:
      - "lvol_result1.changed == True"

- name: check to see if lv=one of vg=main exists
  lvol: lv=one vg=main state=present
  register: lvol_result2

- name: Assert that we did not make changes."
  assert:
    that:
      - "lvol_result2.changed == False"

- name: remove lv=one of vg=main
  lvol: lv=one vg=main state=absent

- name: create lv=two of vg=main sized 30G
  lvol: lv=two size=30G vg=main state=present
  register: lvol_result3

- name: Assert that we made changes."
  assert:
    that:
      - "lvol_result3.changed == True"

- name: reduce lv=two of vg=main to 15G
  lvol: lv=two size=15G vg=main state=present
  register: lvol_result4

- name: Assert that we made changes."
  assert:
    that:
      - "lvol_result4.changed == True"

- name: increase lv=two of vg=main to 30G
  lvol: lv=two size=30G vg=main state=present
  register: lvol_result5

- name: Assert that we made changes."
  assert:
    that:
      - "lvol_result5.changed == True"

- name: create lv=two of vg=main sized 30G when already exists at 30G
  lvol: lv=two size=30g vg=main state=present
  register: lvol_result6

- name: Assert that we did not make changes."
  assert:
    that:
      - "lvol_result6.changed == False"

- name: remove lv=two of vg=main
  lvol: lv=two vg=main state=absent
```
This commit is contained in:
Richard C Isaacson 2014-02-24 16:56:08 -06:00
parent 7517cd9398
commit bf30707670

View file

@ -72,8 +72,10 @@ EXAMPLES = '''
''' '''
import re import re
decimal_point = re.compile(r"(\.|,)") decimal_point = re.compile(r"(\.|,)")
def parse_lvs(data): def parse_lvs(data):
lvs = [] lvs = []
for line in data.splitlines(): for line in data.splitlines():
@ -84,6 +86,7 @@ def parse_lvs(data):
}) })
return lvs return lvs
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec=dict( argument_spec=dict(
@ -102,9 +105,6 @@ def main():
size_opt = 'L' size_opt = 'L'
size_unit = 'm' size_unit = 'm'
if state=='present' and not size:
module.fail_json(msg="No size given.")
if size: if size:
# LVCREATE(8) -l --extents option with percentage # LVCREATE(8) -l --extents option with percentage
if '%' in size: if '%' in size:
@ -141,7 +141,9 @@ def main():
unit = 'm' unit = 'm'
else: else:
unit = size_unit unit = size_unit
rc,current_lvs,err = module.run_command("lvs --noheadings -o lv_name,size --units %s --separator ';' %s" % (unit, vg))
rc, current_lvs, err = module.run_command(
"lvs --noheadings -o lv_name,size --units %s --separator ';' %s" % (unit, vg))
if rc != 0: if rc != 0:
if state == 'absent': if state == 'absent':
@ -160,6 +162,12 @@ def main():
else: else:
this_lv = None this_lv = None
if state == 'present' and not size:
if this_lv is None:
module.fail_json(msg="No size given.")
else:
module.exit_json(changed=False, vg=vg, lv=this_lv['name'], size=this_lv['size'])
msg = '' msg = ''
if this_lv is None: if this_lv is None:
if state == 'present': if state == 'present':
@ -200,6 +208,8 @@ def main():
rc, _, err = module.run_command("%s -%s %s%s %s/%s" % (tool, size_opt, size, size_unit, vg, this_lv['name'])) rc, _, err = module.run_command("%s -%s %s%s %s/%s" % (tool, size_opt, size, size_unit, vg, this_lv['name']))
if rc == 0: if rc == 0:
changed = True changed = True
elif "matches existing size" in err:
module.exit_json(changed=False, vg=vg, lv=this_lv['name'], size=this_lv['size'])
else: else:
module.fail_json(msg="Unable to resize %s to %s%s" % (lv, size, size_unit), rc=rc, err=err) module.fail_json(msg="Unable to resize %s to %s%s" % (lv, size, size_unit), rc=rc, err=err)
@ -207,4 +217,5 @@ def main():
# import module snippets # import module snippets
from ansible.module_utils.basic import * from ansible.module_utils.basic import *
main() main()