27
27
required: false
28
28
default: false
29
29
type: bool
30
+ diff:
31
+ description: Display diffs of formatting changes.
32
+ required: false
33
+ default: false
34
+ type: bool
30
35
recursive:
31
36
description: Also process files in subdirectories. By default only the given directory (or current directory) is processed.
32
37
required: false
33
38
default: false
34
39
type: bool
40
+ write:
41
+ description: Write to source files.
42
+ required: false
43
+ default: true
44
+ type: bool
35
45
36
46
requirements:
37
47
- packer >= 1.7.0
40
50
'''
41
51
42
52
EXAMPLES = r'''
43
- # rewrite Packer files in /path/to/packer_dir to canonical format
44
- - name: Rewrite packer files in /path/to/packer_dir to canonical format
53
+ # rewrite Packer files in /path/to/packer_dir to canonical format, display diffs, and do not write to source files
54
+ - name: Rewrite packer files in /path/to/packer_dir to canonical format, display diffs, and do not write to source files
45
55
mschuchard.general.packer_fmt:
46
56
config_dir: /path/to/packer_dir
57
+ diff: true
58
+ write: false
47
59
48
60
# verify canonical formatting of Packer files in /path/to/packer_dir
49
61
- name: Verify canonical formatting of packer files in /path/to/packer_dir
@@ -76,8 +88,11 @@ def main() -> None:
76
88
argument_spec = {
77
89
'check' : {'type' : 'bool' , 'required' : False },
78
90
'config_dir' : {'type' : 'path' , 'required' : False , 'default' : Path .cwd ()},
79
- 'recursive' : {'type' : 'bool' , 'required' : False }
91
+ 'diff' : {'type' : 'bool' , 'required' : False },
92
+ 'recursive' : {'type' : 'bool' , 'required' : False },
93
+ 'write' : {'type' : 'bool' , 'required' : False , 'default' : True },
80
94
},
95
+ mutually_exclusive = [('check' , 'write' )],
81
96
supports_check_mode = True
82
97
)
83
98
@@ -91,11 +106,24 @@ def main() -> None:
91
106
if check :
92
107
flags .append ('check' )
93
108
changed = False
109
+ if module .params .get ('diff' ):
110
+ flags .append ('diff' )
94
111
if module .params .get ('recursive' ):
95
112
flags .append ('recursive' )
96
113
114
+ # check args
115
+ args : dict = {}
116
+ # reminder: the flag that must be argued instead
117
+ # ruff complains so default should protect against falsey with None
118
+ if not module .params .get ('write' ):
119
+ args .update ({'write' : 'false' })
120
+ changed = False
121
+
122
+ # convert ansible params to terraform args
123
+ args = packer .ansible_to_packer (args )
124
+
97
125
# determine packer command
98
- command : list [str ] = packer .cmd (action = 'fmt' , flags = flags , target_dir = config_dir )
126
+ command : list [str ] = packer .cmd (action = 'fmt' , flags = flags , args = args , target_dir = config_dir )
99
127
100
128
# exit early for check mode
101
129
if module .check_mode :
0 commit comments