質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ansible

Ansibleは、Python で書かれたサーバーの設定を管理するための 構成管理ツールです。

Q&A

0回答

1301閲覧

【Ansible】UnboundLocalError: local variable 'maxlps' referenced before assignment

k.y.61

総合スコア10

Ansible

Ansibleは、Python で書かれたサーバーの設定を管理するための 構成管理ツールです。

0グッド

0クリップ

投稿2018/11/20 02:38

前提・実現したいこと

AnsibleでAIX自動化を検証しています。
標準モジュールとして存在する"aix_lvol"のコードに"mklv"コマンドの
"-x"オプション(MAXLPSを定義する)を追加したく、コードを編集している中で
エラーが発生しました。

発生している問題・エラーメッセージ

UnboundLocalError: local variable 'maxlps' referenced before assignment

該当のソースコード

・・・・文字数制限のため中略・・・・

def parse_lv(data):
name = None

for line in data.splitlines(): match = re.search(r"LOGICAL VOLUME:\s+(\w+)\s+VOLUME GROUP:\s+(\w+)", line) if match is not None: name = match.group(1) vg = match.group(2) continue match = re.search(r"LPs:\s+(\d+).*PPs", line) if match is not None: lps = int(match.group(1)) continue match = re.search(r"PP SIZE:\s+(\d+)", line) if match is not None: pp_size = int(match.group(1)) continue match = re.search(r"INTER-POLICY:\s+(\w+)", line) if match is not None: policy = match.group(1) continue match = re.search(r"BB POLICY:\s+(\D+)", line) if match is not None: bbpolicy = match.group(1) continue match = re.search(r"MAX LPs:\s+(\d+).*PP", line) if match is not None: maxlps = match.group(1) continue if not name: return None size = lps * pp_size return {'name': name, 'vg': vg, 'size': size, 'policy': policy, 'bbpolicy': bbpolicy, 'maxlps': maxlps}

def parse_vg(data):

for line in data.splitlines(): match = re.search(r"VOLUME GROUP:\s+(\w+)", line) if match is not None: name = match.group(1) continue match = re.search(r"TOTAL PP.*((\d+)", line) if match is not None: size = int(match.group(1)) continue match = re.search(r"PP SIZE:\s+(\d+)", line) if match is not None: pp_size = int(match.group(1)) continue match = re.search(r"FREE PP.*((\d+)", line) if match is not None: free = int(match.group(1)) continue return {'name': name, 'size': size, 'free': free, 'pp_size': pp_size}

def main():
module = AnsibleModule(
argument_spec=dict(
vg=dict(type='str', required=True),
lv=dict(type='str', required=True),
lv_type=dict(type='str', default='jfs2'),
size=dict(type='str'),
opts=dict(type='str', default=''),
copies=dict(type='str', default='1'),
maxlps=dict(type='str', default='512'),
state=dict(type='str', default='present', choices=['absent', 'present']),
policy=dict(type='str', default='maximum', choices=['maximum', 'minimum']),
bbpolicy=dict(type='str', default='relocatable', choices=['relocatable', 'non-relocatable']),
pvs=dict(type='list', default=list())
),
supports_check_mode=True,
)

