LoginSignup
0
1

More than 3 years have passed since last update.

AnsibleでWindows向け自作module_utilsの動作確認メモ

Posted at

AnsibleのWindows向け自作モジュールを作る時に共通処理はmodule_utilsにまとめておきたいと思って簡単な動作確認メモです。

各モジュール

動作確認する例モジュールは以下のものです。

example.ps1
#Requires -Module Ansible.ModuleUtils.ExampleUtil
#AnsibleRequires -CSharpUtil Ansible.Basic

$spec = @{
    options = @{
        name = @{ type="str"; required=$true; }
    }
    supports_check_mode = $false
}
$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec)

$r = ReturnTest $module.Params.name

$module.Result.value = $r
$module.ExitJson()

動作確認するmodule_utilsは以下のものです。

Ansible.ModuleUtils.ExampleUtil.psm1
Function ReturnTest($arg) {
    <#
    .SYNOPSIS
    Function that just return arg
    #>

    return $arg
}

単純に name 引数で渡された値が RetrunTest 関数で戻されて戻り値に入るだけの単純なものです。

ディレクトリ構造は以下のようになっています。

(venv) [root@localhost test]# tree .
.
├── inventory
├── library
│   └── example.ps1
├── main.yml
└── module_utils
    └── Ansible.ModuleUtils.ExampleUtil.psm1

Playbook

main.ymlは以下のように書いています。

main.yml
---
- name: Example modul_utils test
  hosts: windows
  gather_facts: no
  tasks:
    - name: example module test
      example:
        name: "Hello, World"
      register: r

    - debug: var=r

実行

実行してみます。

(venv) [root@localhost test]# ansible-playbook -i inventory main.yml

PLAY [Example modul_utils test] *************************************************************************************************************************************

TASK [example module test] ******************************************************************************************************************************************
ok: [windows01]

TASK [debug] ********************************************************************************************************************************************************
ok: [windows01] => {
    "r": {
        "changed": false,
        "failed": false,
        "value": "Hello, World"
    }
}

PLAY RECAP **********************************************************************************************************************************************************
windows01                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

module_utils にある Ansible.ModuleUtils.ExampleUtil.psm1ReturnTest 関数が呼び出されて問題なく実行できました。
こんな感じで module_utils は作れそう。

0
1
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
1