# module development: freestyle parameters

**URL:** <https://forum.ansible.com/t/module-development-freestyle-parameters/14031>\
**Category:** Ansible Project\
**Created:** [November 29, 2013, 6:49am UTC](https://forum.ansible.com/t/module-development-freestyle-parameters/14031 "2013-11-29T06:49:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dimon](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dimon](https://forum.ansible.com/u/Dimon)\
**Post date:** [November 29, 2013, 6:49am UTC](https://forum.ansible.com/t/module-development-freestyle-parameters/14031/1 "2013-11-29T06:49:53Z")

</div>

Hi,

I’m trying to extend yum module to allow passing some “extra” options to it that are provided by some of the yum plugins. I would rather avoid enumeration of all possible options (some I don’t even know what they will be!) What is the best way to reflect “freestyle” options like that in module definition:

```auto
module = AnsibleModule(
    argument_spec = dict(
        state = dict(default='present', choices=['present', 'absent']),
        name = dict(required=True),
        enabled = dict(required=True, choices=BOOLEANS),
        something = dict(aliases=['whatever'])

```

```auto
        **freestyle = dict()....
    )
)

```

```auto

```

```auto
see that "**freestyle"? Is there a construct I can use to say: if you encounter any unknown arguments - just register them for later analysis? Then in my code I can go through supplied arguments and transform them from "foo=bar" into ['--foo','bar'] parameters I will add to yum_cmd. 

```

```auto

```

```auto
I realize that I can just forgo "magic" AnsibleModule piece and go "manual" but I would rather retain most of yum module as-is since it's a good module and no need to fork it if I don't have to.

```

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 29, 2013, 4:26pm UTC](https://forum.ansible.com/t/module-development-freestyle-parameters/14031/2 "2013-11-29T16:26:41Z")

</div>

I’m thinking it might be better to be more explicit, and have something like:

extra\_yum\_flags=“–foo asdf --bar xyz”

?

---

<div class="post-metadata">

**Author:** ![Dimon](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dimon](https://forum.ansible.com/u/Dimon)\
**Post date:** [December 2, 2013, 8:48pm UTC](https://forum.ansible.com/t/module-development-freestyle-parameters/14031/3 "2013-12-02T20:48:08Z")

</div>

IMO it makes things a bit less consistant (since half of options are passed normally via args and other stuff is “extra\_yum\_flags” also it makes module bit heavier having to parse getopt args for yum to deconstruct/reconstruct additional params. that is why I was looking for something like my original post. However I got the feeling at the moment it’s a no-go so may have to stick with extra\_yum\_flags…

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [December 2, 2013, 8:55pm UTC](https://forum.ansible.com/t/module-development-freestyle-parameters/14031/4 "2013-12-02T20:55:25Z")

</div>

Dynamic flag matching would make a typo in another flag very hard to detect

– Michael

---

<div class="post-metadata">

**Author:** ![C\_Morgan\_Hamill](https://avatars.discourse-cdn.com/v4/letter/c/ebca7d/32.png) [@C\_Morgan\_Hamill](https://forum.ansible.com/u/C_Morgan_Hamill)\
**Post date:** [December 2, 2013, 9:13pm UTC](https://forum.ansible.com/t/module-development-freestyle-parameters/14031/5 "2013-12-02T21:13:28Z")

</div>

Third option, which I've done in a module for my own use: have an  
"extra\_yum\_flags" option which can take a dictionary as an argument:

&nbsp;&nbsp;&nbsp;&nbsp;- yum:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: installed  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: mypkg  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;extra\_yum\_flags:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foo: bar  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bam: baz  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;red\_hat: is an operating system

In my module I also happened to allow the extra argument to be taken as  
a string. You'll have to do something like prepending '--' to all the  
keys in the dict and replacing '\_' with '-' or somesuch, but this is  
doable.

For my money, the "extra\_yum\_flag='--foo bar --bam baz" option seems the  
simplest. It's not too much code, however, to notice if the argument is  
a dict and deal with it appropriately. In my code, I did:

&nbsp;&nbsp;&nbsp;&nbsp;def parse\_extra\_args(args):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parsed\_args = ''

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if isinstance(args, dict):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for k, v in args.iteritems():  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parsed\_args += '--' + k.replace('\_', '-') + ' ' + v  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elif isinstance(args, basestring):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parsed\_args = args

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return parsed\_args

Also, you shouldn't need to parse the parameters if they're provided in  
a string, no? Just pass then straight onto the yum command:

&nbsp;&nbsp;&nbsp;&nbsp;cmd = yum\_basecmd + [extra\_yum\_args, 'install', pkg]

Or what have you.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [December 2, 2013, 9:15pm UTC](https://forum.ansible.com/t/module-development-freestyle-parameters/14031/6 "2013-12-02T21:15:56Z")

</div>

This is overkill btw.

Just do type='dict' in the argument\_spec, Ansible knows what to do and  
will take a dict or key=value strings inside the option.

This is how the ec2 module does instance tags now

-- Michael

---

<div class="post-metadata">

**Author:** ![C\_Morgan\_Hamill](https://avatars.discourse-cdn.com/v4/letter/c/ebca7d/32.png) [@C\_Morgan\_Hamill](https://forum.ansible.com/u/C_Morgan_Hamill)\
**Post date:** [December 2, 2013, 10:05pm UTC](https://forum.ansible.com/t/module-development-freestyle-parameters/14031/7 "2013-12-02T22:05:25Z")

</div>

Excerpts from Michael DeHaan's message of 2013-12-02 16:15:56 -0500:

> This is overkill btw.
> 
> Just do type='dict' in the argument\_spec, Ansible knows what to do and  
> will take a dict or key=value strings inside the option.

Right, should have specified: the code I posted is what allows the  
string-containing-options or the dict simultaneously. If you want  
a dict period, then it is \_far\_ easier.

---

<div class="post-metadata">

**Author:** ![Dimon](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dimon](https://forum.ansible.com/u/Dimon)\
**Post date:** [December 12, 2013, 2:05am UTC](https://forum.ansible.com/t/module-development-freestyle-parameters/14031/8 "2013-12-12T02:05:40Z")

</div>

thanks guys. I ended up developing specialized module in the end based on yum as repoquery used in yum module has problem with passing params to yum plugins. Well it was worth a try 🙂
