# Is it possible to natively convert a 3 letter country code to a 2 letter one (e.g. USA -\> US)

**URL:** <https://forum.ansible.com/t/is-it-possible-to-natively-convert-a-3-letter-country-code-to-a-2-letter-one-e-g-usa-us/28201>\
**Category:** Ansible Project\
**Created:** [July 30, 2018, 7:46pm UTC](https://forum.ansible.com/t/is-it-possible-to-natively-convert-a-3-letter-country-code-to-a-2-letter-one-e-g-usa-us/28201 "2018-07-30T19:46:18Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sabrina\_Lautier](https://avatars.discourse-cdn.com/v4/letter/s/c2a13f/32.png) [@Sabrina\_Lautier](https://forum.ansible.com/u/Sabrina_Lautier)\
**Post date:** [July 30, 2018, 7:46pm UTC](https://forum.ansible.com/t/is-it-possible-to-natively-convert-a-3-letter-country-code-to-a-2-letter-one-e-g-usa-us/28201/1 "2018-07-30T19:46:18Z")

</div>

Hi everyone,

I’m migrating most of my code from python 2.7 to ansible and would like to know if there’s a way to natively convert a 3 letter country code to 2 letter one.

It was really easy to do in python:

`

from iso3166 import countries

country\_code\_2l = countries.get(country\_code\_3l).alpha2

`

, but can’t find a way to do it with ansible.

Format does not work as the correlation table is normalized.

I can of course create a python module that does this, but would like to avoid it if such a feature already exists.

Best regards,  
Sabrina

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 30, 2018, 8:36pm UTC](https://forum.ansible.com/t/is-it-possible-to-natively-convert-a-3-letter-country-code-to-a-2-letter-one-e-g-usa-us/28201/2 "2018-07-30T20:36:55Z")

</div>

I don't think something like that exist, but you could do this

{{ lookup('pipe', 'python -c "from iso3166 import countries; print countries.get(\"NOR\").alpha2"') }}

Or create a filter plugin

country\_code\_3to2.py

---

<div class="post-metadata">

**Author:** ![Sabrina\_Lautier](https://avatars.discourse-cdn.com/v4/letter/s/c2a13f/32.png) [@Sabrina\_Lautier](https://forum.ansible.com/u/Sabrina_Lautier)\
**Post date:** [July 31, 2018, 9:11am UTC](https://forum.ansible.com/t/is-it-possible-to-natively-convert-a-3-letter-country-code-to-a-2-letter-one-e-g-usa-us/28201/3 "2018-07-31T09:11:29Z")

</div>

Thanks a lot Kai, I haven’t thought about this.

The command successfully run as root, in the shell command line:

`

# python -c “from iso3166 import countries; print countries.get(‘USA’).alpha2”

US  
`

So, it looks like we’re definitively on the right tracks, but I can’t managed to pass the variable``` to the function countries.get(), even hard coded (see below):`

`

# Convert the 3 letter country code to 2 letters

- set\_fact:  
tenant\_country\_code\_2l: “{{ lookup(‘pipe’, ‘python -c "from iso3166 import countries; print countries.get("USA").alpha2"’) }}”

TASK [../../roles/isam : set\_fact] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
Traceback (most recent call last):  
File “”, line 1, in   
NameError: name ‘USA’ is not defined  
fatal: [runner\_ansible]: FAILED! =\> {  
“failed”: true  
}

MSG:

An unhandled exception occurred while running the lookup plugin ‘pipe’. Error was a \<class ‘ansible.errors.AnsibleError’\>, original message: lookup\_plugin.pipe(python -c “from iso3166 import countries; print countries.get(“USA”).alpha2”) returned 1

`

Would you know how to do it? I’ve tried several solutions but can’t work this out… I guess I’m missing sthg in ansible…

Best regards,  
Sabrina

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 31, 2018, 9:56am UTC](https://forum.ansible.com/t/is-it-possible-to-natively-convert-a-3-letter-country-code-to-a-2-letter-one-e-g-usa-us/28201/4 "2018-07-31T09:56:28Z")

</div>

The problem is the first quote after the colon, what I do to in these situation is to use multi line YAML since you then don't need the quotes.

&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tenant\_country\_code\_2l: |-  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ lookup('pipe', 'python -c "from iso3166 import countries; print countries.get(\"USA\").alpha2"') }}

---

<div class="post-metadata">

**Author:** ![Sabrina\_Lautier](https://avatars.discourse-cdn.com/v4/letter/s/c2a13f/32.png) [@Sabrina\_Lautier](https://forum.ansible.com/u/Sabrina_Lautier)\
**Post date:** [July 31, 2018, 10:43am UTC](https://forum.ansible.com/t/is-it-possible-to-natively-convert-a-3-letter-country-code-to-a-2-letter-one-e-g-usa-us/28201/5 "2018-07-31T10:43:13Z")

</div>

Thanks Kai for your quick answer.

However ansible does not like this syntax (I’m using version 2.6.2 and can’t update to an higher version).

`

# cat ./roles/tasks/the\_task.yml

# Convert the 3 letter country code to 2 letters

- set\_fact:  
tenant\_country\_code\_2l: |-  
{{ lookup(‘pipe’, ‘python -c “from iso3166 import countries;  
print countries.get("USA").alpha2”’) }}

# ansible-playbook …

ERROR! Syntax Error while loading YAML.

The error appears to have been in ‘/nfs/home/slautier/git/odm-oncloud/operate/roles/isam/tasks/create\_customer\_and\_update\_subscription\_in\_ops\_db.yml’: line 20, column 1, but may  
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

- name: Print the 2 letters country code  
^ here

exception type: \<class ‘yaml.scanner.ScannerError’\>  
exception: while scanning a simple key  
in “”, line 18, column 1  
could not find expected ‘:’  
in “”, line 20, column 1

`

I have never heard of ‘|-’, what is it?

---

<div class="post-metadata">

**Author:** ![Sabrina\_Lautier](https://avatars.discourse-cdn.com/v4/letter/s/c2a13f/32.png) [@Sabrina\_Lautier](https://forum.ansible.com/u/Sabrina_Lautier)\
**Post date:** [July 31, 2018, 10:58am UTC](https://forum.ansible.com/t/is-it-possible-to-natively-convert-a-3-letter-country-code-to-a-2-letter-one-e-g-usa-us/28201/6 "2018-07-31T10:58:36Z")

</div>

It works :)!

`

- set\_fact:  
tenant\_country\_code\_2l: |  
{{ lookup(‘pipe’, ‘python -c “from iso3166 import countries; print countries.get("USA").alpha2”’) }}  
`

Thanks again, I didn’t know about “|” but read the doc [https://docs.ansible.com/ansible/latest/reference\_appendices/YAMLSyntax.html](https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html) and I know now :).

Thanks a thousand.

---

<div class="post-metadata">

**Author:** ![Sabrina\_Lautier](https://avatars.discourse-cdn.com/v4/letter/s/c2a13f/32.png) [@Sabrina\_Lautier](https://forum.ansible.com/u/Sabrina_Lautier)\
**Post date:** [July 31, 2018, 11:29am UTC](https://forum.ansible.com/t/is-it-possible-to-natively-convert-a-3-letter-country-code-to-a-2-letter-one-e-g-usa-us/28201/7 "2018-07-31T11:29:04Z")

</div>

Well, it’s not totally solved as I need know to make the ansible variable recognized to the python command…

This does not work as variable **country\_code** is unknown:

`

- set\_fact:  
country\_code\_2l: |  
{{ lookup(‘pipe’, ‘python -c “print country\_code”’) }}  
`

I guess the question really is: how to pass the ansible variable country\_code to the python command line.  
Any clue will be welcome, I’m still searching/testing… but haven’t found anything yet…

Best,  
Sabrina

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [July 31, 2018, 11:38am UTC](https://forum.ansible.com/t/is-it-possible-to-natively-convert-a-3-letter-country-code-to-a-2-letter-one-e-g-usa-us/28201/8 "2018-07-31T11:38:54Z")

</div>

Hi,

> Well, it's not totally solved as I need know to make the ansible  
> variable recognized to the python command...
> 
> This does not work as variable \*country\_code\* is unknown:  
> - set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;country\_code\_2l: |  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ lookup('pipe', 'python -c "print country\_code"') }}
> 
> I guess the question really is: how to pass the ansible variable  
> country\_code to the python command line.

you should be able to do it like this:

- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;country\_code\_2l: |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ lookup('pipe', 'python -c "print \"' ~ country\_code ~ '\""') }}

The tilde is Jinja's string concatenation. Note that this does no  
sanitizing, so if country\_code happens to include a double quote (or  
backslash), you have a problem.

Cheers,  
Felix

---

<div class="post-metadata">

**Author:** ![Sabrina\_Lautier](https://avatars.discourse-cdn.com/v4/letter/s/c2a13f/32.png) [@Sabrina\_Lautier](https://forum.ansible.com/u/Sabrina_Lautier)\
**Post date:** [July 31, 2018, 12:28pm UTC](https://forum.ansible.com/t/is-it-possible-to-natively-convert-a-3-letter-country-code-to-a-2-letter-one-e-g-usa-us/28201/9 "2018-07-31T12:28:15Z")

</div>

Thanks a million, it works and I learnt a lot (I didn’t know about the ‘~’, I’m pretty new in ansible, I started 1 month ago, still a lot to learn…).  
Best,  
Sabrina

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 31, 2018, 12:32pm UTC](https://forum.ansible.com/t/is-it-possible-to-natively-convert-a-3-letter-country-code-to-a-2-letter-one-e-g-usa-us/28201/10 "2018-07-31T12:32:11Z")

</div>

> It works :)!
> 
> - set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;tenant\_country\_code\_2l: |  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ lookup('pipe', 'python -c "from iso3166 import countries; print  
> countries.get(\"USA\").alpha2"') }}

You need to have the minus sign after the pipe to remove the new line at the end.  
If you add this you'll see that it's there

&nbsp;&nbsp;&nbsp;- debug: var=tenant\_country\_code\_2l

The minus after the pipe say strip newline at the end.

> Thanks again, I didn't know about "|" but read the doc  
> [https://docs.ansible.com/ansible/latest/reference\_appendices/YAMLSyntax.html](https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html)  
> and I know now :).

This answer is a good reference on YAML multi line  
[https://stackoverflow.com/questions/3790454/in-yaml-how-do-i-break-a-string-over-multiple-lines#21699210](https://stackoverflow.com/questions/3790454/in-yaml-how-do-i-break-a-string-over-multiple-lines#21699210)
