присоединение шаблона vnic ansible к политике подключения к локальной сети
-
Пример создания шаблона vnic и его привязки к политикам можно найти по адресу https://github.com/CiscoDevNet/intersight-ansible/blob/main/playbooks/intersight_vnic_template.yml . Для привязки шаблонов к политикам необходимо использовать ресурс массового API.
-
Это создает виртуальные сетевые карты, если они отсутствуют? Я получил свой плейбук для создания шаблонов виртуальных сетевых карт, но мне также нужно создать виртуальные сетевые карты из vmnic0-vmnic7.
-
Да, vnic_name в плейбуке будет создан, если он еще не существует.
-
Я не понимаю, почему задача по созданию виртуальных сетевых карт пропускается. Вот мой измененный плейбук. Шаблоны виртуальных сетевых карт создаются, но отсутствующие виртуальные сетевые карты: vmnic0-vmnic7 -
name
:
"Configure vNIC Templates"
vars
:
api_info
:
&
api_info_vnic api_private_key
:
"{{ api_private_key }}"
api_key_id
:
"{{ api_key_id }}"
api_uri
:
"{{ api_uri | default(omit) }}"
validate_certs
:
"{{ validate_certs | default(omit) }}"
state
:
"{{ state | default('present') }}"
block
:name
:
"Configure vNIC Templates in Intersight"
cisco
.
intersight
.
intersight_rest_api
:
<<
:
*
api_info_vnic resource_path
:
"/vnic/VnicTemplates"
query_params
:
$filter
:
"Name eq '{{ item.name }}'"
api_body
:
Name
:
"{{ item.name }}"
Organization
:
Moid
:
"{{ intersight_org.api_response.Moid }}"
Description
:
"vNIC Template for {{ item.name }}"
Cdn
:
Source
:
"vnic"
EnableOverride
:
false EthAdapterPolicy
:
Moid
:
"{{ ethernet_adapter_policy_details.api_response.Moid }}"
EthQosPolicy
:
Moid
:
"{{ ethernet_qos_policy_details.api_response.Moid }}"
FabricEthNetworkControlPolicy
:
Moid
:
"{{ ethernet_network_control_policy_details.api_response.Moid }}"
FabricEthNetworkGroupPolicy
:Moid
:
"{{ item.eth_network_group_policy }}"
FailoverEnabled
:
false MacPool
:
Moid
:
"{{ item.mac_pool }}"
SwitchId
:
"{{ item.switch_id }}"
Tags
:Key
:
"configmode"
Value
:
"ansible"Key
:
"prefix"
Value
:
"{{ prefix }}"
loop
:
"{{ vnic_templates }}"
loop_control
:
label
:
"{{ item.name }}"
register
:
template_respname
:
"Get LAN Connectivity Policy Moid"
cisco
.
intersight
.
intersight_rest_api
:
<<
:
*
api_info_vnic resource_path
:
/
vnic
/
LanConnectivityPolicies query_params
:
$filter
:
"Name eq '{{ name_of_fc_lan_connectivity_policy_biz }}'"
register
:
lan_respname
:
"Get Moid of vNICs by Name"
cisco
.
intersight
.
intersight_rest_api
:
<<
:
*
api_info_vnic resource_path
:
/
vnic
/
EthIfs query_params
:
$filter
:
"Name eq '{{ item.vnic }}'"
loop
:
"{{ vnic_templates }}"
loop_control
:
label
:
"{{ item.vnic }}"
register
:
vnic_respname
:
"Debug vnic_resp"
debug
:
var
:
vnic_respDerive profiles from template (if profiles don't already exist)
name
:
"POST to derive {{ item.vnic }}"
intersight_rest_api
:
resource_path
:
/
bulk
/
MoCloners update_method
:
post api_body
:
|
{
"Organization"
:
{
"Moid"
:
"{{ intersight_org.api_response.Moid }}"
}
,
"Sources"
:
[
{
"ClassId"
:
"mo.MoRef"
,
"ObjectType"
:
"vnic.VnicTemplate"
,
"Moid"
:
"{{ template_resp.api_response.Moid }}"
}
]
,
"Targets"
:
[
{
"Name"
:
"{{ item.vnic }}"
,
"LanConnectivityPolicy"
:
"{{ lan_resp.api_response.Moid }}"
,
"MacAddressType"
:
"POOL"
,
"ObjectType"
:
"vnic.EthIf"
,
"Placement"
:
{
}
,
"ClassId"
:
"vnic.EthIf"
}
]
}
when
:
vnic_resp
is
not
defined
or
vnic_resp
.
results
is
not
defined
or
vnic_resp
.
results
|
length0
or
vnic_resp
.
results
[
0
]
.
api_response
is
not
defined loop
:
"{{ vnic_templates }}"
loop_control
:
label
:
"{{ item.vnic }}" . -
Я запустил плейбук в автономном режиме, но получил следующую ошибку: fatal: [localhost]: FAILED! => {
"msg": "Условная проверка 'vnic_resp.api_response не определена или не является vnic_resp.api_response.Results' не прошла. Ошибка: ошибка при оценке условия (vnic_resp.api_response не определено или не является vnic_resp.api_response.Results): 'dict object' не имеет атрибута 'Results'\n\nОшибка, по-видимому, находится в 'create_vnics.yml': строка 100, столбец 7, но может\nбыть и в другом месте файла, в зависимости от точной синтаксической проблемы.\n\nСтрока, вызывающая ошибку, находится, по-видимому, здесь:\n\n # POST для получения vNIC '{{ vnic_name }}'\n - name: "POST для получения vNIC '{{ vnic_name }}'"\n ^ здесь\nВозможно, мы ошибаемся, но, по-видимому, проблема заключается в\nотсутствии кавычек. Всегда заключайте в кавычки скобки выражения шаблона, когда они\nначинают значение. Например:\n\n with_items:\n - {{ foo }}\n\nДолжно быть написано так:\n\n with_items:\n - "{{ foo }}"\n"
} -
Это сценарий, который я скопировал из репозитория github, но при выполнении задачи по созданию виртуальной сетевой карты постоянно возникает ошибка. -
name
:
"Configure vNIC from Template"
hosts
:
localhost connection
:
local gather_facts
:
false vars
:
api_info
:
&
api_info api_private_key
:
"ansible-automation-SecretKey.txt"
api_key_id
:
"xxxxxxxxxx api_uri
:
"https://cva"
validate_certs
:
false state
:
present prefix
:
"DC1"
org_name
:
"my_org"
template_name
:
"name_of_vnic_template_A"
lan_connectivity_policy
:
"fc_lan_connectivity_policy"
vnic_name
:
"vnic0"
mac_pool
:
"{{ prefix }}-Mac-Pool-A"
network_group
:
"{{ prefix }}-vSwitch0-Policy"
network_control_policy
:
"{{ prefix }}-Enable-CDP-LLDP-Policy"
qos_policy
:
"{{ prefix }}-EthernetQoS-Policy"
ethernet_adapter_policy
:
"{{ prefix }}-EthAdapter-VMware-Policy"
tasks
:name
:
"Get Organization Moid"
cisco
.
intersight
.
intersight_rest_api
:
<<
:
*
api_info resource_path
:
/
organization
/
Organizations query_params
:
$filter
:
"Name eq '{{ org_name }}'"
register
:
org_respname
:
"Get vNIC Template Moid"
cisco
.
intersight
.
intersight_rest_api
:
<<
:
*
api_info resource_path
:
/
vnic
/
VnicTemplates query_params
:
$filter
:
"Name eq '{{ template_name }}'"
register
:
template_respname
:
"Check if FC vNIC '{{ vnic_name }}' exists"
cisco
.
intersight
.
intersight_rest_api
:
<<
:
*
api_info resource_path
:
/
vnic
/
EthIfs query_params
:
$filter
:
"Name eq '{{ vnic_name }}'"
register
:
vnic_respname
:
"Get MAC Pool Moid"
cisco
.
intersight
.
intersight_rest_api
:
<<
:
*
api_info resource_path
:
/
macpool
/
Pools query_params
:
$filter
:
"Name eq '{{ mac_pool }}'"
register
:
mac_respname
:
"Get Network Group Moid"
cisco
.
intersight
.
intersight_rest_api
:
<<
:
*
api_info resource_path
:
/
fabric
/
EthNetworkGroupPolicies query_params
:
$filter
:
"Name eq '{{ network_group }}'"
register
:
network_respname
:
"Get Network Control Policy Moid"
cisco
.
intersight
.
intersight_rest_api
:
<<
:
*
api_info resource_path
:
/
fabric
/
EthNetworkControlPolicies query_params
:
$filter
:
"Name eq '{{ network_control_policy }}'"
register
:
control_respname
:
"Get QoS Policy Moid"
cisco
.
intersight
.
intersight_rest_api
:
<<
:
*
api_info resource_path
:
/
vnic
/
EthQosPolicies query_params
:
$filter
:
"Name eq '{{ qos_policy }}'"
register
:
qos_respname
:
"Get Ethernet Adapter Moid"
cisco
.
intersight
.
intersight_rest_api
:
<<
:
*
api_info resource_path
:
/
vnic
/
EthAdapterPolicies query_params
:
$filter
:
"Name eq '{{ ethernet_adapter_policy }}'"
register
:
adapter_respCheck if vNIC exists
name
:
"Get {{ vnic_name }} Moid"
cisco
.
intersight
.
intersight_rest_api
:
<<
:
*
api_info resource_path
:
/
vnic
/
EthIfs query_params
:
$filter
:
"Name eq '{{ vnic_name }}'"
register
:
vnic_respname
:
"Get LAN Connectivity Policy Moid"
cisco
.
intersight
.
intersight_rest_api
:
<<
:
*
api_info resource_path
:
/
vnic
/
LanConnectivityPolicies query_params
:
$filter
:
"Name eq '{{ lan_connectivity_policy }}'"
register
:
lan_respname
:
"POST to derive vNIC '{{ vnic_name }}'"
cisco
.
intersight
.
intersight_rest_api
:
<<
:
*
api_info resource_path
:
/
bulk
/
MoCloners update_method
:
post api_body
:
|
{
"Organization"
:
{
#"Moid": "{{ org_resp.api_response.Moid }}"
}
,
"Sources"
:
[
{
"ClassId"
:
"mo.MoRef"
,
"ObjectType"
:
"vnic.VnicTemplate"
,
Moid
:
"{{ template_resp.api_response.Moid }}"
}
]
,
"Targets"
:
[
{
"Name"
:
"{{ vnic_name }}"
,
"LanConnectivityPolicy"
:
"{{ lan_resp.api_response.Moid }}"
,
"MacAddressType"
:
"POOL"
,
"ObjectType"
:
"vnic.EthIf"
,
"Placement"
:
{
}
,
"ClassId"
:
"vnic.EthIf"
}
]
}
when
:
vnic_resp
.
api_response
is
not
defined
or
not
vnic_resp
.
api_responsename
:
"POST to update vNIC '{{ vnic_name }}'"
cisco
.
intersight
.
intersight_rest_api
:
<<
:
*
api_info resource_path
:
/
bulk
/
MoMergers update_method
:
post api_body
:
{
"Sources"
:
[
{
"ObjectType"
:
"vnic.VnicTemplate"
,
"Moid"
:
"{{ template_resp.api_response.Moid }}"
}
]
,
"Targets"
:
[
{
"ObjectType"
:
"vnic.EthIf"
,
"Moid"
:
"{{ vnic_resp.api_response.Moid }}"
}
]
,
"MergeAction"
:
"Replace"
}
when
:
vnic_resp
.
api_response
and
vnic_resp
.
changed fatal
:
[
localhost
]
:
FAILED!{
"msg"
:
"The task includes an option with an undefined variable.. 'dict object' has no attribute 'Moid'\n\nThe error appears to be in 'create_vnics.yml': line 107, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: "POST to derive vNIC '{{ vnic_name }}'"\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with_items:\n - {{ foo }}\n\nShould be written as:\n\n with_items:\n - "{{ foo }}"\n"
} -
Похоже, что условия в примере не будут работать во всех случаях, и запрос на существующий vnic должен быть более конкретным. Я поработаю над обновленным примером с исправлениями.
-
Я обновил пример на
https://github.com/CiscoDevNet/intersight-ansible/blob/main/playbooks/intersight_vnic_template.yml
, добавив более конкретный запрос и список vnics для создания или обновления из шаблона. -
Я смог создать шаблоны vnic непосредственно из своей политики подключения к локальной сети. Ваш плейбук привязывает vnic к существующему шаблону vnic?
-
Да, в примере vnics присоединяются к шаблону vnic, созданному в плейбуке.
-
все еще получаю ошибку # Проверить, существует ли vNIC
- name: «Получить {{ vnic_name }} Moid»
intersight_rest_api:
resource_path: /vnic/EthIfs
query_params:
$filter: "Name eq '{{ vnic_name }}' and LanConnectivityPolicy.Moid eq '{{ lan_resp.api_response.Moid }}'"
регистр: vnic_resp
цикл: "{{ vnic_names }}"
loop_control:
loop_var: vnic_name TASK
[
Get
00
vSwitch0
fc
A Moid
]
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
task path
:
create_vnics
.
yml
:
95
[
WARNING
]
:
TASK
:
Get
00vSwitch0
fc
A Moid
:
The loop variable
'vnic_name'
is
already
in
use
.
You should set theloop_varvalue
in
theloop_controloption
for
the task to something
else
to avoid variable collisions
and
unexpected behavior
.
fatal
:
[
localhost
]
:
FAILED!{
"msg"
:
"The task includes an option
with
an undefined variable
.
.
'dict object'
has no attribute
'Moid'
\n\nThe error appears to be
in
'create_vnics.yml'
:
line
95
,
column
7
,
but may\nbe elsewhere
in
the file depending on the exact syntax problem
.
\n\nThe offending line appears to be
:
\n\nCheck if vNIC exists\n - name: "Get {{ vnic_name }} Moid"\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with_items:\n - {{ foo }}\n\nShould be written as:\n\n with_items:\n - "{{ foo }}"\n"
- name: «Получить {{ vnic_name }} Moid»
-
Можете ли вы опубликовать полный плейбук, который вы используете? В примере из github vnic_names является списком в разделе vars, а loop_var, используемый при цикле в vnic_names, является vnic_name. Судя по вашей ошибке, vnic_name, похоже, используется где-то еще в вашем плейбуке.
-
name
:
"Configure vNICs from Template"
hosts
:
localhost connection
:
local gather_facts
:
false vars
:
api_info
:
&
api_info api_private_key
:
"ansible-automation-SecretKey.txt"
api_key_id
:
"xxxxxxxx"
api_uri
:
"https://intersight/api/v1"
validate_certs
:
false state
:
present prefix
:
"DC1"
org_name
:
"my-org"
template_name
:
"FAB-A-BIZ-MGMT"
lan_connectivity_policy
:
"lan_connectivity_policy"
mac_pool
:
"{{ prefix }}-Mac-Pool-A"
network_group
:
"{{ prefix }}-vSwitch0-Policy"
network_control_policy
:
"{{ prefix }}-Enable-CDP-LLDP-Policy"
qos_policy
:
"{{ prefix }}-EthernetQoS-Policy"
ethernet_adapter_policy
:
"{{ prefix }}-EthAdapter-VMware-Policy"
vnic_names
:"00-vSwitch0-A"
"01-vSwitch0-B"
tasks
:name
:
"Get Organization Moid"
cisco
.
intersight
.
intersight_rest_api
:
<<
:
*
api_info resource_path
:
/
organization
/
Organizations query_params
:
$filter
:
"Name eq '{{ org_name }}'"
register
:
org_respname
:
"Get MAC Pool Moid"
cisco
.
intersight
.
intersight_rest_api
:
<<
:
*
api_info resource_path
:
/
macpool
/
Pools query_params
:
$filter
:
"Name eq '{{ mac_pool }}'"
register
:
mac_respname
:
"Get Network Group Moid"
cisco
.
intersight
.
intersight_rest_api
:
<<
:
*
api_info resource_path
:
/
fabric
/
EthNetworkGroupPolicies query_params
:
$filter
:
"Name eq '{{ network_group }}'"
register
:
network_respname
:
"Get Network Control Policy Moid"
cisco
.
intersight
.
intersight_rest_api
:
<<
:
*
api_info resource_path
:
/
fabric
/
EthNetworkControlPolicies query_params
:
$filter
:
"Name eq '{{ network_control_policy }}'"
register
:
control_respname
:
"Get QoS Policy Moid"
cisco
.
intersight
.
intersight_rest_api
:
<<
:
*
api_info resource_path
:
/
vnic
/
EthQosPolicies query_params
:
$filter
:
"Name eq '{{ qos_policy }}'"
register
:
qos_respname
:
"Get Ethernet Adapter Moid"
cisco
.
intersight
.
intersight_rest_api
:
<<
:
*
api_info resource_path
:
/
vnic
/
EthAdapterPolicies query_params
:
$filter
:
"Name eq '{{ ethernet_adapter_policy }}'"
register
:
adapter_respname
:
"Get vNIC Template Moid"
cisco
.
intersight
.
intersight_rest_api
:
<<
:
*
api_info resource_path
:
/
vnic
/
VnicTemplates query_params
:
$filter
:
"Name eq '{{ template_name }}'"
register
:
template_respname
:
"Get LAN Connectivity Policy Moid"
cisco
.
intersight
.
intersight_rest_api
:
<<
:
*
api_info resource_path
:
/
vnic
/
LanConnectivityPolicies query_params
:
$filter
:
"Name eq '{{ lan_connectivity_policy }}'"
register
:
lan_respname
:
"Check if each vNIC exists"
cisco
.
intersight
.
intersight_rest_api
:
<<
:
*
api_info resource_path
:
/
vnic
/
EthIfs query_params
:
$filter
:
"Name eq '{{ item }}' and LanConnectivityPolicy.Moid eq '{{ lan_resp.api_response.Moid }}'"
loop
:
"{{ vnic_names }}"
loop_control
:
label
:
"{{ item }}"
register
:
vnic_checksname
:
"POST to derive vNIC if it does not exist"
cisco
.
intersight
.
intersight_rest_api
:
<<
:
*
api_info resource_path
:
/
bulk
/
MoCloners update_method
:
post api_body
:{
{
{
"Organization"
:
{
"Moid"
:
org_resp
.
api_response
.
Moid
}
,
"Sources"
:
[
{
"ClassId"
:
"mo.MoRef"
,
"ObjectType"
:
"vnic.VnicTemplate"
,
"Moid"
:
template_resp
.
api_response
.
Moid
}
]
,
"Targets"
:
[
{
"Name"
:
item
.
item
,
"LanConnectivityPolicy"
:
lan_resp
.
api_response
.
Moid
,
"MacAddressType"
:
"POOL"
,
"ObjectType"
:
"vnic.EthIf"
,
"Placement"
:
{
}
,
"ClassId"
:
"vnic.EthIf"
}
]
}
}
}
when
:
item
.
api_response
.
Moid
is
not
defined loop
:
"{{ vnic_checks.results }}"
loop_control
:
label
:
"{{ item.item }}"name
:
"POST to update vNIC if it exists and template changed"
cisco
.
intersight
.
intersight_rest_api
:
<<
:
*
api_info resource_path
:
/
bulk
/
MoMergers update_method
:
post api_body
:{
{
{
"Sources"
:
[
{
"ObjectType"
:
"vnic.VnicTemplate"
,
"Moid"
:
template_resp
.
api_response
.
Moid
}
]
,
"Targets"
:
[
{
"ObjectType"
:
"vnic.EthIf"
,
"Moid"
:
item
.
api_response
.
Moid
}
]
,
"MergeAction"
:
"Replace"
}
}
}
when
:
item
.
api_response
.
Moid
is
defined
and
template_resp
.
changed loop
:
"{{ vnic_checks.results }}"
loop_control
:
label
:
"{{ item.item }}"
Здравствуйте! Похоже, вам интересна эта беседа, но у вас пока нет учетной записи.
Вы устали просматривать одни и те же посты каждый раз, когда заходите на сайт? После регистрации, вам не придётся искать обсуждения в которых вы принимали участие, настройте уведомления о новых сообщениях так как вам это удобно (по электронной почте или уведомлением). У вас появится возможность сохранять закладки и ставить лайки постам, чтобы выразить свою благодарность другим участникам сообщества.
С вашими комментариями этот пост может стать ещё лучше 💗
Зарегистрироваться Войти