File: Config.pm

package info (click to toggle)
rex 1.14.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,012 kB
  • sloc: perl: 35,587; xml: 264; sh: 51; makefile: 13
file content (1887 lines) | stat: -rw-r--r-- 40,535 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
#
# (c) Jan Gehring <jan.gehring@gmail.com>
#

=head1 NAME

Rex::Config - Handles Rex configuration

=head1 SYNOPSIS

 use Rex::Config;

 # set a config option
 Rex::Config->set_exec_autodie(TRUE);

 # get value of a config option
 my $user = Rex::Config->get_user();

=head1 DESCRIPTION

This module holds all configuration options for Rex, and also allows you to specify your own ones for your modules.

Please take a look at L<Rex::Commands> first, which provides convenience wrappers for many of these options.

While it's possible to use the methods below to set a configuration option directly, their main intended purpose is to be used as internal plumbing, and to provide an escape hatch in case there are no better alternatives.

=head1 EXPORTED METHODS

=cut

package Rex::Config;

use v5.12.5;
use warnings;

our $VERSION = '1.14.1'; # VERSION

use Rex::Helper::File::Spec;
use Rex::Logger;
use YAML;
use Data::Dumper;
use Rex::Require;
use Symbol;

our (
  $user,                        $password,
  $port,                        $timeout,
  $max_connect_fails,           $password_auth,
  $key_auth,                    $krb5_auth,
  $public_key,                  $private_key,
  $parallelism,                 $log_filename,
  $log_facility,                $sudo_password,
  $ca_file,                     $ca_cert,
  $ca_key,                      $path,
  $no_path_cleanup,             $set_param,
  $environment,                 $connection_type,
  $distributor,                 $template_function,
  $SET_HANDLER,                 $HOME_CONFIG,
  $HOME_CONFIG_YAML,            %SSH_CONFIG_FOR,
  $sudo_without_locales,        $sudo_without_sh,
  $no_tty,                      $source_global_profile,
  $source_profile,              %executor_for,
  $allow_empty_groups,          $use_server_auth,
  $tmp_dir,                     %openssh_opt,
  $use_cache,                   $cache_type,
  $use_sleep_hack,              $report_type,
  $do_reporting,                $say_format,
  $exec_autodie,                $verbose_run,
  $disable_taskname_warning,    $proxy_command,
  $task_call_by_method,         $fallback_auth,
  $register_cmdb_template,      $check_service_exists,
  $set_no_append,               $use_net_openssh_if_present,
  $use_template_ng,             $use_rex_kvm_agent,
  $autodie,                     $task_chaining_cmdline_args,
  $waitpid_blocking_sleep_time, $write_utf8_files,
  $default_auth,
);

# some defaults
%executor_for = (
  perl   => "perl",
  python => "python",
  ruby   => "ruby",
  bash   => "bash",
);

=head2 set_autodie

=head2 get_autodie

Sets and gets the value of the C<$autodie> configuration variable.

This controls whether Rex should C<die()> if there's an error while executing L<file system commands that are supposed to change the contents|Rex::Commands::Fs#Changing-content>.

Default is C<undef>.

=cut

sub set_autodie {
  my $class = shift;
  $autodie = shift;
}

sub get_autodie {
  return $autodie;
}

=head2 set_use_net_openssh_if_present

=head2 get_use_net_openssh_if_present

Sets and gets the value of the C<$use_net_openssh_if_present> configuration variable.

This controls whether Rex should use L<Net::OpenSSH> for connections if that is available. 

Default is C<undef>.

=cut

sub set_use_net_openssh_if_present {
  my $class = shift;
  $use_net_openssh_if_present = shift;
}

sub get_use_net_openssh_if_present {
  return $use_net_openssh_if_present;
}

=head2 set_use_rex_kvm_agent

=head2 get_use_rex_kvm_agent

Sets and gets the value of the C<$use_rex_kvm_agent> configuration variable.

This controls whether Rex should setup and use a serial device for the experimental L<Rex KVM agent|https://github.com/RexOps/rex-kvm-agent> for managed VMs.

Default is C<undef>.

=cut

sub set_use_rex_kvm_agent {
  my $class = shift;
  $use_rex_kvm_agent = shift;
}

sub get_use_rex_kvm_agent {
  return $use_rex_kvm_agent;
}

=head2 set_use_template_ng

=head2 get_use_template_ng

Sets and gets the value of the C<$use_template_ng> configuration variable.

This controls whether Rex should use L<Rex::Template::NG> to render templates.

Default is C<undef>.

=cut

sub set_use_template_ng {
  my $class = shift;
  $use_template_ng = shift;
}

sub get_use_template_ng {
  return $use_template_ng;
}

=head2 set_set_no_append

=head2 get_set_no_append

Sets and gets the value of the C<$set_no_append> configuration variable.

This controls whether Rex should overwrite or append values of configuration options when using the L<set|Rex::Commands#set> command.

Default is C<undef>.

=cut

sub set_set_no_append {
  my $class = shift;
  $set_no_append = shift;
}

sub get_set_no_append {
  return $set_no_append;
}

=head2 set_check_service_exists

=head2 get_check_service_exists

Sets and gets the value of the C<$check_service_exists> configuration variable.

This controls whether Rex should C<die()> early if it is asked to manage a service that doesn't exist.

Default is C<undef>.

=cut

sub set_check_service_exists {
  my $class = shift;
  $check_service_exists = shift;
}

sub get_check_service_exists {
  return $check_service_exists;
}

=head2 set_register_cmdb_template

=head2 get_register_cmdb_template

