关于提高我的剧本效率的建议?

时间:2017-09-19 16:06:29

标签: ansible cisco

开始尝试Ansible并使用playbooks自动执行网络设备上的一些例行任务。我能够得到一些基本的东西在这个过程中工作和学习,但我知道我的知识是有限的,所以当我看到这本剧本和多少东西似乎多余时我不得不假设有更好的方法来消除一些冗余和制作东西更清洁,更有效率。

示例我想尝试使用和解释,以获得一些关于在一组设备上配置新vlan的想法。

通常首先需要在两个分布交换机上配置新的vlan,然后在这两个交换机上有特定的接口,我们必须将vlan添加到。

所以,对于第一部分,我将两个主机放在一个名为" dist"在我的主机文件中:

[dist]
DIST01 ansible_host=10.10.1.1
DIST02 ansible_host=10.10.1.2

然后我在我的剧本中创建了以下内容:

- name: Add Heartbeat VLAN to DIST
  hosts: dist
  connection: local
  gather_facts: no

  tasks:
  - name: Include Login Credentials
    include_vars: secrets.yml

  - name: Define Provider
    set_fact:
      provider:
        host: "{{ ansible_host }}"
        username: "{{ creds['username'] }}"
        password: "{{ creds['password'] }}"

  tasks:
    - name: Ensure VLAN Exists
      provider: "{{ provider }}"
      nxos_vlan: vlan_id="2600" state=present host={{ ansible_host }}

    - name: Ensure VLAN Name Configured
      provider: "{{ provider }}"
      nxos_vlan: vlan_id={{ item.vid }} name={{ item.name }} host={{ ansible_host }} state=present
      with_items:
        - { vid: 2600, name: Ansible Heartbeat VLAN }

    - name: ASSIGN VLAN TO TRUNK PORTS
      nxos_switchport:
        interface: "{{ item.interface }}"
        mode: trunk
        trunk_vlans: "{{ item.vlan }}"
        provider: "{{ provider }}"
      with_items: 
      - { interface: po850, vlan: 2600 }
      - { interface: po860, vlan: 2600 }
      - { interface: po865, vlan: 2600 }
      - { interface: po868, vlan: 2600 }
      - { interface: po871, vlan: 2600 }
      - { interface: po872, vlan: 2600 }
      - { interface: po875, vlan: 2600 }
      - { interface: po877, vlan: 2600 }
      - { interface: po884, vlan: 2600 }

因此,对于该组中的每个主机,它会遍历一个接口/端口列表并添加指定的vlan。

问题#1。 第一件事就是效率低下"在我的脑海里,我不相信必须指明" vlan:2600"每个地方。

我认为我应该将vlan设置为变量某些位置(在playbook中?在其他一些被调用的文件中?),以便在需要它的情况下使用。

下一组任务:

在上一个任务之后,下一个任务要求我们连接到需要部署vlan的每个接入交换机,并在那里配置新的vlan。

我遇到的问题是每个交换机上的端口通道是一个不同的接口#。所以我不能通过遍历设备列表来应用相同的配置。

例如我要做的就是这样:

host: ACCESS01    interface: po850    vlan: 2600
host: ACCESS02    interface: po860    vlan: 2600
host: ACCESS03    interface: po870    vlan: 2600

因此,对于每个主机/交换机,您将vlan添加到与该交换机关联的接口。

我刚为每个设备创建了一个新任务,指定了为该交换机配置的接口。

示例:

- name: Add Heartbeat VLAN to ACCESS01
  hosts: ACCESS01
  connection: local
  gather_facts: no

  tasks:
  - name: Include Login Credentials
    include_vars: secrets.yml

  - name: Define Provider
    set_fact:
      provider:
        host: "{{ ansible_host }}"
        username: "{{ creds['username'] }}"
        password: "{{ creds['password'] }}"

  tasks:
    - name: Ensure VLAN Exists
      provider: "{{ provider }}"
      nxos_vlan: vlan_id="2600" state=present host={{ ansible_host }}

    - name: Ensure VLAN Name Configured
      provider: "{{ provider }}"
      nxos_vlan: vlan_id={{ item.vid }} name={{ item.name }} host={{ ansible_host }} state=present
      with_items:
        - { vid: 2600, name: Ansible Heartbeat VLAN }

    - name: ASSIGN VLAN TO PORTS
      nxos_switchport:
        interface: "{{ item.interface }}"
        mode: trunk
        trunk_vlans: "{{ item.vlan }}"
        provider: "{{ provider }}"
      with_items: 
      - { interface: po850, vlan: 2600 }


