将值从 CSV 文件传递到 Ansible 剧本
Passing Values from a CSV file into an Ansible playbook
下面的文件是我的剧本、库存和一个 CSV 文件(我的字典)
network_objects.yml
---
- hosts: all
connection: httpapi
gather_facts: False
vars_files:
- 'my_var.yml'
tasks:
- name: add-host
check_point.mgmt.cp_mgmt_host:
name: New Host 1
ip_address: 192.0.2.1
state: present
auto_publish_session: yes
- name: add-group
check_point.mgmt.cp_mgmt_group:
name: New Group 1
members:
- New Host 1
state: present
auto_publish_session: yes
my_var.yml
ansible_httpapi_validate_certs: False
ansible_httpapi_use_ssl: True
ansible_network_os: check_point.mgmt.checkpoint
ansible_python_interpreter: "python3"
ansible_user: admin
ansible_password: password
主机
[check_point]
checkpoint ansible_host=#IP address of checkpoint
users.csv
Name,IP Address,Comments
PCUSER1,10.11.92.44, Created by User Feb 19 2021
PCUSER2,10.11.12.13, Created by User 02/19/2012
readCSV.yml ---> 这会读取一个名为 users.csv
的 csv 文件
- name: "Check for CSV File"
hosts: localhost
gather_facts: no
tasks:
- name: Read CSV File and Return a dictionary
read_csv:
path: users.csv
key: name
register: user
- ansible.builtin.debug:
msg: '{{ user }}
我想达到的目标,
我希望能够读取我的 CSV 并按照 CSV 数据列出的顺序传递这些变量。然后我可以将新主机(包含所有名称、IP 地址、评论)发布到 Checkpoint
- name: add-host
check_point.mgmt.cp_mgmt_host:
name: {{ Get all Names from CSV File }}
ip_address: {{ Get all IP Addresses from CSV File }}
comments: {{ Get all Comments from CSV File }}
- name: add-host
check_point.mgmt.cp_mgmt_host:
name: {{ item.key }}
ip_address: {{ item.value[0]}}
comments: {{ item.value[1] }}
loop: "{{ user |dict2items}}"
我不确定是否物有所值。
我认为用户的输出是 { user:[ip_address,comments],...}
看你的结构字典,我觉得我的目的是好的,但要适应它。
来源https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#iterating-over-a-dictionary
下面的文件是我的剧本、库存和一个 CSV 文件(我的字典)
network_objects.yml
---
- hosts: all
connection: httpapi
gather_facts: False
vars_files:
- 'my_var.yml'
tasks:
- name: add-host
check_point.mgmt.cp_mgmt_host:
name: New Host 1
ip_address: 192.0.2.1
state: present
auto_publish_session: yes
- name: add-group
check_point.mgmt.cp_mgmt_group:
name: New Group 1
members:
- New Host 1
state: present
auto_publish_session: yes
my_var.yml
ansible_httpapi_validate_certs: False
ansible_httpapi_use_ssl: True
ansible_network_os: check_point.mgmt.checkpoint
ansible_python_interpreter: "python3"
ansible_user: admin
ansible_password: password
主机
[check_point]
checkpoint ansible_host=#IP address of checkpoint
users.csv
Name,IP Address,Comments
PCUSER1,10.11.92.44, Created by User Feb 19 2021
PCUSER2,10.11.12.13, Created by User 02/19/2012
readCSV.yml ---> 这会读取一个名为 users.csv
的 csv 文件- name: "Check for CSV File"
hosts: localhost
gather_facts: no
tasks:
- name: Read CSV File and Return a dictionary
read_csv:
path: users.csv
key: name
register: user
- ansible.builtin.debug:
msg: '{{ user }}
我想达到的目标,
我希望能够读取我的 CSV 并按照 CSV 数据列出的顺序传递这些变量。然后我可以将新主机(包含所有名称、IP 地址、评论)发布到 Checkpoint
- name: add-host
check_point.mgmt.cp_mgmt_host:
name: {{ Get all Names from CSV File }}
ip_address: {{ Get all IP Addresses from CSV File }}
comments: {{ Get all Comments from CSV File }}
- name: add-host
check_point.mgmt.cp_mgmt_host:
name: {{ item.key }}
ip_address: {{ item.value[0]}}
comments: {{ item.value[1] }}
loop: "{{ user |dict2items}}"
我不确定是否物有所值。 我认为用户的输出是 { user:[ip_address,comments],...} 看你的结构字典,我觉得我的目的是好的,但要适应它。
来源https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#iterating-over-a-dictionary