2
4

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 3 years have passed since last update.

AnsibleでExcelファイルからパラメータを抽出する

Last updated at Posted at 2020-04-03

はじめに

AnsibleにはCSVファイルのパラメータをリスト/辞書データとして抽出できるread_csvモジュールが存在します。
一方、Excelファイルから抽出できるモジュールは公式には無いのですが、GitHubにmamullen13316さんが公開しているxls_to_factsモジュールがあったので試してみました。

GitHub - mamullen13316/ansible_xls_to_facts

使い方

  • openpyxlをインストール
$ sudo pip install openpyxl
  • こちらを参考に、GitHubからのデータ取得、モジュールの格納、パスの追加を実施

Playbook

  • タスク(1)
    xls_to_factsモジュールのsrcアーギュメントで、Excelファイルの格納先を指定。
    今回は、GitHub上のサンプルを使わせて頂きました。
  • タスク(2)
    全データを表示。
  • タスク(3)
    json_queryフィルターを使い、ホスト名Switch-1の管理IPアドレスを表示。
playbook_xls_to_facts1.yml
---

- hosts: localhost
  gather_facts: no
  connection: local

  tasks:
    - name: read excel spreadsheet and return facts   # (1)
      xls_to_facts:
        src: example.xlsx
      register: result

    - name: display all facts   # (2)
      debug:
        msg: "{{ result }}"

    - name: display the ip address of specific hostname   # (3)
      debug:
        msg: "{{ result | json_query('ansible_facts.spreadsheet_Sheet1[?Hostname==`Switch-1`].Mgmt_ip') }}"

実行結果

タスク(2)を見ると、Excelの各シートのパラメータがspreadsheet_{シート名}の値として出力されています。
またタスク(3)で想定通りSwitch-1の管理IPアドレスが表示されました。

$ ansible-playbook playbook_xls_to_facts1.yml 
 [WARNING]: provided hosts list is empty, only localhost is available. Note that the
implicit localhost does not match 'all'


PLAY [localhost] ***************************************************************************

TASK [read excel spreadsheet and return a list] ********************************************
ok: [localhost]

TASK [display all facts] *******************************************************************
ok: [localhost] => {
    "msg": {
        "ansible_facts": {
            "spreadsheet_Sheet1": [
                {
                    "Hostname": "Switch-1",
                    "Mgmt_ip": "10.0.0.1"
                },
                {
                    "Hostname": "Switch-2",
                    "Mgmt_ip": "10.0.0.2"
                },
                {
                    "Hostname": "Switch-3",
                    "Mgmt_ip": "10.0.0.3"
                }
            ],
            "spreadsheet_Sheet2": [
                {
                    "Description": "To Spine-1",
                    "Interface": "Ethernet1/1",
                    "Interface_IP": "192.168.100.1/30"
                },
                {
                    "Description": "To Spine-2",
                    "Interface": "Ethernet1/2",
                    "Interface_IP": "192.168.100.5/30"
                }
            ]
        },
        "changed": false,
        "failed": false
    }
}

TASK [display the ip address of specific hostname] *****************************************
ok: [localhost] => {
    "msg": [
        "10.0.0.1"
    ]
}

PLAY RECAP *********************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 
2
4
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
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?