# shell commands and escaping characters

**URL:** <https://forum.ansible.com/t/shell-commands-and-escaping-characters/16445>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [October 6, 2014, 6:36pm UTC](https://forum.ansible.com/t/shell-commands-and-escaping-characters/16445 "2014-10-06T18:36:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Palmieri](https://avatars.discourse-cdn.com/v4/letter/p/85e7bf/32.png) [@Peter\_Palmieri](https://forum.ansible.com/u/Peter_Palmieri)\
**Post date:** [October 6, 2014, 6:36pm UTC](https://forum.ansible.com/t/shell-commands-and-escaping-characters/16445/1 "2014-10-06T18:36:08Z")

</div>

I’m pulling data from EC2 instance metadata (from an IAM instance profile). The shell commands are:

curl [http://169.254.169.254/latest/meta-data/iam/security-credentials/PullCredentials](http://169.254.169.254/latest/meta-data/iam/security-credentials/PullCredentials) \> creds.txt

access=`cat /home/ubuntu/creds.txt | grep "AccessKeyId" | cut -d':' -f2 | tr -d ' ' | tr -d '"' | tr -d ','`

My playbook fragment:

- name: get IAM role creds  
shell: curl [http://169.254.169.254/latest/meta-data/iam/security-credentials/PullCredentials](http://169.254.169.254/latest/meta-data/iam/security-credentials/PullCredentials) \> creds.txt

- name: set env variables  
command: access=`cat /home/ubuntu/creds.txt | grep "AccessKeyId" | cut -d':' -f2 | tr -d ' ' | tr -d '"' | tr -d ','`

When I run this manually, the creds.txt file is created, and the cat | grep | cut etc works fine.

When it runs:

TASK: [get IAM role creds] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [\*\*\*\*]

TASK: [set env variables] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
failed: [\*\*\*\*] =\> {“cmd”: “‘access=`cat' /home/ubuntu/creds.txt '|' grep AccessKeyId '|' cut -d: -f2 '|' tr -d ' ' '|' tr -d '\"' '|' tr -d ',`’”, “failed”: true, “rc”: 2}  
msg: [Errno 2] No such file or directory

If I set everything after command: in quotes, I get a parser error:

ERROR: Syntax Error while loading YAML script, ansible-qa-ami.yml  
Note: The error may actually appear before this position: line 197, column 82

- name: set env variables  
command: ‘access=`cat /home/ubuntu/creds.txt | grep "AccessKeyId" | cut -d':' -f2 | tr -d ' ' | tr -d '"' | tr -d ','`’  
^

So I seem to have an issue with colons, quotes, and/or back ticks. Any suggestions are very welcome.

---

<div class="post-metadata">

**Author:** ![Mark2](https://avatars.discourse-cdn.com/v4/letter/m/e19adc/32.png) [@Mark2](https://forum.ansible.com/u/Mark2)\
**Post date:** [October 7, 2014, 2:17pm UTC](https://forum.ansible.com/t/shell-commands-and-escaping-characters/16445/2 "2014-10-07T14:17:52Z")

</div>

Hi,  
Total newb here but sometimes the simplest mistakes are the longest to find 😉

Have you tried putting a space between -d and ‘:’ ?  
Also, try an online yaml checker like yamllint?

---

<div class="post-metadata">

**Author:** ![Michael\_Peters](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@Michael\_Peters](https://forum.ansible.com/u/Michael_Peters)\
**Post date:** [October 7, 2014, 2:29pm UTC](https://forum.ansible.com/t/shell-commands-and-escaping-characters/16445/3 "2014-10-07T14:29:43Z")

</div>

The command module just takes a simple command. If you need a full  
shell (for pipes, io redirection, etc) use the shell command.  
But in this case it's still not going to do what you want. The  
"access" variable won't be visible to anything outside of that command  
so it's essentially worthless. If you need to capture it and store it,  
look at using "register" and then perhaps a set\_fact, or environment  
keyword.
