0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ansible で QNAP の QPKG を入れる

Posted at

QNAPにAnsibleできるようになったところで、まずはQPKGを入れてみる。

どうやって入れるか

qpkg_cli を使う。

# qpkg_cli -s パッケージ名

で入っているかどうかをチェックして、

# qpkg_cli -a パッケージ名

で入れる

なお、キューに入れてすぐ終了してしまうので、

# qpkg_cli -s パッケージ名

でインストール済みになるまで待つ。

-sオプションの返り値

[CLI] QPKG XXX not found

未インストール

[CLI] QPKG XXX download YY%
[CLI] QPKG XXX is in installation stage Y

インストール中

[CLI] QPKG XXX is installed

インストール終了

[CLI] QPKG XXX opcode 5 status code 4

削除中

Ansibleで実現する

shellなどを組み合わせていろいろ考えるより、モジュールを作ってしまった方が早い。
自分で使うだけなのでいい加減なモジュール。もちろんテストもしてない。

qpkg.py
# !/usr/bin/python

import time

ANSIBLE_METADATA = {
    'metadata_version': '1.1',
    'status': ['preview'],
    'supported_by': 'iwatam'
}

DOCUMENTATION = '''
'''

EXAMPLES = '''
'''

RETURN = '''
'''

from ansible.module_utils.basic import AnsibleModule

def check_status(module,name):
    while True:
        rc, out, err=module.run_command(['qpkg_cli','-s',name])
        if name+' is installed' in out:
            return 'present'
        elif name+' not found' in out:
            return 'absent'
        time.sleep(1)

def main():
    module = AnsibleModule(
        argument_spec=dict(
            name=dict(type='str',required=True),
            state=dict(type='str',
                       choices=['absent','present'],
                       default='present',
                       required=False),
            volume=dict(type='int',required=False)
        ),
        supports_check_mode=False
    )
    name=module.params['name']
    state=module.params['state']
    volume=module.params['volume']

    changed=False
    if check_status(module,name) != state:
        if state=='present':
            cmd=['qpkg_cli','-a',name]
        else:
            cmd=['qpkg_cli','-R',name]
        if volume is not None:
            cmd+=['-I',str(volume)]
        module.run_command(cmd)
        if check_status(module,name) != state:
            module.fail_json(msg='Install/Uninstall failed')
            return
        changed=True
    module.exit_json(changed=changed,
                     msg='QPKG '+name+' is '+state)

if __name__ == '__main__':
    main()

こうやって使う。

- name: install MusicStation
  qpkg:
    name: MusicStation
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?