On Wed, Feb 4, 2015 at 2:10 AM, Giovanni Tirloni <
g...@gtirloni.com> wrote:
> On Tue, 03 Feb 2015 23:07 -0800, Toshio Kuratomi <
tkur...@ansible.com>
> wrote:
> I faced the issue with the following organization:
>
> # roles/common/vars/main.yml
>
> my_dirs:
> - path: /path/to/dir
> owner: user
> group: group
> mode: 0775
>
> # roles/common/tasks/main.yml
>
> - name: "Create my dirs"
> file:
> path: "{{ item.path }}"
> owner: "{{ item.owner }}"
> group: "{{ item.group }}"
> mode: "{{ item.mode }}"
> state: directory
> with_items: my_dirs
>
> The workaround was to specify the mode between quotes in vars/main.yml.
>
> Thinking now, the {{ }} might be unnecessary is this specific case? I
> think I naturally defaulted to quote everything in in my tasks but might
> not be the right thing to do here.
>
Ahhh.... so here the variable is first defined as a number in a
variable. Then when we want to make use of the variable it becomes a
string because we have to quote it to get jinja2 to process it. That
is unintuitive :-(
There's two ways that I can see to work around that in the playbook --
as you've done, make sure that the value is a string from start to
finish. The other way is to make sure that it is a number from start
to finish by using the jinja2 int filter like this:
file:
mode: "{{ item.mode|int }}"
I think using strings throughout is the easier workaround for people
to understand but both should work.
> Is it right to assume the playbooks are processed in the following way?
>
> [text files] -> [jinja] -> [ansible runner]
>
There's also the yaml parserl in there like this:
[text file] -> PyYAML -> jinja2 -> ansible runner
In my example where vars weren't involved it was pyyaml that was
interpreting the value as a number rather than a string.
>> If we didn't care about backwards compatibility we could tell ansible
>> that the value of mode must be a string. Then we could make sure that
>> we parsed the string as an octal value always. However, that seems
>> like a somewhat high price to pay here since most greybeards
>> understand that this is an octal value and needs to be prepended with
>> a zero in most programming contexts. Middle ground might be to
>> clarify the documentation for the mode parameter. That seems to be
>> the route that languages like php take:
>>
http://php.net/manual/en/function.chmod.php
>
> It looks like a good compromise. Are the docs generated from the
> docstring in the modules or somewhere else?
>
By and large. But file mode parameters for file are documented
specially because they're shared among several different modules.
You'll find them here:
https://github.com/ansible/ansible/blob/devel/lib/ansible/utils/module_docs_fragments/files.py
-Toshio