TaskExecutorのクラス内でメソッドを下記の順で呼んでいる。
run ⇒ _run_loop ⇒ _execute ⇒ _get_action_handler ⇒ _get_action_handler_with_module_context
そして_get_action_handler_with_module_contextの内容は下記
def_get_action_handler_with_module_context(self, templar):
''' Returns the correct action plugin to handle the requestion task action and the module context '''
module_collection, separator, module_name = self._task.action.rpartition(".")
module_prefix = module_name.split('_')[0]
if module_collection:
# For network modules, which look for one action plugin per platform, look for the# action plugin in the same collection as the module by prefixing the action plugin# with the same collection.
network_action = "{0}.{1}".format(module_collection, module_prefix)
else:
network_action = module_prefix
collections = self._task.collections
# Check if the module has specified an action handler
module = self._shared_loader_obj.module_loader.find_plugin_with_context(
self._task.action, collection_list=collections
)
ifnot module.resolved ornot module.action_plugin:
module = Noneif module isnotNone:
handler_name = module.action_plugin
# let action plugin override module, fallback to 'normal' action plugin otherwiseelif self._shared_loader_obj.action_loader.has_plugin(self._task.action, collection_list=collections):
handler_name = self._task.action
elifall((module_prefix in C.NETWORK_GROUP_MODULES, self._shared_loader_obj.action_loader.has_plugin(network_action, collection_list=collections))):
handler_name = network_action
display.vvvv("Using network group action {handler} for {action}".format(handler=handler_name,
action=self._task.action),
host=self._play_context.remote_addr)
else:
# use ansible.legacy.normal to allow (historic) local action_plugins/ override without collections search
handler_name = 'ansible.legacy.normal'
collections = None# until then, we don't want the task's collection list to be consulted; use the builtin# networking/psersistent connections handlingifany(((self._connection.supports_persistence and C.USE_PERSISTENT_CONNECTIONS), self._connection.force_persistence)):
# check handler in case we dont need to do all the work to setup persistent connection
handler_class = self._shared_loader_obj.action_loader.get(handler_name, class_only=True)
ifgetattr(handler_class, '_requires_connection', True):
# for persistent connections, initialize socket path and start connection manager
self._play_context.timeout = self._connection.get_option('persistent_command_timeout')
display.vvvv('attempting to start connection', host=self._play_context.remote_addr)
display.vvvv('using connection plugin %s' % self._connection.transport, host=self._play_context.remote_addr)
options = self._connection.get_options()
socket_path = start_connection(self._play_context, options, self._task._uuid)
display.vvvv('local domain socket path is %s' % socket_path, host=self._play_context.remote_addr)
setattr(self._connection, '_socket_path', socket_path)
else:
# TODO: set self._connection to dummy/noop connection, using local for now
self._connection = self._get_connection({}, templar, 'local')
handler = self._shared_loader_obj.action_loader.get(
handler_name,
task=self._task,
connection=self._connection,
play_context=self._play_context,
loader=self._loader,
templar=templar,
shared_loader_obj=self._shared_loader_obj,
collection_list=collections
)
ifnot handler:
raise AnsibleError("the handler '%s' was not found" % handler_name)
return handler, module
name: aws-test
runtime: yaml
description: A minimal AWS Pulumi YAML program
outputs: # Export the name of the bucketbucketName: ${my-bucket.id}
resources: # Create an AWS resource (S3 Bucket)my-bucket:type: aws:s3:Bucket
name: aws-apigateway-py-routes
runtime:name: python
options:virtualenv: venv
description: Demonstration of API Gateway routes
template:config:aws:region:description: The AWS region to deploy into
default: us-east-2
jcluser@vMX2> show route
inet.0: 16 destinations, 16 routes (16 active, 0 holddown, 0 hidden)
+ = Active Route, - = Last Active, * = Both
0.0.0.0/0 *[Static/5] 00:46:44
> to 100.123.0.1 via fxp0.0
10.100.12.0/24 *[Direct/0] 00:41:52
> via ge-0/0/3.0
10.100.12.2/32 *[Local/0] 00:41:52
Local via ge-0/0/3.0
10.100.13.0/24 *[OSPF/10] 00:40:51, metric 2
> to 10.100.23.2 via ge-0/0/2.0
to 10.100.12.1 via ge-0/0/3.0
10.100.23.0/24 *[Direct/0] 00:41:52
> via ge-0/0/2.0
10.100.23.1/32 *[Local/0] 00:41:52
Local via ge-0/0/2.0
10.100.24.0/24 *[Direct/0] 00:41:52
> via ge-0/0/0.0
10.100.24.1/32 *[Local/0] 00:41:52
Local via ge-0/0/0.0
10.100.25.0/24 *[Direct/0] 00:41:52
> via ge-0/0/1.0
10.100.25.1/32 *[Local/0] 00:41:52
Local via ge-0/0/1.0
10.100.100.1/32 *[OSPF/10] 00:40:51, metric 1
> to 10.100.12.1 via ge-0/0/3.0
10.100.100.2/32 *[Direct/0] 00:41:52
> via lo0.0
10.100.100.3/32 *[OSPF/10] 00:40:56, metric 1
> to 10.100.23.2 via ge-0/0/2.0
100.123.0.0/16 *[Direct/0] 00:46:44
> via fxp0.0
100.123.1.1/32 *[Local/0] 00:46:44
Local via fxp0.0
224.0.0.5/32 *[OSPF/10] 00:41:52, metric 1
MultiRecv
inet6.0: 1 destinations, 1 routes (1 active, 0 holddown, 0 hidden)
+ = Active Route, - = Last Active, * = Both
ff02::2/128 *[INET6/0] 00:47:43
MultiRecv
set policy-options prefix-list vMX2Loopback 10.100.100.2/32
set policy-options policy-statement ADV-Loopback term 1 from prefix-list vMX2Loopback
set policy-options policy-statement ADV-Loopback term 1 then accept
set protocols bgp group to-AS64533 export ADV-Loopback
set protocols bgp group to-AS64544 export ADV-Loopback
ちなみにvMX2の起動時のコンフィグは下記
vMX2のconfig
set version 21.1R3.11
set system host-name vMX2
set system root-authentication encrypted-password "$6$w0uV/Veg$MxUKS00aYKDRZKuI13guEQ3yhv0XjZ5vDD/xBSVatXwzxvgMZCjERUu5kEpMaRzFDhrcyf8NLW8lQiM.KpUCE1"
set system scripts language python
set system login user jcladmin uid 2000
set system login user jcladmin class super-user
set system login user jcladmin authentication encrypted-password "$6$COH4QgW/$uFzZAk1fYdnuwVl5WUjhb/4JdtSWIq7y/eCqB3qEFLFK/QBeG1C686NzW0XL0sz8qX4bzyYW0uMIBNXK47Kw7."
set system login user jcluser uid 2001
set system login user jcluser class super-user
set system login user jcluser authentication encrypted-password "$6$G44rGtvQ$I3jMwJk.0/CbTlhEoZzoDGv9dcFuZYdKvNFHiZwZ6s5Lktf/vMHipZxDwEXxgtid.dmN5K27fMBYwKnSijiQ/."
set system services ssh root-login allow
set system services netconf ssh
set system services rest http port 3000
set system services rest enable-explorer
set system syslog user * any emergency
set system syslog file messages any notice
set system syslog file messages authorization info
set system syslog file interactive-commands interactive-commands any
set system processes dhcp-service traceoptions file dhcp_logfile
set system processes dhcp-service traceoptions file size 10m
set system processes dhcp-service traceoptions level all
set system processes dhcp-service traceoptions flag all
set chassis fpc 0 pic 0 number-of-ports 8
set chassis fpc 0 lite-mode
set interfaces ge-0/0/0 unit 0 family inet address 10.100.24.1/24
set interfaces ge-0/0/1 unit 0 family inet address 10.100.25.1/24
set interfaces ge-0/0/2 unit 0 family inet address 10.100.23.1/24
set interfaces ge-0/0/3 unit 0 family inet address 10.100.12.2/24
set interfaces fxp0 unit 0 family inet address 100.123.1.1/16
set interfaces lo0 unit 0 family inet address 10.100.100.2/32
set protocols bgp group IBGP type internal
set protocols bgp group IBGP local-address 10.100.100.2
set protocols bgp group IBGP neighbor 10.100.100.1
set protocols bgp group IBGP neighbor 10.100.100.3
set protocols bgp group to-AS64533 type external
set protocols bgp group to-AS64533 peer-as 64533
set protocols bgp group to-AS64533 neighbor 10.100.24.2
set protocols bgp group to-AS64544 type external
set protocols bgp group to-AS64544 peer-as 64544
set protocols bgp group to-AS64544 neighbor 10.100.25.2
set protocols ospf area 0.0.0.0 interface ge-0/0/3.0
set protocols ospf area 0.0.0.0 interface ge-0/0/2.0
set protocols ospf area 0.0.0.0 interface lo0.0
set routing-options autonomous-system 64522
set routing-options static route 0.0.0.0/0 next-hop 100.123.0.1
jcluser@vMX4> show route
inet.0: 14 destinations, 16 routes (14 active, 0 holddown, 0 hidden)
+ = Active Route, - = Last Active, * = Both
0.0.0.0/0 *[Static/5] 01:14:43
> to 100.123.0.1 via fxp0.0
10.100.24.0/24 *[Direct/0] 01:10:00
> via ge-0/0/0.0
10.100.24.2/32 *[Local/0] 01:10:00
Local via ge-0/0/0.0
10.100.34.0/24 *[Direct/0] 01:10:00
> via ge-0/0/1.0
10.100.34.2/32 *[Local/0] 01:10:00
Local via ge-0/0/1.0
10.100.45.0/24 *[Direct/0] 01:10:00
> via ge-0/0/2.0
10.100.45.1/32 *[Local/0] 01:10:00
Local via ge-0/0/2.0
10.100.46.0/24 *[Direct/0] 01:10:00
> via ge-0/0/3.0
10.100.46.1/32 *[Local/0] 01:10:00
Local via ge-0/0/3.0
10.100.100.2/32 *[BGP/170] 00:00:32, localpref 100
AS path: 64522 I, validation-state: unverified
> to 10.100.24.1 via ge-0/0/0.0
[BGP/170] 00:00:32, localpref 100
AS path: 64544 64522 I, validation-state: unverified
> to 10.100.45.2 via ge-0/0/2.0
[BGP/170] 00:00:31, localpref 100
AS path: 64544 64522 I, validation-state: unverified
> to 10.100.46.2 via ge-0/0/3.0
10.100.100.4/32 *[Direct/0] 01:10:00
> via lo0.0
100.123.0.0/16 *[Direct/0] 01:14:43
> via fxp0.0
100.123.1.3/32 *[Local/0] 01:14:43
Local via fxp0.0
224.0.0.5/32 *[OSPF/10] 01:10:00, metric 1
MultiRecv
inet6.0: 1 destinations, 1 routes (1 active, 0 holddown, 0 hidden)
+ = Active Route, - = Last Active, * = Both
ff02::2/128 *[INET6/0] 01:15:42
MultiRecv
jcluser@vMX2> show route protocol bgp
inet.0: 19 destinations, 24 routes (19 active, 0 holddown, 0 hidden)
+ = Active Route, - = Last Active, * = Both
10.100.100.4/32 *[BGP/170] 00:46:15, localpref 100
AS path: 64533 I, validation-state: unverified
> to 10.100.24.2 via ge-0/0/0.0
[BGP/170] 00:46:14, localpref 100, from 10.100.100.3
AS path: 64533 I, validation-state: unverified
> to 100.123.0.1 via fxp0.0
[BGP/170] 00:46:38, localpref 100
AS path: 64544 64533 I, validation-state: unverified
> to 10.100.25.2 via ge-0/0/1.0
10.100.100.5/32 *[BGP/170] 00:46:16, localpref 100
AS path: 64544 I, validation-state: unverified
> to 10.100.25.2 via ge-0/0/1.0
[BGP/170] 00:46:15, localpref 100, from 10.100.100.3
AS path: 64544 I, validation-state: unverified
> to 100.123.0.1 via fxp0.0
[BGP/170] 00:46:38, localpref 100
AS path: 64533 64544 I, validation-state: unverified
> to 10.100.24.2 via ge-0/0/0.0
10.100.100.6/32 *[BGP/170] 00:46:40, localpref 100
AS path: 64533 64544 I, validation-state: unverified
> to 10.100.24.2 via ge-0/0/0.0
[BGP/170] 00:46:40, localpref 100, from 10.100.100.3
AS path: 64533 64544 I, validation-state: unverified
> to 100.123.0.1 via fxp0.0
inet6.0: 1 destinations, 1 routes (1 active, 0 holddown, 0 hidden)
jcluser@vMX6> show route protocol bgp
inet.0: 13 destinations, 16 routes (13 active, 0 holddown, 0 hidden)
+ = Active Route, - = Last Active, * = Both
10.100.100.2/32 *[BGP/170] 01:06:56, localpref 100, from 10.100.100.5
AS path: 64522 I, validation-state: unverified
> to 100.123.0.1 via fxp0.0
[BGP/170] 01:07:17, localpref 100
AS path: 64533 64522 I, validation-state: unverified
> to 10.100.46.1 via ge-0/0/3.0
10.100.100.3/32 *[BGP/170] 01:06:52, localpref 100, from 10.100.100.5
AS path: 64522 I, validation-state: unverified
> to 100.123.0.1 via fxp0.0
[BGP/170] 01:07:13, localpref 100
AS path: 64533 64522 I, validation-state: unverified
> to 10.100.46.1 via ge-0/0/3.0
10.100.100.4/32 *[BGP/170] 01:07:14, localpref 100
AS path: 64533 I, validation-state: unverified
> to 10.100.46.1 via ge-0/0/3.0
[BGP/170] 01:07:13, localpref 100, from 10.100.100.5
AS path: 64533 I, validation-state: unverified
> to 100.123.0.1 via fxp0.0
inet6.0: 1 destinations, 1 routes (1 active, 0 holddown, 0 hidden)
jcluser@vMX6> show route
inet.0: 13 destinations, 16 routes (13 active, 0 holddown, 0 hidden)
+ = Active Route, - = Last Active, * = Both
0.0.0.0/0 *[Static/5] 02:22:02
> to 100.123.0.1 via fxp0.0
10.100.46.0/24 *[Direct/0] 02:17:33
> via ge-0/0/3.0
10.100.46.2/32 *[Local/0] 02:17:33
Local via ge-0/0/3.0
10.100.56.0/24 *[Direct/0] 02:17:33
> via ge-0/0/4.0
10.100.56.2/32 *[Local/0] 02:17:33
Local via ge-0/0/4.0
10.100.100.2/32 *[BGP/170] 01:27:26, localpref 100, from 10.100.100.5
AS path: 64522 I, validation-state: unverified
> to 100.123.0.1 via fxp0.0
[BGP/170] 01:27:47, localpref 100
AS path: 64533 64522 I, validation-state: unverified
> to 10.100.46.1 via ge-0/0/3.0
10.100.100.3/32 *[BGP/170] 01:27:22, localpref 100, from 10.100.100.5
AS path: 64522 I, validation-state: unverified
> to 100.123.0.1 via fxp0.0
[BGP/170] 01:27:43, localpref 100
AS path: 64533 64522 I, validation-state: unverified
> to 10.100.46.1 via ge-0/0/3.0
10.100.100.4/32 *[BGP/170] 01:27:44, localpref 100
AS path: 64533 I, validation-state: unverified
> to 10.100.46.1 via ge-0/0/3.0
[BGP/170] 01:27:43, localpref 100, from 10.100.100.5
AS path: 64533 I, validation-state: unverified
> to 100.123.0.1 via fxp0.0
10.100.100.5/32 *[OSPF/10] 02:16:34, metric 1
> to 10.100.56.1 via ge-0/0/4.0
10.100.100.6/32 *[Direct/0] 02:17:33
> via lo0.0
100.123.0.0/16 *[Direct/0] 02:22:02
> via fxp0.0
100.123.1.5/32 *[Local/0] 02:22:02
Local via fxp0.0
224.0.0.5/32 *[OSPF/10] 02:17:33, metric 1
MultiRecv
inet6.0: 1 destinations, 1 routes (1 active, 0 holddown, 0 hidden)
+ = Active Route, - = Last Active, * = Both
ff02::2/128 *[INET6/0] 02:23:03
MultiRecv
set policy-options policy-statement NEXT_HOP term 1 from protocol bgp
set policy-options policy-statement NEXT_HOP term 1 then next-hop self
set protocols bgp group IBGP export NEXT_HOP
ということでこれを流すタスクをPlaybookに追加します。
---- hosts: junos
name: Advertise loopback
gather_facts:falsetasks:- name: Get Loopback info
junipernetworks.junos.junos_command:commands:- show interfaces lo0.0
display: json
register: lo_info
- name: Get BGP info
junipernetworks.junos.junos_command:commands:- show bgp group
display: json
register: bgp_info
- name: Make Policy Option
junipernetworks.junos.junos_config:lines:- set policy-options prefix-list {{ inventory_hostname }}Loopback {{ loopback_address }}/32
- set policy-options policy-statement ADV-Loopback term 1 from prefix-list {{ inventory_hostname }}Loopback
- set policy-options policy-statement ADV-Loopback term 1 then accept
vars:loopback:"{{ lo_info.stdout[0]['interface-information'][0]['logical-interface'][0]['address-family'][0] }}"loopback_address:"{{ loopback['interface-address'][0]['ifa-local'][0]['data'] }}"- name: Set Export
junipernetworks.junos.junos_bgp_global:config:groups:- name:"{{ item.name[0].data }}"type: external
export: ADV-Loopback
vars:bgp_group:"{{ bgp_info.stdout[0]['bgp-group-information'][0]['bgp-group'] }}"loop:"{{ bgp_group }}"when: item.type[0].data == "External" # このタスクを追加- name: Set Next hop attribute
junipernetworks.junos.junos_config:lines:- set policy-options policy-statement NEXT_HOP term 1 from protocol bgp
- set policy-options policy-statement NEXT_HOP term 1 then next-hop self
- set protocols bgp group IBGP export NEXT_HOP
jcluser@vMX6> show route protocol bgp
inet.0: 13 destinations, 16 routes (13 active, 0 holddown, 0 hidden)
+ = Active Route, - = Last Active, * = Both
10.100.100.2/32 *[BGP/170] 00:11:48, localpref 100, from 10.100.100.5
AS path: 64522 I, validation-state: unverified
> to 10.100.56.1 via ge-0/0/4.0
[BGP/170] 02:27:19, localpref 100
AS path: 64533 64522 I, validation-state: unverified
> to 10.100.46.1 via ge-0/0/3.0
10.100.100.3/32 *[BGP/170] 00:11:48, localpref 100, from 10.100.100.5
AS path: 64522 I, validation-state: unverified
> to 10.100.56.1 via ge-0/0/4.0
[BGP/170] 02:27:15, localpref 100
AS path: 64533 64522 I, validation-state: unverified
> to 10.100.46.1 via ge-0/0/3.0
10.100.100.4/32 *[BGP/170] 02:27:16, localpref 100
AS path: 64533 I, validation-state: unverified
> to 10.100.46.1 via ge-0/0/3.0
[BGP/170] 00:11:48, localpref 100, from 10.100.100.5
AS path: 64533 I, validation-state: unverified
> to 10.100.56.1 via ge-0/0/4.0
inet6.0: 1 destinations, 1 routes (1 active, 0 holddown, 0 hidden)
set policy-options prefix-list vMX1Loopback 10.100.100.1/32
set policy-options policy-statement ADV-Loopback term 2 from prefix-list vMX1Loopback
set policy-options policy-statement ADV-Loopback term 2 then accept
これをPlaybookのタスクに付け加えます。
- name: Advertise vMX1 Loopback
junipernetworks.junos.junos_config:lines:- set policy-options prefix-list vMX1Loopback 10.100.100.1/32
- set policy-options policy-statement ADV-Loopback term 2 from prefix-list vMX1Loopback
- set policy-options policy-statement ADV-Loopback term 2 then accept
when:- inventory_hostname in ['vMX2', 'vMX3']
---- hosts: ix
gather_facts:falsetasks:- name: show ip interface Loopback0.0
rucdev.ix.ix_command:commands:- show ip interface Loopback0.0
register: pre_status
- name: pre check
ansible.builtin.debug:msg:"{{ pre_status.stdout_lines[0] }}"- name: set ip address
rucdev.ix.ix_config:lines:- ip address 10.0.0.1/24
parents: interface Loopback0.0
- name: show ip interface Loopback0.0
rucdev.ix.ix_command:commands:- show ip interface Loopback0.0
register: post_status
- name: post check
ansible.builtin.debug:msg:"{{ post_status.stdout_lines[0] }}"
PLAY [ix] *********************************************************************************************************************************************************
TASK [show ip interface Loopback0.0] ******************************************************************************************************************************
ok: [ix_test]
TASK [pre check] **************************************************************************************************************************************************
ok: [ix_test] => {
"msg": [
"Interface Loopback0.0 is dormant, line protocol is down",
" Internet protocol processing disabled"
]
}
TASK [change description] *****************************************************************************************************************************************
changed: [ix_test]
TASK [show ip interface Loopback0.0] ******************************************************************************************************************************
ok: [ix_test]
TASK [post check] *************************************************************************************************************************************************
ok: [ix_test] => {
"msg": [
"Interface Loopback0.0 is up, line protocol is up",
" Internet address is 1.1.1.1/24",
" Broadcast address is 255.255.255.255",
" Address determined by config",
" MTU is 1500 octets",
" Directed broadcast forwarding is disabled",
" Proxy ARP is disabled",
" Local proxy ARP is disabled",
" ICMP redirects are never sent",
" TCP MSS adjustment is disabled"
]
}
PLAY RECAP ********************************************************************************************************************************************************
ix_test : ok=5 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
事前事後のshowコマンドも取れていて設定もできている。良さそうですね。
冪等性のチェックとして同じプレイブックをもう一度流すと、、、
PLAY [ix] *********************************************************************************************************************************************************
TASK [show ip interface Loopback0.0] ******************************************************************************************************************************
ok: [ix_test]
TASK [pre check] **************************************************************************************************************************************************
ok: [ix_test] => {
"msg": [
"Interface Loopback0.0 is up, line protocol is up",
" Internet address is 1.1.1.1/24",
" Broadcast address is 255.255.255.255",
" Address determined by config",
" MTU is 1500 octets",
" Directed broadcast forwarding is disabled",
" Proxy ARP is disabled",
" Local proxy ARP is disabled",
" ICMP redirects are never sent",
" TCP MSS adjustment is disabled"
]
}
TASK [change description] *****************************************************************************************************************************************
ok: [ix_test]
TASK [show ip interface Loopback0.0] ******************************************************************************************************************************
ok: [ix_test]
TASK [post check] *************************************************************************************************************************************************
ok: [ix_test] => {
"msg": [
"Interface Loopback0.0 is up, line protocol is up",
" Internet address is 1.1.1.1/24",
" Broadcast address is 255.255.255.255",
" Address determined by config",
" MTU is 1500 octets",
" Directed broadcast forwarding is disabled",
" Proxy ARP is disabled",
" Local proxy ARP is disabled",
" ICMP redirects are never sent",
" TCP MSS adjustment is disabled"
]
}
PLAY RECAP ********************************************************************************************************************************************************
ix_test : ok=5 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0