使用 Ansible 从 csv 在 Netbox 中创建站点

Create site in Netbox from csv with Ansible

我正在尝试基于 CSV 文件在 Netbox 中创建网站。因为我在站点名称中添加了一个 ID,所以我不希望在没有为站点指定 ID 时创建该站点。在这种情况下,将创建 CHI 和 STO,但不会创建 BER。

我试过

when: {{ item.ID }} not ''

在第二个任务循环之前和之后都没有成功。

CSV:

sitename,ID,location
CHI,101,Chicago
BER,,Berlin
STO,103,Stockholm

剧本:

tasks:
  - name: "read sites from csv"
    community.general.read_csv:
      path: sites.csv
      delimiter: ','
    register: sitelist
    delegate_to: localhost

  - name: "create/update sites in netbox"
    netbox.netbox.netbox_site:
      netbox_url: https://url
      netbox_token: token
      data:
        name: "{{ item.sitename }}-{{ item.ID}}"
        physical_address: "{{ item.location }}"
    loop: "{{ sitelist.list }}"

你必须这样写你的测试:

  tasks:
  - name: "read sites from csv"
    community.general.read_csv:
      path: sites.csv
      delimiter: ','
    register: sitelist
    delegate_to: localhost

  - debug:
      msg: "name: {{ item.sitename }}-{{ item.ID}}, physical_address: {{ item.location }}"
    loop: "{{ sitelist.list }}"
    when: item.ID != ''

结果:

ok: [localhost] => (item={'sitename': 'CHI', 'ID': '101', 'location': 'Chicago'}) => {
    "msg": "name: CHI-101, physical_address: Chicago"
}
skipping: [localhost] => (item={'sitename': 'BER', 'ID': '', 'location': 'Berlin'}) 
ok: [localhost] => (item={'sitename': 'STO', 'ID': '103', 'location': 'Stockholm'}) => {
    "msg": "name: STO-103, physical_address: Stockholm"
}