Sets and gets the value of the C<$register_cmdb_template> configuration variable.

This controls whether Rex should make L<CMDB|Rex::CMDB> data available to be used in templates as variables.

Default is C<undef>.

=cut

sub set_register_cmdb_template {
  my $class = shift;
  $register_cmdb_template = shift;
}

sub get_register_cmdb_template {
  return $register_cmdb_template;
}

=head2 set_fallback_auth

=head2 get_fallback_auth

Sets and gets the value of the C<$fallback_auth> configuration variable.

This can be used to define an array of hash references, each of them containing L<authentication details|Rex::Commands#auth> to be tried during connection attempts when the directly specified ones fail.

Default is C<undef>.

=cut

sub set_fallback_auth {
  my $class = shift;
  $fallback_auth = [@_];
}

sub get_fallback_auth {
  return $fallback_auth;
}

=head2 set_task_call_by_method

=head2 get_task_call_by_method

Sets and gets the value of the C<$task_call_by_method> configuration variable.

This controls whether calling tasks as a method is allowed or not.

Default is C<undef>.

=cut

sub set_task_call_by_method {
  my $class = shift;
  $task_call_by_method = shift;
}

sub get_task_call_by_method {
  return $task_call_by_method;
}

=head2 set_disable_taskname_warning

=head2 get_disable_taskname_warning

Sets and gets the value of the C<$disable_taskname_warning> configuration variable.

This controls whether Rex should show or suppress the warning message about task names that can not be used as Perl identifiers.

Default is C<undef>.

=cut

sub set_disable_taskname_warning {
  my $class = shift;
  $disable_taskname_warning = shift;
}

sub get_disable_taskname_warning {
  return $disable_taskname_warning;
}

=head2 set_task_chaining_cmdline_args

=head2 get_task_chaining_cmdline_args

Sets and gets the value of the C<$task_chaining_cmdline_args> configuration variable.

This controls whether Rex should parse task arguments on the command line per task, or should pass all arguments to all tasks.

Default is C<undef>.

=cut

sub set_task_chaining_cmdline_args {
  my $class = shift;
  $task_chaining_cmdline_args = shift;
}

sub get_task_chaining_cmdline_args {
  return $task_chaining_cmdline_args;
}

=head2 set_verbose_run

=head2 get_verbose_run

Sets and gets the value of the C<$verbose_run> configuration variable.

This controls whether Rex should show verbose output about executed L<run|Rex::Commands::Run#run> commands. This means an error message if the command is not found, a warning message if the exit code indicates an error, and an informational message upon success.

Default is C<undef>.

=cut

sub set_verbose_run {
  my $class = shift;
  $verbose_run = shift;
}

sub get_verbose_run {
  return $verbose_run;
}

=head2 set_exec_autodie

=head2 get_exec_autodie

Sets and gets the value of the C<$exec_autodie> configuration variable.

This controls whether Rex should C<die()> or not when the exit code of executed L<run|Rex::Commands::Run#run> command indicate an error.

Default is C<undef>.

=cut

sub set_exec_autodie {
  my $class = shift;
  $exec_autodie = shift;
}

sub get_exec_autodie {
  return $exec_autodie;
}

=head2 set_no_path_cleanup

=head2 get_no_path_cleanup

Sets and gets the value of the C<$no_path_cleanup> configuration variable.

