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 > 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 > 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.