- name: Add Heartbeat VLAN to ACCESS02
  hosts: ACCESS02
  connection: local
  gather_facts: no

  tasks:
  - name: Include Login Credentials
    include_vars: secrets.yml

  - name: Define Provider
    set_fact:
      provider:
        host: "{{ ansible_host }}"
        username: "{{ creds['username'] }}"
        password: "{{ creds['password'] }}"

  tasks:
    - name: Ensure VLAN Exists
      provider: "{{ provider }}"
      nxos_vlan: vlan_id="2600" state=present host={{ ansible_host }}

    - name: Ensure VLAN Name Configured
      provider: "{{ provider }}"
      nxos_vlan: vlan_id={{ item.vid }} name={{ item.name }} host={{ ansible_host }} state=present
      with_items:
        - { vid: 2600, name: Ansible Heartbeat VLAN }

    - name: ASSIGN VLAN TO PORTS
      nxos_switchport:
        interface: "{{ item.interface }}"
        mode: trunk
        trunk_vlans: "{{ item.vlan }}"
        provider: "{{ provider }}"
      with_items: 
      - { interface: po860, vlan: 2600 }

- name: Add Heartbeat VLAN to ACCESS03
  hosts: ACCESS03
  connection: local
  gather_facts: no

  tasks:
  - name: Include Login Credentials
    include_vars: secrets.yml

  - name: Define Provider
    set_fact:
      provider:
        host: "{{ ansible_host }}"
        username: "{{ creds['username'] }}"
        password: "{{ creds['password'] }}"

  tasks:
    - name: Ensure VLAN Exists
      provider: "{{ provider }}"
      nxos_vlan: vlan_id="2600" state=present host={{ ansible_host }}

    - name: Ensure VLAN Name Configured
      provider: "{{ provider }}"
      nxos_vlan: vlan_id={{ item.vid }} name={{ item.name }} host={{ ansible_host }} state=present
      with_items:
        - { vid: 2600, name: Ansible Heartbeat VLAN }

    - name: ASSIGN VLAN TO PORTS
      nxos_switchport:
        interface: "{{ item.interface }}"
        mode: trunk
        trunk_vlans: "{{ item.vlan }}"
        provider: "{{ provider }}"
      with_items: 
      - { interface: po870, vlan: 2600 }

所以你看...我知道当我看到几乎相同的东西一遍又一遍地重复时,我不得不假设有一种更好的方法而且我还不知道自己能解决的问题。< / p>

问题#2 。我怀疑有更好的方法来处理剧本中每个任务重复以下内容:

tasks:
      - name: Include Login Credentials
        include_vars: secrets.yml

      - name: Define Provider
        set_fact:
          provider:
            host: "{{ ansible_host }}"
            username: "{{ creds['username'] }}"
            password: "{{ creds['password'] }}"

问题#3 ,我是否可能只是在Playbook或其他文件中列出这些数据,然后创建一个可以遍历数据以确定需要哪个端口的任务构造

host: ACCESS01    interface: po850    vlan: 2600
host: ACCESS02    interface: po860    vlan: 2600
host: ACCESS03    interface: po870    vlan: 2600

在我看来,某种逻辑就像是,如果&#34;主持人&#34;等于&#34; ACCESS01&#34;接口等于po850。

因此,任务可能只是引用根据其当前正在处理的主机填充的变量?

非常感谢任何关于改进剧本和我的知识的想法和建议。我想我会找到最多的&#34; ansiblistic&#34;实现这一目标的方法。那不是一个字吧?

1 个答案:

答案 0 :(得分:0)

对于问题#1,您可以这样使用:

- { interface: po850, vlan: 2700 }

如果要将不同的vlan分配给一个或多个界面,则可以这样使用:

list_input = raw_input('Enter list of numbers----> ')

# Using list comprehension separate the string by commas, then convert to an integer.
int_list = [int(i) for i in list_input.split(',')]

print int_list

希望能帮助你。