ロール内でディレクティブを利用して変数やターゲットノードをコントロールする

はじめに

ロールやタスクレベルで変数やターゲットノードをコントロールしたい場合、ディレクティブで結構柔軟に対応できます。下記に、Play、Role、Block、Taskに利用可能なディレクティブの一覧が記載されています。

docs.ansible.com

以下、簡単な検証結果です。

Playbook

  • ロールの呼び出し元

hostsCisco DevNet SandboxのCSRを指定しています。

---
- hosts: csr
  gather_facts: false
  collections:
    - cisco.ios
  tasks:
    - name: import debug role
      import_role:
        name: debug
  • ロール(roles/debug/tasks/main.yml)
---
################################################################################
# csrタスク
- name: show version
  ios_command:
    commands:
      - show version
  register: res_show_version

- name: debug show version
  debug:
    var: res_show_version.stdout_lines

################################################################################
# connection: localなタスク
- name: show ansible's version
  connection: local
  command: ansible --version
  register: res_command_module

- name: debug ansible's version
  debug:
    var: res_command_module.stdout_lines

################################################################################
# blockとvarsの組み合わせ
- name: block
  vars:
    var1: hoge
    var2: piyo
  block:
    - name: debug var1
      debug:
        var: var1

    - name: debug var2
      debug:
        var: var2

実行結果

# ansible-playbook -i inventory.ini debug.yml 

PLAY [csr] ************************************************************************************************************************************************************************************

TASK [debug : show version] *******************************************************************************************************************************************************************
ok: [ios-xe-mgmt-latest.cisco.com]

TASK [debug show version] *********************************************************************************************************************************************************************
ok: [ios-xe-mgmt-latest.cisco.com] => {
    "res_show_version.stdout_lines": [
        [
            "Cisco IOS XE Software, Version 16.11.01a",
            "Cisco IOS Software [Gibraltar], Virtual XE Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 16.11.1a, RELEASE SOFTWARE (fc1)",
            "Technical Support: http://www.cisco.com/techsupport",
            "Copyright (c) 1986-2019 by Cisco Systems, Inc.",
            "Compiled Thu 11-Apr-19 23:59 by mcpre",
            "",
            "",
            "Cisco IOS-XE software, Copyright (c) 2005-2019 by cisco Systems, Inc.",
            "All rights reserved.  Certain components of Cisco IOS-XE software are",
            "licensed under the GNU General Public License (\"GPL\") Version 2.0.  The",
            "software code licensed under GPL Version 2.0 is free software that comes",
            "with ABSOLUTELY NO WARRANTY.  You can redistribute and/or modify such",
            "GPL code under the terms of GPL Version 2.0.  For more details, see the",
            "documentation or \"License Notice\" file accompanying the IOS-XE software,",
            "or the applicable URL provided on the flyer accompanying the IOS-XE",
            "software.",
            "",
            "",
            "ROM: IOS-XE ROMMON",
            "",
            "csr1000v-1 uptime is 1 day, 11 hours, 9 minutes",
            "Uptime for this control processor is 1 day, 11 hours, 10 minutes",
            "System returned to ROM by reload",
            "System image file is \"bootflash:packages.conf\"",
            "Last reload reason: reload",
            "",
            "",
            "",
            "This product contains cryptographic features and is subject to United",
            "States and local country laws governing import, export, transfer and",
            "use. Delivery of Cisco cryptographic products does not imply",
            "third-party authority to import, export, distribute or use encryption.",
            "Importers, exporters, distributors and users are responsible for",
            "compliance with U.S. and local country laws. By using this product you",
            "agree to comply with applicable laws and regulations. If you are unable",
            "to comply with U.S. and local laws, return this product immediately.",
            "",
            "A summary of U.S. laws governing Cisco cryptographic products may be found at:",
            "http://www.cisco.com/wwl/export/crypto/tool/stqrg.html",
            "",
            "If you require further assistance please contact us by sending email to",
            "export@cisco.com.",
            "",
            "License Level: ax",
            "License Type: N/A(Smart License Enabled)",
            "Next reload license Level: ax",
            "",
            "",
            "Smart Licensing Status: UNREGISTERED/No Licenses in Use",
            "",
            "cisco CSR1000V (VXE) processor (revision VXE) with 2378575K/3075K bytes of memory.",
            "Processor board ID 9PBZVGDVBQ2",
            "3 Gigabit Ethernet interfaces",
            "32768K bytes of non-volatile configuration memory.",
            "8112832K bytes of physical memory.",
            "7774207K bytes of virtual hard disk at bootflash:.",
            "0K bytes of WebUI ODM Files at webui:.",
            "",
            "Configuration register is 0x2102"
        ]
    ]
}

TASK [debug : show ansible's version] *********************************************************************************************************************************************************
changed: [ios-xe-mgmt-latest.cisco.com]

TASK [debug ansible's version] ****************************************************************************************************************************************************************
ok: [ios-xe-mgmt-latest.cisco.com] => {
    "res_command_module.stdout_lines": [
        "ansible 2.10.0",
        "  config file = /root/ansible/ansible.cfg",
        "  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']",
        "  ansible python module location = /usr/local/lib/python3.6/site-packages/ansible",
        "  executable location = /usr/local/bin/ansible",
        "  python version = 3.6.8 (default, Apr 16 2020, 01:36:27) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]"
    ]
}

TASK [debug var1] *****************************************************************************************************************************************************************************
ok: [ios-xe-mgmt-latest.cisco.com] => {
    "var1": "hoge"
}

TASK [debug var2] *****************************************************************************************************************************************************************************
ok: [ios-xe-mgmt-latest.cisco.com] => {
    "var2": "piyo"
}

PLAY RECAP ************************************************************************************************************************************************************************************
ios-xe-mgmt-latest.cisco.com : ok=6    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0