I'm trying to create sites in Netbox based on a CSV file. Since I'm adding an ID to the site name, I don't want the site to be created when there is no ID specified for the site. In this case, CHI and STO will be created but BER not.
I've tried to put
when: {{ item.ID }} not ''
before and after the loop int the second task but without success.
CSV:
sitename,ID,location
CHI,101,Chicago
BER,,Berlin
STO,103,Stockholm
Playbook:
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 }}"
CodePudding user response:
you have to write your test like this:
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 != ''
result:
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"
}