-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjenkins_agent.py
1786 lines (1464 loc) · 60.9 KB
/
jenkins_agent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
# Copyright: (c) 2020, Ricardo Pescuma Domenecci
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
ANSIBLE_METADATA = {
'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'community'
}
DOCUMENTATION = '''
---
module: jenkins_slave
short_description: Manage jenkins slaves configuration using Jenkins REST API.
version_added: "2.9"
author:
- Ricardo Pescuma Domenecci (@pescuma)
options:
name:
description:
- Name that uniquely identifies an agent within this Jenkins installation.
required: true
type: str
state:
description:
- What to do with the slave
- "C(query): only query current state, make no changes"
- "C(absent): remove the slave"
- "C(present): makes sure the slave exists. It can be online, offline, connected or disconnected. "
- "C(online): makes sure the slave is online, aka not temporarily offline. Does no connect or disconnect to the slave."
- "C(offline): makes the slave temporarily offline"
- "C(connected): tries to activelly connect to the slave, if the launch_method allows it."
- "C(disconnected): tries to activelly disconnect to the slave, if the launch_method allows it."
required: false
type: str
choices: ['query', 'absent', 'present', 'online', 'offline', 'connected', 'disconnected']
default: present
server_url:
description:
- URL of the Jenkins server
required: false
type: str
default: http://localhost:8080
server_username:
description:
- Username to authenticate with the Jenkins server
required: false
type: str
server_password:
description:
- Password to authenticate with the Jenkins server
- One of I(server_password) or I(server_token) can be provided
required: false
type: str
server_token:
description:
- API Token to authenticate with the Jenkins server
- One of I(server_password) or I(server_token) can be provided
required: false
type: str
server_validate_certs:
description:
- Validate Jenkins server certificates
required: false
type: bool
default: true
server_timeout:
description:
- Timeout to wait for Jenkins server responses
required: false
type: int
default: forever
offline_reason:
description:
- Reason to show in Jenkins for taking this node offline
required: false
type: str
disconnected_reason:
description:
- Reason to show in Jenkins for disconnecting this node
required: false
type: str
wait_jobs_finish:
description:
- Wait running jobs on node to finish after taking it offline or before disconnecting it
required: false
type: bool
default: true
wait_jobs_finish_max_time:
description:
- Max time (in seconds) to wait for jobs to finish
- C(0) means forever
required: false
type: int
default: forever
description:
description:
- Optional human-readable description for this agent.
required: false
type: str
executors:
description:
- The maximum number of concurrent builds that Jenkins may perform on this node.
required: false
type: int
default: 1 when creating, current value when changing node
root_dir:
description:
- "Remote root directory"
- An agent needs to have a directory dedicated to Jenkins. Specify the path to this directory on the agent. It is best to use an absolute path, such as /var/jenkins or c:\\jenkins. This should be a path local to the agent machine. There is no need for this path to be visible from the master.
required: false
type: str
labels:
description:
- Labels (or tags) are used to group multiple agents into one logical group.
required: false
type: str
usage:
description:
- Controls how Jenkins schedules builds on this node.
- "C(normal): Use this node as much as possible"
- "C(exclusive): Only build jobs with label expressions matching this node"
required: false
type: str
choices: ['normal', 'exclusive']
default: normal when creating, current value when changing node
launch_method:
description:
- Controls how Jenkins starts this agent.
- "C(jnlp): Launch agent by connecting it to the master (Java Web Start)"
- "C(command): Launch agent via execution of command on the master"
- "C(ssh): Launch agent agents via SSH"
- "C(wmi): Let Jenkins control this Windows agent as a Windows service (Windows Management Instrumentation)"
required: false
type: str
choices: ['jnlp', 'command', 'ssh', 'wmi']
default: jnlp when creating, current value when changing node
jnlp_workdir_enabled:
description:
- Allows disabling Remoting Work Directory for the agent. In such case the agent will be running in the legacy mode without logging enabled by default.
- Only used if I(launch_method='jnlp')
required: false
type: bool
default: no when creating, current value when changing node
jnlp_workdir_path:
description:
- "Custom WorkDir path"
- If defined, a custom Remoting work directory will be used instead of the Agent Root Directory. This option has no environment variable resolution so far, it is recommended to use only absolute paths.
- Only used if I(launch_method='jnlp')
required: false
type: str
default: empty when creating, current value when changing node
jnlp_internal_dir:
description:
- "Internal data directory"
- Defines a storage directory for the internal data. This directory will be created within the Remoting working directory.
- Only used if I(launch_method='jnlp')
required: false
type: str
default: remoting when creating, current value when changing node
jnlp_fail_if_workspace_missing:
description:
- If defined, Remoting will fail at startup if the target work directory is missing. The option may be used to detect infrastructure issues like failed mount.
- Only used if I(launch_method='jnlp')
required: false
type: bool
default: no when creating, current value when changing node
jnlp_tunnel:
description:
- "Tunnel connection through"
- This tunneling option allows you to route this connection to another host/port.
- Only used if I(launch_method='jnlp')
required: false
type: str
default: empty when creating, current value when changing node
jnlp_jvm_options:
description:
- If the agent JVM should be launched with additional VM arguments, such as "-Xmx256m", specify those here.
- Only used if I(launch_method='jnlp')
required: false
type: str
default: empty when creating, current value when changing node
jnlp_websocket:
description:
- Use WebSocket to connect to the Jenkins master rather than the TCP port. See JEP-222 for background.
- Only used if I(launch_method='jnlp')
required: false
type: bool
default: false
command_launch_command:
description:
- Single command to launch an agent program, which controls the agent computer and communicates with the master. Jenkins assumes that the executed program launches the agent.jar program on the correct machine.
- Only used if I(launch_method='command')
required: false
type: str
default: empty when creating, current value when changing node
wmi_admin_username:
description:
- Provide the name of the Windows user who has the administrative access on this computer, such as 'Administrator'. This information is necessary to start a process remotely.
- Only used if I(launch_method='wmi')
required: false
type: str
default: empty when creating, current value when changing node
wmi_admin_password:
description:
- Password for the user expecified in I(wmi_admin_username)
- Only used if I(launch_method='wmi')
required: false
type: str
default: empty when creating, current value when changing node
wmi_host:
description:
- Provide the host name of the Windows host if different to the name of the Agent.
- Only used if I(launch_method='wmi')
required: false
type: str
default: empty when creating, current value when changing node
wmi_service_run_as:
description:
- Sometimes the administrator account that can install a service remotely might not be the user account you want to run your Jenkins agent (one reason you might want to do this is to run your builds/tests in more restricted account because you don't trust them. Another reason you might want to do this is to run agents in some domain user account.) This option lets you do this.
- "C(local_system): Use Local System User"
- "C(user): Log on using a different account"
- "C(administrator): Use Administrator account given above"
- Only used if I(launch_method='wmi')
required: false
type: str
choices: ['local_system', 'user', 'administrator']
default: local_system when creating, current value when changing node
wmi_service_username:
description:
- Username to run the service
- Only used if I(launch_method='wmi') and I(wmi_service_run_as='user')
required: false
type: str
default: empty when creating, current value when changing node
wmi_service_password:
description:
- Password for the user expecified in I(wmi_service_username)
- Only used if I(launch_method='wmi') and I(wmi_service_run_as='user')
required: false
type: str
default: empty when creating, current value when changing node
wmi_java_path:
description:
- "Path to java executable"
- Path to the Java executable to be used on this node. Defaults to "java", assuming JRE is installed and available on system PATH (e.g. C:\\Program Files\\Java\\jre7\\bin\\java.exe)
- Only used if I(launch_method='wmi')
required: false
type: str
default: empty when creating, current value when changing node
wmi_jvm_options:
description:
- Additional VM arguments
- Only used if I(launch_method='wmi')
required: false
type: str
default: empty when creating, current value when changing node
ssh_host:
description:
- Agent's Hostname or IP to connect.
- Only used if I(launch_method='ssh')
required: false
type: str
default: empty when creating, current value when changing node
ssh_port:
description:
- The TCP port on which the agent's SSH daemon is listening, usually 22.
- Only used if I(launch_method='ssh')
required: false
type: int
default: 22 when creating, current value when changing node
ssh_credentials_id:
description:
- Select the credentials to be used for logging in to the remote host. This must have been previously created.
- Only used if I(launch_method='ssh')
required: false
type: str
default: empty when creating, current value when changing node
ssh_host_verification:
description:
- Controls how Jenkins verifies the SSH key presented by the remote host whilst connecting.
- "C(known_hosts): Known hosts file"
- "C(key): Manually provided key"
- "C(manually_trusted): Manually trusted key"
- "C(none): Non verifying"
- Only used if I(launch_method='ssh')
required: false
type: str
choices: ['known_hosts', 'key', 'manually_trusted', 'none']
default: known_hosts when creating, current value when changing node
ssh_host_key:
description:
- The SSH key expected for this connection. This key should be in the form `algorithm value` where algorithm is one of ssh-rsa or ssh-dss, and value is the Base 64 encoded content of the key.
- Only used if I(launch_method='ssh') and I(ssh_host_verification='key')
required: false
type: str
default: empty when creating, current value when changing node
ssh_host_manually_trusted_require_initial_verification:
description:
- Require a user with Computer.CONFIGURE permission to authorise the key presented during the first connection to this host before the connection will be allowed to be established.
- Only used if I(launch_method='ssh') and I(ssh_host_verification='manually_trusted')
required: false
type: bool
default: no when creating, current value when changing node
ssh_java_path:
description:
- This java Path will be used to start the jvm. (/mycustomjdkpath/bin/java ) If empty Jenkins will search java command in the agent
- Only used if I(launch_method='ssh')
required: false
type: str
default: empty when creating, current value when changing node
ssh_jvm_options:
description:
- Additional arguments for the JVM, such as -Xmx or GC options
- Only used if I(launch_method='ssh')
required: false
type: str
default: empty when creating, current value when changing node
ssh_command_prefix:
description:
- "Prefix Start Agent Command"
- What you enter here will be prepended to the launch command.
- Only used if I(launch_method='ssh')
required: false
type: str
default: empty when creating, current value when changing node
ssh_command_suffix:
description:
- "Suffix Start Agent Command"
- What you enter here will be appended to the launch command.
- Only used if I(launch_method='ssh')
required: false
type: str
default: empty when creating, current value when changing node
ssh_connection_timeout:
description:
- "Connection Timeout in Seconds"
- Set the timeout value for ssh agent launch in seconds. If empty, it will be reset to default value.
- Only used if I(launch_method='ssh')
required: false
type: str
default: empty when creating, current value when changing node
ssh_retries:
description:
- "Maximum Number of Retries"
- Set the number of times the SSH connection will be retried if the initial connection results in an error. If empty, it will be reset to default value.
- Only used if I(launch_method='ssh')
required: false
type: int
default: empty when creating, current value when changing node
ssh_wait_between_retries:
description:
- "Seconds To Wait Between Retries"
- Set the number of seconds to wait between retry attempts of the initial SSH connection.
- Only used if I(launch_method='ssh')
required: false
type: int
default: empty when creating, current value when changing node
ssh_tcp_no_delay:
description:
- "Use TCP_NODELAY flag on the SSH connection"
- Enable/Disables the TCP_NODELAY flag on the SSH connection. If set, disable the Nagle algorithm. This means that segments are always sent as soon as possible, even if there is only a small amount of data. When not set, data is buffered until there is a sufficient amount to send out, thereby avoiding the frequent sending of small packets, which results in poor utilization of the network.
- Only used if I(launch_method='ssh')
required: false
type: bool
default: yes when creating, current value when changing node
ssh_workdir:
description:
- "Remoting Work directory"
- The Remoting work directory is an internal data storage, which may be used by Remoting to store caches, logs and other metadata. For more details see Remoting Work directory If remoting parameter "-workDir PATH" or "-jar-cache PATH" is set in Suffix Start Agent Command this field will be ignored. If empty, the Remote root directory is used as Remoting Work directory
- Only used if I(launch_method='ssh')
required: false
type: str
default: empty when creating, current value when changing node
availability:
description:
- Controls when Jenkins starts and stops this agent.
- "C(always): Keep this agent online as much as possible"
- "C(on_demand): Bring this agent online when in demand, and take offline when idle"
required: false
type: str
choices: ['always', 'on_demand']
default: always when creating, current value when changing node
on_demand_in_demand_delay:
description:
- The number of minutes for which jobs must have been waiting in the queue before Jenkins will attempt to bring this agent online.
- Only used if I(availability='on_demand')
required: false
type: int
default: 0 when creating, current value when changing node
on_demand_idle_delay:
description:
- The number of minutes that this agent must remain idle before Jenkins will take it offline.
- Only used if I(availability='on_demand')
required: false
type: int
default: 1 when creating, current value when changing node
requirements:
- "python-jenkins >= 0.4.12"
'''
EXAMPLES = '''
# Query the state of a slave
- name: Query slave state
jenkins_slave:
name: slave_name
state: query
# Creates (if needed) a jnlp slave. The secret that must be used to connect is stored in return.jnlp_secret
- name: Create a jnlp slave
jenkins_slave:
name: slave_name
state: online
launch_method: jnlp
register: return
# Creates (if needed) a wmi slave. Because this module only works on linux, you may need to delegate the work
- name: Create a wmi slave
jenkins_slave:
name: slave_name
state: online
launch_method: wmi
delegate_to: localhost
# Sets a slave as temporarily offline
- name: Take slave offline
jenkins_slave:
name: slave_name
state: offline
offline_reason: Testing ansible
'''
RETURN = '''
name:
description: Name that uniquely identifies an agent within this Jenkins installation.
type: str
returned: always
state:
description: Current state of the slave
type: str
returned: always
offline_reason:
description: Reason to show in Jenkins for taking this node offline
type: str
returned: if I(state=offline)
disconnected_reason:
description: Reason to show in Jenkins for disconnecting this node
type: str
returned: if I(state=disconnected)
description:
description: Optional human-readable description for this agent.
type: str
returned: always
executors:
description: The maximum number of concurrent builds that Jenkins may perform on this node.
type: int
returned: always
root_dir:
description: Remote root directory
type: str
returned: always
labels:
description: Labels (or tags) are used to group multiple agents into one logical group.
type: str
returned: always
usage:
description: Controls how Jenkins schedules builds on this node.
type: str
sample: normal, exclusive
returned: always
launch_method:
description: Controls how Jenkins starts this agent.
type: str
sample: jnlp, command, ssh, wmi
returned: always
jnlp_workdir_enabled:
description: Allows disabling Remoting Work Directory for the agent. In such case the agent will be running in the legacy mode without logging enabled by default.
type: bool
returned: if I(launch_method='jnlp')
jnlp_workdir_path:
description: Custom WorkDir path
type: str
returned: if I(launch_method='jnlp')
jnlp_internal_dir:
description: Internal data directory
type: str
returned: if I(launch_method='jnlp')
jnlp_fail_if_workspace_missing:
description: If defined, Remoting will fail at startup if the target work directory is missing. The option may be used to detect infrastructure issues like failed mount.
type: bool
returned: if I(launch_method='jnlp')
jnlp_tunnel:
description: Tunnel connection through
type: str
returned: if I(launch_method='jnlp')
jnlp_jvm_options:
description: If the agent JVM should be launched with additional VM arguments, such as "-Xmx256m", specify those here.
type: str
returned: if I(launch_method='jnlp')
jnlp_secret:
description: The secret that must be used to connect to this jnlp slave
type: str
returned: if I(launch_method='jnlp')
jnlp_websocket:
description: Use WebSocket to connect to the Jenkins master rather than the TCP port. See JEP-222 for background.
type: bool
returned: if I(launch_method='jnlp')
command_launch_command:
description: Single command to launch an agent program, which controls the agent computer and communicates with the master. Jenkins assumes that the executed program launches the agent.jar program on the correct machine.
type: str
returned: if I(launch_method='command')
wmi_admin_username:
description: Provide the name of the Windows user who has the administrative access on this computer, such as 'Administrator'. This information is necessary to start a process remotely.
- Only used if I(launch_method='wmi')
type: str
returned: if I(launch_method='wmi')
wmi_admin_password:
description: Password for the user expecified in I(wmi_admin_username)
type: str
returned: if I(launch_method='wmi')
wmi_host:
description: Provide the host name of the Windows host if different to the name of the Agent.
type: str
returned: if I(launch_method='wmi')
wmi_service_run_as:
description: Sometimes the administrator account that can install a service remotely might not be the user account you want to run your Jenkins agent (one reason you might want to do this is to run your builds/tests in more restricted account because you don't trust them. Another reason you might want to do this is to run agents in some domain user account.) This option lets you do this.
type: str
sample: local_system, user, administrator
returned: if I(launch_method='wmi')
wmi_service_username:
description: Username to run the service
type: str
returned: if I(launch_method='wmi') and I(wmi_service_run_as='user')
wmi_service_password:
description: Password for the user expecified in I(wmi_service_username)
type: str
returned: if I(launch_method='wmi') and I(wmi_service_run_as='user')
wmi_java_path:
description: "Path to java executable"
required: false
type: str
returned: if I(launch_method='wmi')
wmi_jvm_options:
description: Additional VM arguments
type: str
returned: if I(launch_method='wmi')
ssh_host:
description: Agent's Hostname or IP to connect.
type: str
returned: if I(launch_method='ssh')
ssh_port:
description: The TCP port on which the agent's SSH daemon is listening, usually 22.
type: int
returned: if I(launch_method='ssh')
ssh_credentials_id:
description: Select the credentials to be used for logging in to the remote host. This must have been previously created.
- Only used if I(launch_method='ssh')
required: false
type: str
default: empty when creating, current value when changing node
returned: if I(launch_method='ssh')
ssh_host_verification:
description: Controls how Jenkins verifies the SSH key presented by the remote host whilst connecting.
type: str
sample: known_hosts, key, manually_trusted, none
returned: if I(launch_method='ssh')
ssh_host_key:
description: The SSH key expected for this connection. This key should be in the form `algorithm value` where algorithm is one of ssh-rsa or ssh-dss, and value is the Base 64 encoded content of the key.
type: str
returned: if I(launch_method='ssh') and I(ssh_host_verification='key')
ssh_host_manually_trusted_require_initial_verification:
description: Require a user with Computer.CONFIGURE permission to authorise the key presented during the first connection to this host before the connection will be allowed to be established.
type: bool
returned: if I(launch_method='ssh') and I(ssh_host_verification='manually_trusted')
ssh_java_path:
description: This java Path will be used to start the jvm. (/mycustomjdkpath/bin/java ) If empty Jenkins will search java command in the agent
type: str
returned: if I(launch_method='ssh')
ssh_jvm_options:
description: Additional arguments for the JVM, such as -Xmx or GC options
type: str
returned: if I(launch_method='ssh')
ssh_command_prefix:
description: Prefix Start Agent Command
type: str
returned: if I(launch_method='ssh')
ssh_command_suffix:
description: Suffix Start Agent Command
type: str
returned: if I(launch_method='ssh')
ssh_connection_timeout:
description: Connection Timeout in Seconds
type: str
returned: if I(launch_method='ssh')
ssh_retries:
description: Maximum Number of Retries
type: int
returned: if I(launch_method='ssh')
ssh_wait_between_retries:
description: Seconds To Wait Between Retries
type: int
returned: if I(launch_method='ssh')
ssh_tcp_no_delay:
description: Use TCP_NODELAY flag on the SSH connection
type: bool
returned: if I(launch_method='ssh')
ssh_workdir:
description: Remoting Work directory
type: str
returned: if I(launch_method='ssh')
availability:
description: Controls when Jenkins starts and stops this agent.
type: str
sample: always, on_demand
on_demand_in_demand_delay:
description: The number of minutes for which jobs must have been waiting in the queue before Jenkins will attempt to bring this agent online.
type: int
returned: if I(availability='on_demand')
on_demand_idle_delay:
description: The number of minutes that this agent must remain idle before Jenkins will take it offline.
type: int
returned: if I(availability='on_demand')
'''
import traceback
from string import Template
import json
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
from ansible.module_utils._text import to_native
JENKINS_IMP_ERR = None
try:
import jenkins
python_jenkins_installed = True
except ImportError:
JENKINS_IMP_ERR = traceback.format_exc()
python_jenkins_installed = False
def run_module():
module_args = dict(
server_url = dict(required=False, type="str", default="http://localhost:8080"),
server_username = dict(required=False, type="str", default=None),
server_password = dict(required=False, no_log=True, type="str", default=None),
server_token = dict(required=False, no_log=True, type="str", default=None),
server_validate_certs = dict(required=False, type="bool", default=True),
server_timeout = dict(required=False, type="int", default=None),
name = dict(required=True, type='str'),
state = dict(required=False, choices=[
'query',
'absent',
'present',
'online',
'offline',
'connected',
'disconnected'
],
default='online'),
offline_reason = dict(required=False, type="str", default=None),
disconnected_reason = dict(required=False, type="str", default=None),
wait_jobs_finish = dict(required=False, type="bool", default=True),
wait_jobs_finish_max_time = dict(required=False, type="int", default=0),
description = dict(required=False, type="str", default=None),
executors = dict(required=False, type="int", default=None),
root_dir = dict(required=False, type="str", default=None),
labels = dict(required=False, type="str", default=None),
usage = dict(required=False, choices=[ 'normal', 'exclusive'], default=None),
launch_method = dict(required=False, choices=[
'jnlp',
'command',
'ssh',
'wmi'
],
default=None),
jnlp_workdir_enabled = dict(required=False, type="bool", default=None),
jnlp_workdir_path = dict(required=False, type="str", default=None),
jnlp_internal_dir = dict(required=False, type="str", default=None),
jnlp_fail_if_workspace_missing = dict(required=False, type="bool", default=None),
jnlp_tunnel = dict(required=False, type="str", default=None),
jnlp_jvm_options = dict(required=False, type="str", default=None),
jnlp_websocket = dict(required=False, type="bool", default=False),
command_launch_command = dict(required=False, type="str", default=None),
wmi_admin_username = dict(required=False, type="str", default=None),
wmi_admin_password = dict(required=False, no_log=True, type="str", default=None),
wmi_host = dict(required=False, type="str", default=None),
wmi_service_run_as = dict(required=False, choices=[
'local_system',
'user',
'administrator'
],
default=None),
wmi_service_username = dict(required=False, type="str", default=None),
wmi_service_password = dict(required=False, no_log=True, type="str", default=None),
wmi_java_path = dict(required=False, type="str", default=None),
wmi_jvm_options = dict(required=False, type="str", default=None),
ssh_host = dict(required=False, type="str", default=None),
ssh_port = dict(required=False, type="int", default=None),
ssh_credentials_id = dict(required=False, type="str", default=None),
ssh_host_verification = dict(required=False, choices=[
'known_hosts',
'key',
'manually_trusted',
'none'
],
default=None),
ssh_host_key = dict(required=False, type="str", default=None),
ssh_host_manually_trusted_require_initial_verification = dict(required=False, type="bool", default=None),
ssh_java_path = dict(required=False, type="str", default=None),
ssh_jvm_options = dict(required=False, type="str", default=None),
ssh_command_prefix = dict(required=False, type="str", default=None),
ssh_command_suffix = dict(required=False, type="str", default=None),
ssh_connection_timeout = dict(required=False, type="int", default=None),
ssh_retries = dict(required=False, type="int", default=None),
ssh_wait_between_retries = dict(required=False, type="int", default=None),
ssh_tcp_no_delay = dict(required=False, type="bool", default=None),
ssh_workdir = dict(required=False, type="str", default=None),
availability = dict(required=False, choices=[
'always',
'on_demand'
],
default=None),
on_demand_in_demand_delay = dict(required=False, type="int", default=None),
on_demand_idle_delay = dict(required=False, type="int", default=None)
)
module = AnsibleModule(
argument_spec=module_args,
supports_check_mode=True
)
if not python_jenkins_installed:
module.fail_json(
msg=missing_required_lib("python-jenkins",
url="https://python-jenkins.readthedocs.io/en/latest/install.html"),
exception=JENKINS_IMP_ERR)
args_escaped = dict()
for key in module.params:
if key.startswith('server_'):
continue
val = module.params[key]
if val == None:
val = 'null'
elif isinstance(val, str):
val = '"' + val.replace('\\', '\\\\').replace('"', '\\"') + '"'
elif isinstance(val, bool):
val = str(val).lower()
else:
val = str(val)
args_escaped[key] = val
script = Template("""
import jenkins.model.*
import hudson.model.*
import hudson.node_monitors.*
import jenkins.slaves.*
import hudson.slaves.*
import hudson.util.*
import java.util.concurrent.*
import groovy.json.*
args = [
state: $state,
offline_reason: $offline_reason,
disconnected_reason: $disconnected_reason,
wait_jobs_finish: $wait_jobs_finish,
wait_jobs_finish_max_time: $wait_jobs_finish_max_time,
name: $name,
description: $description,
executors: $executors,
root_dir: $root_dir,
labels: $labels,
usage: $usage,
launch_method: $launch_method,
jnlp_workdir_enabled: $jnlp_workdir_enabled,
jnlp_workdir_path: $jnlp_workdir_path,
jnlp_internal_dir: $jnlp_internal_dir,
jnlp_fail_if_workspace_missing: $jnlp_fail_if_workspace_missing,
jnlp_tunnel: $jnlp_tunnel,
jnlp_jvm_options: $jnlp_jvm_options,
jnlp_websocket: $jnlp_websocket,
command_launch_command: $command_launch_command,
wmi_admin_username: $wmi_admin_username,
wmi_admin_password: $wmi_admin_password,
wmi_host: $wmi_host,
wmi_service_run_as: $wmi_service_run_as,
wmi_service_username: $wmi_service_username,
wmi_service_password: $wmi_service_password,
wmi_java_path: $wmi_java_path,
wmi_jvm_options: $wmi_jvm_options,
ssh_host: $ssh_host,
ssh_port: $ssh_port,
ssh_credentials_id: $ssh_credentials_id,
ssh_host_verification: $ssh_host_verification,
ssh_host_key: $ssh_host_key,
ssh_host_manually_trusted_require_initial_verification: $ssh_host_manually_trusted_require_initial_verification,
ssh_java_path: $ssh_java_path,
ssh_jvm_options: $ssh_jvm_options,
ssh_command_prefix: $ssh_command_prefix,
ssh_command_suffix: $ssh_command_suffix,
ssh_connection_timeout: $ssh_connection_timeout,
ssh_retries: $ssh_retries,
ssh_wait_between_retries: $ssh_wait_between_retries,
ssh_tcp_no_delay: $ssh_tcp_no_delay,
ssh_workdir: $ssh_workdir,
availability: $availability,
on_demand_in_demand_delay: $on_demand_in_demand_delay,
on_demand_idle_delay: $on_demand_idle_delay
]
args.description = to_string_arg(args.description)
args.executors = to_int_arg(args.executors, 1)
args.root_dir = to_string_arg(args.root_dir)
args.labels = to_string_arg(args.labels)
args.usage = to_choice_arg(args.usage, 'normal')
args.launch_method = to_choice_arg(args.launch_method, 'jnlp')
args.jnlp_workdir_enabled = to_bool_arg(args.jnlp_workdir_enabled, true)
args.jnlp_workdir_path = to_string_arg(args.jnlp_workdir_path)
args.jnlp_internal_dir = to_string_arg(args.jnlp_internal_dir, 'remoting')
args.jnlp_fail_if_workspace_missing = to_bool_arg(args.jnlp_fail_if_workspace_missing, false)
args.jnlp_tunnel = to_string_arg(args.jnlp_tunnel)
args.jnlp_jvm_options = to_string_arg(args.jnlp_jvm_options)
args.jnlp_websocket = to_bool_arg(args.jnlp_websocket)
args.command_launch_command = to_string_arg(args.command_launch_command)
args.wmi_admin_username = to_string_arg(args.wmi_admin_username)
args.wmi_admin_password = to_string_arg(args.wmi_admin_password)
args.wmi_host = to_string_arg(args.wmi_host)
args.wmi_service_run_as = to_choice_arg(args.wmi_service_run_as, 'local_system')
args.wmi_service_username = to_string_arg(args.wmi_service_username)
args.wmi_service_password = to_string_arg(args.wmi_service_password)
args.wmi_java_path = to_string_arg(args.wmi_java_path)
args.wmi_jvm_options = to_string_arg(args.wmi_jvm_options)
args.ssh_host = to_string_arg(args.ssh_host)
args.ssh_port = to_int_arg(args.ssh_port, 22)
args.ssh_credentials_id = to_string_arg(args.ssh_credentials_id)
args.ssh_host_verification = to_choice_arg(args.ssh_host_verification, 'known_hosts')
args.ssh_host_key = to_string_arg(args.ssh_host_key)
args.ssh_host_manually_trusted_require_initial_verification = to_bool_arg(args.ssh_host_manually_trusted_require_initial_verification, false)
args.ssh_java_path = to_string_arg(args.ssh_java_path)
args.ssh_jvm_options = to_string_arg(args.ssh_jvm_options)
args.ssh_command_prefix = to_string_arg(args.ssh_command_prefix)
args.ssh_command_suffix = to_string_arg(args.ssh_command_suffix)
args.ssh_connection_timeout = to_int_arg(args.ssh_connection_timeout, null)
args.ssh_retries = to_int_arg(args.ssh_retries, null)
args.ssh_wait_between_retries = to_int_arg(args.ssh_wait_between_retries, null)
args.ssh_tcp_no_delay = to_bool_arg(args.ssh_tcp_no_delay, true)
args.ssh_workdir = to_string_arg(args.ssh_workdir)
args.availability = to_choice_arg(args.availability, 'always')
args.on_demand_in_demand_delay = to_int_arg(args.on_demand_in_demand_delay, 0)
args.on_demand_idle_delay = to_int_arg(args.on_demand_idle_delay, 1)
def process() {
node = getNode(args.name)
changed = false
switch(args.state) {
case 'query':
break
case 'absent':
changed = removeNode(node)
break
case 'present':
case 'online':
case 'offline':
case 'connected':
case 'disconnected':
if (node == null)
changed = createNode()
else
changed = changeNode(node)
break
default:
throw new Exception("Unknown state: " + args.state)
}
result = getCurrentState()
result.changed = changed
return result
}
def createNode() {
Jenkins.instance.addNode(createNodeObject())