vg = module.params['vg'] lv = module.params['lv'] lv_type = module.params['lv_type'] size = module.params['size'] opts = module.params['opts'] copies = module.params['copies'] maxlps = module.params['maxlps'] policy = module.params['policy'] bbpolicy = module.params['bbpolicy'] state = module.params['state'] pvs = module.params['pvs'] pv_list = ' '.join(pvs) if policy == 'maximum': lv_policy = 'x' else: lv_policy = 'm' if bbpolicy == 'non-relocatable': lv_bbpolicy = 'n' else: lv_bbpolicy = 'y' # Add echo command when running in check-mode if module.check_mode: test_opt = 'echo ' else: test_opt = '' # check if system commands are available lsvg_cmd = module.get_bin_path("lsvg", required=True) lslv_cmd = module.get_bin_path("lslv", required=True) # Get information on volume group requested rc, vg_info, err = module.run_command("%s %s" % (lsvg_cmd, vg)) if rc != 0: if state == 'absent': module.exit_json(changed=False, msg="Volume group %s does not exist." % vg) else: module.fail_json(msg="Volume group %s does not exist." % vg, rc=rc, out=vg_info, err=err) this_vg = parse_vg(vg_info) if size is not None: # Calculate pp size and round it up based on pp size. lv_size = round_ppsize(convert_size(module, size), base=this_vg['pp_size']) # Get information on logical volume requested rc, lv_info, err = module.run_command( "%s %s" % (lslv_cmd, lv)) if rc != 0: if state == 'absent': module.exit_json(changed=False, msg="Logical Volume %s does not exist." % lv) changed = False this_lv = parse_lv(lv_info) if state == 'present' and not size: if this_lv is None: module.fail_json(msg="No size given.") if this_lv is None: if state == 'present': if lv_size > this_vg['free']: module.fail_json(msg="Not enough free space in volume group %s: %s MB free." % (this_vg['name'], this_vg['free'])) # create LV mklv_cmd = module.get_bin_path("mklv", required=True) cmd = "%s %s -t %s -y %s -c %s -x %s -e %s -b %s %s %s %sM %s" % (test_opt, mklv_cmd, lv_type, lv, copies, lv_maxlps, lv_policy, lv_bbpolicy, opts, vg, lv_size, pv_list) rc, out, err = module.run_command(cmd) if rc == 0: module.exit_json(changed=True, msg="Logical volume %s created." % lv) else: module.fail_json(msg="Creating logical volume %s failed." % lv, rc=rc, out=out, err=err) else: if state == 'absent': # remove LV rmlv_cmd = module.get_bin_path("rmlv", required=True) rc, out, err = module.run_command("%s %s -f %s" % (test_opt, rmlv_cmd, this_lv['name'])) if rc == 0: module.exit_json(changed=True, msg="Logical volume %s deleted." % lv) else: module.fail_json(msg="Failed to remove logical volume %s." % lv, rc=rc, out=out, err=err) elif this_lv['bbpolicy'] != bbpolicy: # change lv bb policy chlv_cmd = module.get_bin_path("chlv", required=True) rc, out, err = module.run_command("%s %s -b %s %s" % (test_opt, chlv_cmd, lv_bbpolicy, this_lv['name'])) if rc == 0: module.exit_json(changed=True, msg="Logical volume %s bbpolicy changed: %s." % (lv, bbpolicy)) else: module.fail_json(msg="Failed to change logical volume %s bbpolicy." % lv, rc=rc, out=out, err=err) elif this_lv['maxlps'] != maxlps: # change lv maxlps chlv_cmd = module.get_bin_path("chlv", required=True) rc, out, err = module.run_command("%s %s -x %s %s" % (test_opt, chlv_cmd, lv_maxlps, this_lv['name'])) if rc == 0: module.exit_json(changed=True, msg="Logical volume %s maxlps changed: %s." % (lv, maxlps)) else: module.fail_json(msg="Failed to change logical volume %s maxlps." % lv, rc=rc, out=out, err=err) else: if this_lv['policy'] != policy: # change lv allocation policy chlv_cmd = module.get_bin_path("chlv", required=True) rc, out, err = module.run_command("%s %s -e %s %s" % (test_opt, chlv_cmd, lv_policy, this_lv['name'])) if rc == 0: module.exit_json(changed=True, msg="Logical volume %s policy changed: %s." % (lv, policy)) else: module.fail_json(msg="Failed to change logical volume %s policy." % lv, rc=rc, out=out, err=err) if vg != this_lv['vg']: module.fail_json(msg="Logical volume %s already exist in volume group %s" % (lv, this_lv['vg'])) # from here the last remaining action is to resize it, if no size parameter is passed we do nothing. if not size: module.exit_json(changed=False, msg="Logical volume %s already exist." % (lv)) # resize LV based on absolute values if int(lv_size) > this_lv['size']: extendlv_cmd = module.get_bin_path("extendlv", required=True) cmd = "%s %s %s %sM" % (test_opt, extendlv_cmd, lv, lv_size - this_lv['size']) rc, out, err = module.run_command(cmd) if rc == 0: module.exit_json(changed=True, msg="Logical volume %s size extended to %sMB." % (lv, lv_size)) else: module.fail_json(msg="Unable to resize %s to %sMB." % (lv, lv_size), rc=rc, out=out, err=err) elif lv_size < this_lv['size']: module.fail_json(msg="No shrinking of Logical Volume %s permitted. Current size: %s MB" % (lv, this_lv['size'])) else: module.exit_json(changed=False, msg="Logical volume %s size is already %sMB." % (lv, lv_size))

if name == 'main':
main()

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問