This controls whether Rex should use either the default or the explicitly configured C<PATH> settings (via L<path|Rex::Commands#path> or L<set_path>) when executing commands or not.

Default is C<undef>.

=cut

sub set_no_path_cleanup {
  my $class = shift;
  $no_path_cleanup = shift;
}

sub get_no_path_cleanup {
  return $no_path_cleanup;
}

=head2 set_source_profile

=head2 get_source_profile

Sets and gets the value of the C<$source_profile> configuration variable.

This controls whether Rex should source shell-specific profile files before executing commands.

Default is C<undef>.

=cut

sub set_source_profile {
  my $class = shift;
  $source_profile = shift;
}

sub get_source_profile {
  return $source_profile;
}

=head2 set_say_format

=head2 get_say_format

Sets and gets the value of the C<$say_format> configuration variable.

This controls the output format of the built-in C<say> command (see also L<sayformat|Rex::Commands#sayformat>).

Default is C<undef>.

=cut

sub set_say_format {
  my $class = shift;
  $say_format = shift;
}

sub get_say_format {
  return $say_format;
}

=head2 set_do_reporting

=head2 get_do_reporting

Sets and gets the value of the C<$do_reporting> configuration variable.

This controls whether Rex should do reporting on executed resources where it is supported. This only affects the data structures returned internally.

Default is C<undef>.

=cut

sub set_do_reporting {
  my $class = shift;
  $do_reporting = shift;
}

sub get_do_reporting {
  return $do_reporting;
}

=head2 set_report_type

=head2 get_report_type

Sets and gets the value of the C<$report_type> configuration variable, which can also be controlled via the C<REX_REPORT_TYPE> environment variable.

This selects the reporting type (format) Rex should use, e.g. C<'YAML'>.

Default is C<undef>.

=cut

sub set_report_type {
  my $class = shift;
  $report_type = shift;
}

sub get_report_type {
  if ( exists $ENV{REX_REPORT_TYPE} ) {
    return $ENV{REX_REPORT_TYPE};
  }

  return $report_type;
}

=head2 set_sleep_hack

=head2 get_sleep_hack

Sets and gets the value of the C<$sleep_hack> configuration variable.

This controls whether Rex should use or not an extra 10 ns long sleep after executed commands.

This might help working around an issue when Rex runs inside a KVM virtualized host and L<Net::SSH2>/L<libssh2|https://www.libssh2.org> is used to connect to another VM on the same hardware.

Default is C<undef>.

=cut

sub set_sleep_hack {
  my $class = shift;
  $use_sleep_hack = shift;
}

sub get_sleep_hack {
  return $use_sleep_hack;
}

=head2 set_cache_type

=head2 get_cache_type

Sets and gets the value of the C<$cache_type> configuration variable, which can also be controlled via the C<REX_CACHE_TYPE> environment variable.

This selects the cache type Rex should use, e.g. C<'YAML'>.

Default is C<'Base'>.

=cut

sub set_cache_type {
  my $class = shift;
  $cache_type = shift;
}

sub get_cache_type {
  if ( exists $ENV{REX_CACHE_TYPE} ) {
    return $ENV{REX_CACHE_TYPE};
  }

  return $cache_type || "Base";
}

=head2 set_use_cache

=head2 get_use_cache

Sets and gets the value of the C<$use_cache> configuration variable.

This controls whether Rex should use caching or not for runtime information like CMDB contents, hardware and operating system information, or the shell type that is being used to execute commands on the managed endpoint.

Default is C<undef>.

=cut

sub set_use_cache {
  my $class = shift;
  $use_cache = shift;
}

sub get_use_cache {
  return $use_cache;
}

=head2 set_openssh_opt

=head2 get_openssh_opt

Sets and gets the value of the C<$openssh_opt> configuration variable, which holds a hash of the SSH configuration options used for the connection. See the L<ssh_config(5) man page|http://man.openbsd.org/OpenBSD-current/man5/ssh_config.5> for the available options.

  Rex::Config->set_openssh_opt( $option => $value, );

There is a custom option named C<initialize_options> specific to Rex, which can be used to pass a hash reference describing the L<constructor parameters|https://metacpan.org/pod/Net::OpenSSH#Net::OpenSSH-E<gt>new($host,-%opts)> for the underlying L<Net::OpenSSH> object:

 Rex::Config->set_openssh_opt( initialize_options => { $parameter => $value, } );

Default is C<undef>.

=cut

sub set_openssh_opt {
  my ( $class, %opt ) = @_;

  for my $key ( keys %opt ) {
    if ( !defined $opt{$key} ) {
      $openssh_opt{$key} = undef;
      delete $openssh_opt{$key};
      next;
    }

    $openssh_opt{$key} = $opt{$key};
  }
}

sub get_openssh_opt {
  return %openssh_opt;
}

=head2 set_sudo_without_locales

=head2 get_sudo_without_locales

Sets and gets the value of the C<$sudo_without_locales> configuration variable.

This controls whether Rex should execute L<sudo|Rex::Commands::Run#sudo> commands without setting any locales via the C<LC_ALL> environment variable.

B<Warning:> if the locale is something else than C<C> or C<en_US>, then things will break!

Default is C<undef>.

=cut

sub set_sudo_without_locales {
  my $class = shift;
  $sudo_without_locales = shift;
}

sub get_sudo_without_locales {
  return $sudo_without_locales;
}

=head2 set_sudo_without_sh

=head2 get_sudo_without_sh

Sets and gets the value of the C<$sudo_without_sh> configuration variable.

This controls whether Rex should run L<sudo|Rex::Commands::Run#sudo> commands without C<sh>. This might break things.

Default is C<undef>.

=cut

sub set_sudo_without_sh {
  my $class = shift;
  $sudo_without_sh = shift;
}

sub get_sudo_without_sh {
  return $sudo_without_sh;
}

=head2 set_executor_for

=head2 get_executor_for

Sets and gets the keys and values of the C<%executor_for> configuration variable.

This sets the executor for a given file type when using the C<upload_and_run()> function of L<Rex::Helper::Run> module.

Default is:

 (
   perl   => 'perl',
   python => 'python',
   ruby   => 'ruby',
   bash   => 'bash',
 )

=cut

sub set_executor_for {
  my $class = shift;
  my $for   = shift;
  my $e     = shift;

  $executor_for{$for} = $e;
}

sub get_executor_for {
  my $class = shift;
  my $e     = shift;

  return $executor_for{$e};
}

=head2 set_tmp_dir

=head2 get_tmp_dir

Sets and gets the value of the C<$tmp_dir> configuration variable.

This controls which directory Rex should use for temporary files.

Default is determined by the following logic:

=over 4

=item * try to use what C<< File::Spec->tmpdir >> would return on the managed endpoint

=item * fall back to C<'/tmp'>

=back

=cut

sub set_tmp_dir {
  my ( $class, $dir ) = @_;
  if ( $class eq "Rex::Config" ) {
    $tmp_dir = $dir;
  }
  else {
    $tmp_dir = $class;
  }
}

sub get_tmp_dir {
  my $cache = Rex::get_cache();
  if ( my $cached_tmp = $cache->get("tmpdir") ) {
    return $cached_tmp;
  }

  if ( !$tmp_dir ) {
    if ( my $ssh = Rex::is_ssh() ) {
      my $exec;
      if ( Rex::is_sudo() ) {
        if ( ref $ssh eq "Net::OpenSSH" ) {
          $exec = Rex::Interface::Exec->create("OpenSSH");
        }
        else {
          $exec = Rex::Interface::Exec->create("SSH");
        }
      }
      else {
        $exec = Rex::Interface::Exec->create;
      }
      my ($out) =
        $exec->exec("perl -MFile::Spec -le 'print File::Spec->tmpdir'");

      if ( $? == 0 && $out ) {
        $out =~ s/[\r\n]//gms;

        $cache->set( "tmpdir", $out );
        return $out;
      }
      $cache->set( "tmpdir", "/tmp" );
      return "/tmp";
    }
    else {
      $cache->set( "tmpdir", Rex::Helper::File::Spec->tmpdir );
      return Rex::Helper::File::Spec->tmpdir;
    }
  }
  return $tmp_dir;
}

=head2 set_path

=head2 get_path

Sets and gets the value of the C<$path> configuration variable.

This controls which C<PATH> Rex should use when executing commands via the L<Rex::Commands::Run> module.

The value should be set as an array reference, and will be dereferenced as such before returned by C<get_path>. The L<path|Rex::Commands#path> command is a convenience wrapper for C<set_path>, and accepts an array.

Default is

 qw(
   /bin
   /sbin
   /usr/bin
   /usr/sbin
   /usr/local/bin
   /usr/local/sbin
   /usr/pkg/bin
   /usr/pkg/sbin
 )

=cut

sub set_path {
  my $class = shift;
  $path = shift;
}

sub get_path {
  if ( !$path ) {
    return (
      "/bin",         "/sbin",          "/usr/bin",
      "/usr/sbin",    "/usr/local/bin", "/usr/local/sbin",
      "/usr/pkg/bin", "/usr/pkg/sbin"
    );
  }
  return @{$path};
}

=head2 set_user

=head2 get_user

Sets and gets the value of the C<$user> configuration variable, which also can be set via the C<REX_USER> environment variable.

This controls which L<user|Rex::Commands#user> Rex should use for authentication.

Default is determined by the following logic:

=over 4

=item * value of C<REX_USER> environment variable

=item * user set by L<user|Rex::Commands#user> command

=item * user running Rex

=back

=cut

sub set_user {
  my $class = shift;
  $user = shift;
}

sub has_user {
  my $class = shift;
  return $user;
}

sub get_user {
  my $class = shift;

  if ( exists $ENV{REX_USER} ) {
    return $ENV{REX_USER};
  }

  if ($user) {
    return $user;
  }

  if ( $^O =~ m/^MSWin/ ) {
    return getlogin;
  }
  else {
    return scalar getpwuid($<);
  }
}

=head2 set_password

=head2 get_password

Sets and gets the value of the C<$password> configuration variable, which also can be set via the C<REX_PASSWORD> environment variable.

This controls what L<password|Rex::Commands#password> Rex should use for authentication or as passphrase when using private keys.

Default is C<undef>.

=cut

sub set_password {
  my $class = shift;
  $password = shift;
}

sub get_password {
  my $class = shift;

  if ( exists $ENV{REX_PASSWORD} ) {
    return $ENV{REX_PASSWORD};
  }

  return $password;
}

=head2 set_port

=head2 get_port

Sets and gets the value of the C<$port> configuration variable.

This controls which L<port|Rex::Commands#port> Rex should connect to.

C<get_port> accepts an optional C<< server => $server >> argument to return the C<port> setting for the given C<$server> as optionally set in group files.

Default is C<undef>.

=cut

sub set_port {
  my $class = shift;
  $port = shift;
}

sub get_port {
  my $class = shift;
  my $param = {@_};

  if ( exists $param->{server}
    && exists $SSH_CONFIG_FOR{ $param->{server} }
    && exists $SSH_CONFIG_FOR{ $param->{server} }->{port} )
  {
    return $SSH_CONFIG_FOR{ $param->{server} }->{port};
  }

  return $port;
}

=head2 set_sudo_password

=head2 get_sudo_password

Sets and gets the value of the C<$sudo_password> configuration variable, which can also be controlled via the C<REX_SUDO_PASSWORD> environment variable.

This controls what L<sudo password|Rex::Commands#sudo_password> Rex should use.

Default is determined by the following logic:

=over 4

=item * value of C<REX_SUDO_PASSWORD> environment variable

=item * sudo password set by the L<sudo_password|Rex::Command#sudo_password> command

=item * password set by the L<password|Rex::Command#password> command

=item * empty string (C<''>)

=back

=cut

sub set_sudo_password {
  my $class = shift;
  $sudo_password = shift;
}

sub get_sudo_password {
  my $class = shift;

  if ( exists $ENV{REX_SUDO_PASSWORD} ) {
    return $ENV{REX_SUDO_PASSWORD};
  }

  if ($sudo_password) {
    return $sudo_password;
  }
  elsif ( !defined $sudo_password ) {
    return "";
  }
  else {
    return $password;
  }

  return "";
}

=head2 set_source_global_profile

=head2 get_source_global_profile

Sets and gets the value of the C<$source_global_profile> configuration variable.

This controls whether Rex should source C</etc/profile> before executing commands.

Default is C<undef>.

=cut

sub set_source_global_profile {
  my $class = shift;
  $source_global_profile = shift;
}

sub get_source_global_profile {
  return $source_global_profile;
}

=head2 set_max_connect_fails

=head2 get_max_connect_fails

Sets and gets the value of the C<$max_connect_fails> configuration variable.

This controls how many times Rex should retry to connect before giving up.

C<get_max_connect_fails> accepts an optional C<< server => $server >> argument to C<connectionattempts> setting for the given C<$server> as optionally set in group files.

Default is C<undef>.

=cut

sub set_max_connect_fails {
  my $class = shift;
  $max_connect_fails = shift;
}

sub get_max_connect_fails {
  my $class = shift;
  my $param = {@_};

  if ( exists $param->{server}
    && exists $SSH_CONFIG_FOR{ $param->{server} }
    && exists $SSH_CONFIG_FOR{ $param->{server} }->{connectionattempts} )
  {
    return $SSH_CONFIG_FOR{ $param->{server} }->{connectionattempts};
  }

  return $max_connect_fails || 3;
}

=head2 set_proxy_command

=head2 get_proxy_command

Sets and gets the value of the C<$proxy_command> configuration variable.

This controls the SSH ProxyCommand Rex should set for connections when L<Net::OpenSSH> is used.

C<get_proxy_command> accepts an optional C<< server => $server >> argument to return the C<proxycommand> setting for the given C<$server> as optionally set in group files.

Default is C<undef>.

=cut

sub set_proxy_command {
  my $class = shift;
  $proxy_command = shift;
}

sub get_proxy_command {
  my $class = shift;
  my $param = {@_};

  if ( exists $param->{server}
    && exists $SSH_CONFIG_FOR{ $param->{server} }
    && exists $SSH_CONFIG_FOR{ $param->{server} }->{proxycommand} )
  {
    return $SSH_CONFIG_FOR{ $param->{server} }->{proxycommand};
  }

  return $proxy_command;
}

=head2 set_timeout

=head2 get_timeout

Sets and gets the value of the C<$timeout> configuration variable.

This controls how many seconds Rex should wait for connections to succeed when using SSH or L<Rex::Commands::Rsync>.

C<get_timeout> accepts an optional C<< server => $server >> argument to return the C<connecttimeout> setting for the given C<$server> as optionally set in group files.

Default is C<undef>.

=cut

sub set_timeout {
  my $class = shift;
  $timeout = shift;
}

sub get_timeout {
  my $class = shift;
  my $param = {@_};

  if ( exists $param->{server}
    && exists $SSH_CONFIG_FOR{ $param->{server} }
    && exists $SSH_CONFIG_FOR{ $param->{server} }->{connecttimeout} )
  {
    return $SSH_CONFIG_FOR{ $param->{server} }->{connecttimeout};
  }

  return $timeout || 2;
}

=head2 set_password_auth

=head2 get_password_auth

Sets and gets the value of the C<$password_auth> configuration variable, which can also be set by setting the C<REX_AUTH_TYPE> environment variable to C<pass>.

This controls whether Rex should use the L<password authentication|Rex::Commands#pass_auth> method.

Default is C<undef>.

=cut

sub set_password_auth {
  my $class = shift;
  $key_auth      = 0;
  $krb5_auth     = 0;
  $password_auth = shift || 1;
}

sub get_password_auth {
  if ( exists $ENV{REX_AUTH_TYPE} && $ENV{REX_AUTH_TYPE} eq "pass" ) {
    return 1;
  }
  return $password_auth;
}

=head2 set_key_auth

=head2 get_key_auth

Sets and gets the value of the C<$key_auth> configuration variable, which can also be set by setting the C<REX_AUTH_TYPE> environment variable to C<key>.

This controls whether Rex should use the L<key authentication|Rex::Commands#key_auth> method.

Default is C<undef>.

=cut

sub set_key_auth {
  my $class = shift;
  $password_auth = 0;
  $krb5_auth     = 0;
  $key_auth      = shift || 1;
}

sub get_key_auth {
  if ( exists $ENV{REX_AUTH_TYPE} && $ENV{REX_AUTH_TYPE} eq "key" ) {
    return 1;
  }
  return $key_auth;
}

=head2 set_krb5_auth

=head2 get_krb5_auth

Sets and gets the value of the C<$krb5_auth> configuration variable, which can also be set by setting the C<REX_AUTH_TYPE> environment variable to C<krb5>.

This controls whether Rex should use the L<Kerberos 5|Rex::Commands#krb5_auth> authentication method.

Default is C<undef>.

=cut

sub set_krb5_auth {
  my $class = shift;
  $password_auth = 0;
  $key_auth      = 0;
  $krb5_auth     = shift || 1;
}

sub get_krb5_auth {
  if ( exists $ENV{REX_AUTH_TYPE} && $ENV{REX_AUTH_TYPE} eq "krb5" ) {
    return 1;
  }
  return $krb5_auth;
}

=head2 set_public_key

=head2 get_public_key

Sets and gets the value of the C<$public_key> configuration variable.

This controls which L<public key|Rex::Commands#public_key> Rex should use when using L<Net::SSH2> for connections.

Default is C<undef>.

=cut

sub set_public_key {
  my $class = shift;
  $public_key = shift;
}

sub has_public_key {
  return get_public_key();
}

sub get_public_key {
  if ( exists $ENV{REX_PUBLIC_KEY} ) { return $ENV{REX_PUBLIC_KEY}; }
  if ($public_key) {
    return $public_key;
  }

  return;
}

=head2 set_private_key

=head2 get_private_key

Sets and gets the value of the C<$private_key> configuration variable.

This controls which L<private key|Rex::Commands#private_key> Rex should use with L<Rex::Commands::Rsync> or when using L<Net::SSH2> for connections.

Default is C<undef>.

=cut

sub set_private_key {
  my $class = shift;
  $private_key = shift;
}

sub has_private_key {
  return get_private_key();
}

sub get_private_key {
  if ( exists $ENV{REX_PRIVATE_KEY} ) { return $ENV{REX_PRIVATE_KEY}; }
  if ($private_key) {
    return $private_key;
  }

  return;
}

=head2 set_parallelism

=head2 get_parallelism

Sets and gets the value of the C<$parallelism> configuration variable.

This controls how many hosts Rex should connect to in L<parallel|Rex::Commands#parallelism>.

Default is C<1>.

=cut

sub set_parallelism {
  my $class = shift;
  $parallelism = $_[0];
}

sub get_parallelism {
  my $class = shift;
  return $parallelism || 1;
}

=head2 set_log_filename

=head2 get_log_filename

Sets and gets the value of the C<$log_filename> configuration variable.

This controls which file Rex should use for L<logging|Rex::Commands#logging>.

Default is C<undef>.

=cut

sub set_log_filename {
  my $class = shift;
  $log_filename = shift;
}

sub get_log_filename {
  my $class = shift;
  return $log_filename;
}

=head2 set_log_facility

=head2 get_log_facility

Sets and gets the value of the C<$log_facility> configuration variable.

This controls which log facility Rex should use when L<logging|Rex::Commands#logging> to syslog.

Default is C<'local0'>.

=cut

sub set_log_facility {
  my $class = shift;
  $log_facility = shift;
}

sub get_log_facility {
  my $class = shift;
  return $log_facility || "local0";
}

=head2 set_environment

=head2 get_environment

Sets and gets the value of the C<$environment> configuration variable.

This controls which L<environment|Rex::Commands#environment> Rex should use.

Default is C<''>.

=cut

sub set_environment {
  my ( $class, $env ) = @_;
  $environment = $env;
}

sub get_environment {
  return $environment || "";
}

sub get_ssh_config_username {
  my $class = shift;
  my $param = {@_};

  if ( exists $param->{server}
    && exists $SSH_CONFIG_FOR{ $param->{server} }
    && exists $SSH_CONFIG_FOR{ $param->{server} }->{user} )
  {
    return $SSH_CONFIG_FOR{ $param->{server} }->{user};
  }

  return 0;
}

sub get_ssh_config_hostname {
  my $class = shift;
  my $param = {@_};

  if ( exists $param->{server}
    && exists $SSH_CONFIG_FOR{ $param->{server} }
    && exists $SSH_CONFIG_FOR{ $param->{server} }->{hostname} )
  {
    if ( $SSH_CONFIG_FOR{ $param->{server} }->{hostname} =~ m/^\%h(\.(.*))?/ ) {
      return $param->{server} . $1;
    }
    else {
      return $SSH_CONFIG_FOR{ $param->{server} }->{hostname};
    }
  }

  return 0;
}

sub get_ssh_config_private_key {
  my $class = shift;
  my $param = {@_};

  if ( exists $param->{server}
    && exists $SSH_CONFIG_FOR{ $param->{server} }
    && exists $SSH_CONFIG_FOR{ $param->{server} }->{identityfile} )
  {

    my $file     = $SSH_CONFIG_FOR{ $param->{server} }->{identityfile};
    my $home_dir = _home_dir();
    $file =~ s/^~/$home_dir/;

    return $file;
  }

  return 0;
}

sub get_ssh_config_public_key {
  my $class = shift;
  my $param = {@_};

  if ( exists $param->{server}
    && exists $SSH_CONFIG_FOR{ $param->{server} }
    && exists $SSH_CONFIG_FOR{ $param->{server} }->{identityfile} )
  {
    my $file     = $SSH_CONFIG_FOR{ $param->{server} }->{identityfile} . ".pub";
    my $home_dir = _home_dir();
    $file =~ s/^~/$home_dir/;
    return $file;
  }

  return 0;
}

sub get_connection_type {
  my $class = shift;

  if ( $^O !~ m/^MSWin/ && !$connection_type && $use_net_openssh_if_present ) {
    my $has_net_openssh = 0;
    eval {
      Net::OpenSSH->require;
      Net::SFTP::Foreign->require;
      $has_net_openssh = 1;
      1;
    };

    if ($has_net_openssh) {
      Rex::Logger::debug(
        "Found Net::OpenSSH and Net::SFTP::Foreign - using it as default");
      $connection_type = "OpenSSH";
      return "OpenSSH";
    }
  }

  if ( !$connection_type ) {
    my $has_net_ssh2 = 0;
    eval {
      Net::SSH2->require;
      $has_net_ssh2 = 1;
      1;
    };

    if ($has_net_ssh2) {
      $connection_type = "SSH";
      return "SSH";
    }
  }

  return $connection_type || "SSH";
}

sub get_ca {
  my $class = shift;
  return $ca_file || "";
}

sub get_ca_cert {
  my $class = shift;
  return $ca_cert || "";
}

sub get_ca_key {
  my $class = shift;
  return $ca_key || "";
}

=head2 set_distributor

=head2 get_distributor

Sets and gets the value of the C<$distributor> configuration variable.

This controls which method Rex should use for distributing tasks for parallel execution.

Default is C<'Base'>.

=cut

sub set_distributor {
  my $class = shift;
  $distributor = shift;
}

sub get_distributor {
  my $class = shift;
  return $distributor || "Base";
}

=head2 set_template_function

=head2 get_template_function

Sets and gets the value of the C<$template_function> configuration variable.

This controls the function to be used for rendering L<templates|Rex::Commands::File#template>. The value should be a subroutine reference that will be called with passing two scalar references as positional arguments: first is template content, second is template variables.

Default is determined by the following logic:

=over 4

=item * if L<Rex::Template::NG> is loadable and L<use_template_ng|Rex::Config#get_use_template_ng> is true, use that

=item * fall back to L<Rex::Template> otherwise

=back

=cut

sub set_template_function {
  my $class = shift;
  ($template_function) = @_;
}

sub get_template_function {
  if ( ref($template_function) eq "CODE" ) {
    return sub {
      my ( $content, $template_vars ) = @_;
      $template_vars = {
        Rex::Commands::task()->get_opts,
        (
          Rex::Resource->is_inside_resource
          ? %{ Rex::Resource->get_current_resource()->get_all_parameters }
          : ()
        ),
        %{ $template_vars || {} }
        }
        if ( Rex::Commands::task() );
      return $template_function->( $content, $template_vars );
    };
  }

  if ( Rex::Template::NG->is_loadable && get_use_template_ng() ) {

    # new template engine
    return sub {
      my ( $content, $template_vars ) = @_;
      $template_vars = {
        Rex::Commands::task()->get_opts,
        (
          Rex::Resource->is_inside_resource
          ? %{ Rex::Resource->get_current_resource()->get_all_parameters }
          : ()
        ),
        %{ $template_vars || {} }
        }
        if ( Rex::Commands::task() );
      Rex::Template::NG->require;
      my $t = Rex::Template::NG->new;
      return $t->parse( $content, %{$template_vars} );
    };
  }

  return sub {
    my ( $content, $template_vars ) = @_;
    $template_vars = {
      Rex::Commands::task()->get_opts,
      (
        Rex::Resource->is_inside_resource
        ? %{ Rex::Resource->get_current_resource()->get_all_parameters }
        : ()
      ),
      %{ $template_vars || {} }
      }
      if ( Rex::Commands::task() );
    use Rex::Template;
    my $template = Rex::Template->new;
    return $template->parse( $content, $template_vars );
  };
}

=head2 set_no_tty

=head2 get_no_tty

Sets and gets the value of the C<$no_tty> configuration variable.

This controls whether Rex should request a terminal when using L<Net::SSH2> or allocate a pseudo-tty for the remote process when using L<Net::OpenSSH>.

Default is C<undef>.

=cut

sub set_no_tty {
  shift;
  $no_tty = shift;
}

sub get_no_tty {
  return $no_tty;
}

=head2 set_allow_empty_groups

=head2 get_allow_empty_groups

Sets and gets the value of the C<$allow_empty_groups> configuration variable.

This controls whether Rex should allow empty L<groups of hosts|Rex::Commands#group> or not.

Default is C<0>.

=cut

sub set_allow_empty_groups {
  my ( $class, $set ) = @_;
  if ($set) {
    $allow_empty_groups = 1;
  }
  else {
    $allow_empty_groups = 0;
  }
}

sub get_allow_empty_groups {
  if ($allow_empty_groups) {
    return 1;
  }

  return 0;
}

=head2 set_use_server_auth

=head2 get_use_server_auth

Sets and gets the value of the C<$use_server_auth> configuration variable.

This controls whether Rex should use server-specific authentication information from group files.

Default is C<0>.

=cut

sub set_use_server_auth {
  my ( $class, $set ) = @_;
  if ($set) {
    $use_server_auth = 1;
  }
  else {
    $use_server_auth = 0;
  }
}

sub get_use_server_auth {
  if ($use_server_auth) {
    return 1;
  }

  return 0;
}

=head2 set_waitpid_blocking_sleep_time

=head2 get_waitpid_blocking_sleep_time

Sets and gets the value of the C<$waitpid_blocking_sleep_time> configuration variable.

This controls how many seconds Rex should sleep between checking forks.

Default is C<0.1>.

=cut

sub set_waitpid_blocking_sleep_time {
  my $self = shift;
  $waitpid_blocking_sleep_time = shift;
}

sub get_waitpid_blocking_sleep_time {
  return $waitpid_blocking_sleep_time // 0.1;
}

=head2 set_write_utf8_files

=head2 get_write_utf8_files

Sets and gets the value of the C<$write_utf8_files> configuration variable.

This controls whether Rex should force C<UTF-8> encoding when writing files.

Default is C<undef>.

=cut

sub set_write_utf8_files {
  my $self = shift;
  $write_utf8_files = shift;
}

sub get_write_utf8_files {
  return $write_utf8_files;
}

=head2 set_default_auth

=head2 get_default_auth

Sets and gets the value of the C<$default_auth> configuration variable.

This controls whether Rex should attach default authentication info to tasks.

Default is C<1>.

=cut

sub set_default_auth {
  my $self = shift;
  $default_auth = shift;
}

sub get_default_auth {
  return $default_auth // 1;
}

=head2 register_set_handler($handler_name, $code)

Register a handler that gets called by I<set>.

 Rex::Config->register_set_handler("foo", sub {
   my ($value) = @_;
   print "The user set foo -> $value\n";
 });

And now you can use this handler in your I<Rexfile> like this:

 set foo => "bar";

=cut

sub register_set_handler {
  my ( $class, $handler_name, $code ) = @_;
  $SET_HANDLER->{$handler_name} = $code;
}

sub set {
  my ( $class, $var, $data ) = @_;

  if ( exists( $SET_HANDLER->{$var} ) ) {
    shift;
    shift;
    return &{ $SET_HANDLER->{$var} }(@_);
  }

  if ($set_no_append) {
    $set_param->{$var} = $data;
  }
  else {
    if ( ref($data) eq "HASH" ) {
      if ( !ref( $set_param->{$var} ) ) {
        $set_param->{$var} = {};
      }
      for my $key ( keys %{$data} ) {
        $set_param->{$var}->{$key} = $data->{$key};
      }
    }
    elsif ( ref($data) eq "ARRAY" ) {
      push( @{ $set_param->{$var} }, @{$data} );
    }
    else {
      $set_param->{$var} = $data;
    }
  }
}

sub unset {
  my ( $class, $var ) = @_;
  $set_param->{$var} = undef;
  delete $set_param->{$var};
}

sub get {
  my ( $class, $var ) = @_;
  $var or return;
  if ( exists $set_param->{$var} ) {
    return $set_param->{$var};
  }
}

sub get_all {
  my ($class) = @_;
  return $set_param;
}

=head2 register_config_handler($topic, $code)

With this function it is possible to register own sections in the users config file (C<$HOME/.rex/config.yml>).

Example:

 Rex::Config->register_config_handler("foo", sub {
  my ($param) = @_;
  print "bar is: " . $param->{bar} . "\n";
 });

And now the user can set this in his configuration file:

 base:
   user: theuser
   password: thepassw0rd
 foo:
   bar: baz

=cut

sub register_config_handler {
  my ( $class, $topic, $code ) = @_;

  if ( !ref($HOME_CONFIG) ) { $HOME_CONFIG = {}; }
  $HOME_CONFIG->{$topic} = $code;

  if ( ref($HOME_CONFIG_YAML) && exists $HOME_CONFIG_YAML->{$topic} ) {
    &$code( $HOME_CONFIG_YAML->{$topic} );
  }
}

sub read_config_file {
  my ($config_file) = @_;
  $config_file ||= _home_dir() . "/.rex/config.yml";

  if ( -f $config_file ) {
    my $yaml = eval { local ( @ARGV, $/ ) = ($config_file); <>; };
    eval { $HOME_CONFIG_YAML = Load($yaml); };

    if ($@) {
      print STDERR "Error loading $config_file\n";
      print STDERR "$@\n";
      exit 2;
    }

    for my $key ( keys %{$HOME_CONFIG} ) {
      if ( exists $HOME_CONFIG_YAML->{$key} ) {
        my $code = $HOME_CONFIG->{$key};
        &$code( $HOME_CONFIG_YAML->{$key} );
      }
    }
  }
}

sub read_ssh_config_file {
  my ($config_file) = @_;
  $config_file ||= _home_dir() . '/.ssh/config';

  if ( -f $config_file ) {
    my @lines = eval { local (@ARGV) = ($config_file); <>; };
    %SSH_CONFIG_FOR = _parse_ssh_config(@lines);
  }
}

sub _parse_ssh_config {
  my (@lines) = @_;

  my %ret = ();

  my ( @host, $in_host );
  for my $line (@lines) {
    chomp $line;
    next if ( $line =~ m/^\s*#/ );
    next if ( $line =~ m/^\s*$/ );

    if ( $line =~ m/^Host(?:\s*=\s*|\s+)(.*)$/i ) {
      my $host_tmp = $1;
      @host    = split( /\s+/, $host_tmp );
      $in_host = 1;
      for my $h (@host) {
        $ret{$h} = {};
      }
      next;
    }
    elsif ($in_host) {

      #my ($key, $val) = ($line =~ m/^\s*([^\s]+)\s+=?\s*(.*)$/);
      $line =~ s/^\s*//g;
      my ( $key, $val_tmp ) = split( /[\s=]/, $line, 2 );
      $val_tmp =~ s/^[\s=]+//g;
      my $val = $val_tmp;

      $val =~ s/^\s+//;
      $val =~ s/\s+$//;
      for my $h (@host) {
        $ret{$h}->{ lc($key) } = $val;
      }
    }
  }

  return %ret;
}

sub import {
  read_ssh_config_file();
  read_config_file();
}

_register_config_handlers();

sub _register_config_handlers {
  __PACKAGE__->register_config_handler(
    base => sub {
      my ($param) = @_;

      for my $key ( keys %{$param} ) {

        if ( $key eq "keyauth" ) {
          $key_auth = $param->{keyauth};
          next;
        }

        if ( $key eq "passwordauth" ) {
          $password_auth = $param->{passwordauth};
          next;
        }

        if ( $key eq "passauth" ) {
          $password_auth = $param->{passauth};
          next;
        }

        my $ref_to_key        = qualify_to_ref( $key, __PACKAGE__ );
        my $ref_to_key_scalar = *{$ref_to_key}{SCALAR};

        ${$ref_to_key_scalar} = $param->{$key};
      }
    }
  );
}

_register_set_handlers();

sub _register_set_handlers {
  my @set_handler =
    qw/user password private_key public_key -keyauth -passwordauth -passauth
    parallelism sudo_password connection ca cert key distributor
    template_function port waitpid_blocking_sleep_time/;
  for my $hndl (@set_handler) {
    __PACKAGE__->register_set_handler(
      $hndl => sub {
        my ($val) = @_;
        if ( $hndl =~ m/^\-/ ) {
          $hndl = substr( $hndl, 1 );
        }
        if ( $hndl eq "keyauth" ) { $hndl = "key_auth"; $val = 1; }
        if ( $hndl eq "passwordauth" || $hndl eq "passauth" ) {
          $hndl = "password_auth";
          $val  = 1;
        }
        if ( $hndl eq "connection" ) { $hndl = "connection_type"; }
        if ( $hndl eq "ca" )         { $hndl = "ca_file"; }
        if ( $hndl eq "cert" )       { $hndl = "ca_cert"; }
        if ( $hndl eq "key" )        { $hndl = "ca_key"; }

        my $ref_to_hndl        = qualify_to_ref( $hndl, __PACKAGE__ );
        my $ref_to_hndl_scalar = *{$ref_to_hndl}{SCALAR};

        ${$ref_to_hndl_scalar} = $val;
      }
    );
  }
}

sub _home_dir {
  if ( $^O =~ m/^MSWin/ ) {
    return $ENV{'USERPROFILE'};
  }

  return $ENV{'HOME'} || "";
}

1;