Ansible - create LVM

68 views
Skip to first unread message

tasio...@op.pl

unread,
Feb 5, 2018, 3:04:56 PM2/5/18
to Ansible Project
Hello,
I'm pretty new in ansible, so excuse me for this (stupid?) question, but it bothers me.

I'm trying to set up LVM on the remote host with ansible.

Here is my role:

---
- name: install lvm2
  yum
:
    name
: "{{ item }}"
    state
: present
  with_items
:
   
- lvm2
  tags
:
   
- install

- name: select a drive for LVM
  shell
: "fdisk -l |grep vdb | awk '{print $2}'"
 
register: drive_for_lvm
  tags
:
   
- configure

- set_fact:
    drive_for_lvm
: "{{ drive_for_lvm.stdout_lines }}"
  tags
:
   
- configure

- set_fact:
    drive_for_lvm
: "{{ drive_for_lvm|replace(':','') }}"
  tags
:
   
- configure

############################## ADD LVM ###########################

- name: create VG
  lvg
:
    vg
: vg_test
    pvs
: "{{ drive_for_lvm }}"
    pesize
: 16
  tags
:
   
- configure

- name: create LV
  lvol
:
    vg
: vg_test
    lv
: lv_test
    size
: 100%FREE
    pvs
: "{{ drive_for_lvm }}"
  tags
:
   
- configure


When I execute it, I've got this:

root@ubuntu-latest:~/ansible# ansible-playbook -i group_vars/ playbook.yml --skip-tags=install

PLAY
[test] ********************************************************************************************************************************

TASK
[lvm : select a drive for LVM] ********************************************************************************************************
changed
: [docker2]

TASK
[lvm : set_fact] **********************************************************************************************************************
ok
: [docker2]

TASK
[lvm : set_fact] **********************************************************************************************************************
ok
: [docker2]

TASK
[lvm : create VG] *********************************************************************************************************************
changed
: [docker2]

TASK
[lvm : create LV] *********************************************************************************************************************
fatal
: [docker2]: FAILED! => {"changed": false, "err": "  Physical Volume \"[/dev/vdb]\" not found in Volume Group \"vg_test\".\n", "msg": "Creating logical volume 'lv_test' failed", "rc": 5}


Then, I verified the host called "docker2" if it contains PVS /dev/vdb (and it does).

[root@docker2 ~]# pvs |grep vdb
 
/dev/vdb   vg_test lvm2 a--   4.98g 4.98g



So why ansible says that there is no such physical volume?


I finally edited main.yml inside the role (just commented out pvs section in last task):

- name: create VG
  lvg
:
    vg
: vg_test
    pvs
: "{{ drive_for_lvm }}"
    pesize
: 16
  tags
:
   
- configure

- name: create LV
  lvol
:
    vg
: vg_test
    lv
: lv_test
    size
: 100%FREE
#    pvs: "{{ drive_for_lvm }}"
  tags
:
   
- configure



And it works as expected.. Can somebody explain the difference? It is a bug or what?

Best regards!


Henning Rohde

unread,
Feb 5, 2018, 4:07:06 PM2/5/18
to Ansible Project
As the output of some shell-command is not per se delimited to one single value, registering it creates a list.

Thus, using "{{ some_list }}" get's you square-brackets around the list of values - although in your case, it's a list with just one single value.

Besides omitting the parameter, you could for instance join the list to a string: "{{ drive_for_lvm | join '' }}", see https://docs.ansible.com/ansible/playbooks_filters.html#id7
... or you could limit the list to its first element, either with "{{ drive_for_lvm[0] }}" or "{{ drive_for_lvm | first }}", see again the filters or https://docs.ansible.com/ansible/playbooks_variables.html#accessing-complex-variable-data

tasio...@op.pl

unread,
Feb 6, 2018, 2:11:33 AM2/6/18
to Ansible Project
Thank you so much for exhaustive explanation.

Best regards,
Michal
Reply all
Reply to author
Forward
0 new messages