今回は Cisco ACI の設定を Ansible で自動化する手順をメモしておきます。 尚、サンプルとして理解しやすいよう、Playbook は「ひとつの機能毎に分割」しています。
目次
テスト環境
今回は下記の環境でテストしました。
- ACI 4.2(2e)
- CentOS8
- Ansible 2.8.5
ゴール
Ansible で以下の構成を設定します。 Physical Domain で設定することも出来ますが、Ansible のネットワークモジュールである aci_epg_to_domain ページにあるサンプルが Physical Domain になっていたので、今回は敢えて VMM Domain で設定してみました。
Ansible のインストール
今回は CentOS8 に pip
で Ansible をインストールしました。
pip install ansible
インベントリファイルの用意
インベントリファイルは以下の内容で用意しました。 機能毎で Playbook を分割してしまったので、APIC のアドレス・ユーザ名・パスワードは個々の Playbook で指定するのでは無く、インベントリファイル中で変数として定義しました。
[all] 127.0.0.1 [all:vars] address=10.0.0.1 username=admin password=password
Ansible で設定する
ここから Ansible を使って設定していきます。 以降は Playbook のみを掲載していきます。 Playbook の実行は以下のようにします。
ansible-playbook --inventory [INVENTORY] [PLAYBOOK]
例えばインベントリファイルが「inventory
」、Playbook が「create_tenant.yml
」だとすると、以下のように実行します。
ansible-playbook --inventory inventory create_tenant.yml
Step.1 Tenant の作成
--- - name: Using Ansible for Cisco ACI Automation hosts: all connection: local gather_facts: no tasks: - name: Add a new Tenant. aci_tenant: hostname: "{{ address }}" username: "{{ username }}" password: "{{ password }}" tenant: Tenant-01 state: present validate_certs: false
Step.2 VRF の作成
--- - name: Using Ansible for Cisco ACI Automation hosts: all connection: local gather_facts: no tasks: - name: Add a new VRF. aci_vrf: hostname: "{{ address }}" username: "{{ username }}" password: "{{ password }}" tenant: Tenant-01 vrf: Vrf-01 policy_control_preference: enforced policy_control_direction: ingress state: present validate_certs: false
Step.3 Contract の作成
--- - name: Using Ansible for Cisco ACI Automation hosts: all connection: local gather_facts: no tasks: - name: Add a new Contract. aci_contract: hostname: "{{ address }}" username: "{{ username }}" password: "{{ password }}" tenant: Tenant-01 contract: Contract-01 state: present validate_certs: false
Step.4 Subject の作成
--- - name: Using Ansible for Cisco ACI Automation hosts: all connection: local gather_facts: no tasks: - name: Add a new Subject. aci_contract_subject: hostname: "{{ address }}" username: "{{ username }}" password: "{{ password }}" tenant: Tenant-01 contract: Contract-01 subject: Subject-01 state: present validate_certs: false
Step.5 Filter を Subject に関連付ける
Filter はデフォルトで common
テナントに定義されている default
を使いました。
--- - name: Using Ansible for Cisco ACI Automation hosts: all connection: local gather_facts: no tasks: - name: Add a Subject to Filter binding. aci_contract_subject_to_filter: hostname: "{{ address }}" username: "{{ username }}" password: "{{ password }}" tenant: Tenant-01 contract: Contract-01 subject: Subject-01 filter: default state: present validate_certs: false
Step.6 BD (Bridge Domain) の作成
BD の作成時に、同時に Subnet は定義出来ない為、まず BD だけ作成します。
--- - name: Using Ansible for Cisco ACI Automation hosts: all connection: local gather_facts: no tasks: - name: Add a new BD-01. aci_bd: hostname: "{{ address }}" username: "{{ username }}" password: "{{ password }}" tenant: Tenant-01 bd: Bd-01 vrf: Vrf-01 state: present validate_certs: false - name: Add a new BD-02. aci_bd: hostname: "{{ address }}" username: "{{ username }}" password: "{{ password }}" tenant: Tenant-01 bd: Bd-02 vrf: Vrf-01 state: present validate_certs: false
Step.7 BD へ Subnet の追加
続いて BD へ Subnet を追加します。
--- - name: Using Ansible for Cisco ACI Automation hosts: all connection: local gather_facts: no tasks: - name: Add a new Subnet-01. aci_bd_subnet: hostname: "{{ address }}" username: "{{ username }}" password: "{{ password }}" tenant: Tenant-01 bd: Bd-01 gateway: 192.168.1.254 mask: 24 state: present validate_certs: false - name: Add a new Subnet-02. aci_bd_subnet: hostname: "{{ address }}" username: "{{ username }}" password: "{{ password }}" tenant: Tenant-01 bd: Bd-02 gateway: 192.168.2.254 mask: 24 state: present validate_certs: false
Step.8 Application Profile の作成
--- - name: Using Ansible for Cisco ACI Automation hosts: all connection: local gather_facts: no tasks: - name: Add a new Application Profile. aci_ap: hostname: "{{ address }}" username: "{{ username }}" password: "{{ password }}" tenant: Tenant-01 ap: Ap-01 state: present validate_certs: false
Step.9 EPG (End Point Group) の作成
EPG の作成時に Domain や Contract を関連付けることが出来ない為、まず EPG だけ作成します。
--- - name: Using Ansible for Cisco ACI Automation hosts: all connection: local gather_facts: no tasks: - name: Add a new EPG-01. aci_epg: hostname: "{{ address }}" username: "{{ username }}" password: "{{ password }}" tenant: Tenant-01 ap: Ap-01 bd: Bd-01 epg: Epg-01 state: present validate_certs: false - name: Add a new EPG-02. aci_epg: hostname: "{{ address }}" username: "{{ username }}" password: "{{ password }}" tenant: Tenant-01 ap: Ap-01 bd: Bd-02 epg: Epg-02 state: present validate_certs: false
Step.10 EPG へ VMM Domain を関連付ける
次に先ほど作成した EPG へ VMM Domain を関連付けます。 domain_type
を phys
にすれば Physical Domain を関連付けることも出来ます。 Physical Domain であれば vm_provider
を指定する必要はありません (指定するとエラーになります)。
--- - name: Using Ansible for Cisco ACI Automation hosts: all connection: local gather_facts: no tasks: - name: Add a new VMM Domain to EPG-01 binding. aci_epg_to_domain: hostname: "{{ address }}" username: "{{ username }}" password: "{{ password }}" tenant: Tenant-01 ap: Ap-01 epg: Epg-01 domain: VmmDomain-01 domain_type: vmm vm_provider: vmware state: present validate_certs: false - name: Add a new VMM Domain to EPG-02 binding. aci_epg_to_domain: hostname: "{{ address }}" username: "{{ username }}" password: "{{ password }}" tenant: Tenant-01 ap: Ap-01 epg: Epg-02 domain: VmmDomain-01 domain_type: vmm vm_provider: vmware state: present validate_certs: false
Step.11 EPG へ Contract を関連付ける
最後に EPG へ Contract を関連付けます。 今回は Epg-01
を Consumer として、Epg-02
を Provider として設定しています。
--- - name: Using Ansible for Cisco ACI Automation hosts: all connection: local gather_facts: no tasks: - name: Add a new Contract to EPG-01 binding (Consumer). aci_epg_to_contract: hostname: "{{ address }}" username: "{{ username }}" password: "{{ password }}" tenant: Tenant-01 ap: Ap-01 epg: Epg-01 contract: Contract-01 contract_type: consumer state: present validate_certs: false - name: Add a new Contract to EPG-01 binding (Consumer). aci_epg_to_contract: hostname: "{{ address }}" username: "{{ username }}" password: "{{ password }}" tenant: Tenant-01 ap: Ap-01 epg: Epg-02 contract: Contract-01 contract_type: provider state: present validate_certs: false