I am working on a playbook to join linux systems to Active Directory. I can't seem to find a way to convert the value of ansible_hostname to uppercase. One of the commands I need to run requires the hostname to be supplied in uppercase.
Asked
Active
Viewed 1.1e+01k times
44
-
2What did you try? – 030 Mar 23 '15 at 19:53
-
1I haven't tried anything yet because I have not been able to find a documented way to do it. – grahamjgreen Mar 23 '15 at 19:58
-
Could you add an example, an expected outcome and explain why you need this (context)? Why does the hostname need to be in UPPERCASE? – 030 Mar 23 '15 at 20:05
-
Please add the code snippet you have so far so that people can add the missing code – 030 Mar 23 '15 at 20:13
-
This may belong in stackoverflow – Jacob Evans Oct 25 '16 at 22:17
-
http://stackoverflow.com/questions/23195321/how-to-convert-string-to-uppercase-lowercase-in-jinja2 – Jacob Evans Oct 25 '16 at 22:39
2 Answers
85
As Hector Valverde mentionned, it seems to be
{{ ansible_hostname|upper }}
...rather than "uppercase"
-
29Similarly, there's also `{{ ansible_hostname | lower }}` instead of [Jinja2's documented `lower()`](http://jinja.pocoo.org/docs/dev/templates/#lower) – bmaupin Oct 06 '16 at 15:56
6
In my case to convert the value of a variable to uppercase pipe the variable to upper like so:
{{ ansible_hostname|upper }}
grahamjgreen
- 841
- 2
- 8
- 12
-
3
-
10It should be documented on http://docs.ansible.com/playbooks_filters.html but like many things in Ansible it's assumed you "just know"; I'm often frustrated by this as well. E.g. there are multiple examples of how to define a host variable (ntp server is used as an example) but how to actually *use* such a host variable is not explained anywhere. – wurtel Mar 24 '15 at 10:13
-
3
-
2@wurtel See http://jinja.pocoo.org/docs/templates/#builtin-filters for all the filters that aren't documented directly at Ansible. – Tim Malone Jun 22 '18 at 22:56