# Incorrect variable substition in command module cause command to fail

**URL:** <https://forum.ansible.com/t/incorrect-variable-substition-in-command-module-cause-command-to-fail/20733>\
**Category:** Ansible Project\
**Created:** [March 1, 2016, 8:08pm UTC](https://forum.ansible.com/t/incorrect-variable-substition-in-command-module-cause-command-to-fail/20733 "2016-03-01T20:08:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_MacBride](https://avatars.discourse-cdn.com/v4/letter/d/ebca7d/32.png) [@David\_MacBride](https://forum.ansible.com/u/David_MacBride)\
**Post date:** [March 1, 2016, 8:08pm UTC](https://forum.ansible.com/t/incorrect-variable-substition-in-command-module-cause-command-to-fail/20733/1 "2016-03-01T20:08:50Z")

</div>

I’m running some sudo commands via the command module. In one of the commands, when I use a variable to specify the path to a script there is an error during the variable substitution and the command fails. In some commands, the variable substitution works while in one it doesn’t. I have not determined what is different between the examples that work and those that do not.

Here is more detail:

When I run a playbook containing the following two commands  
command: “sudo -u wasadmin {{ was\_install\_dir }}/profiles/{{ was\_profile\_name }}/bin/stopServer.sh {{ was\_server\_name }}”  
command: “sudo -u wasadmin /opt/IBM/WebSphere/UpdateInstaller/update.sh -silent -options {{ was\_maintenance\_response\_file }}”

… both commands run successfully on the remote host. The output of the playbook shows that the correct commands are run:  
TASK: [worklight\_maintenance | Stop the application server] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
\<remote.host.name\> REMOTE\_MODULE command sudo -u wasadmin /opt/IBM/WebSphere/AppServer/profiles/AppSrv01/bin/stopServer.sh server1

TASK: [worklight\_maintenance | Apply WAS maintenance] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
\<remote.host.name\> REMOTE\_MODULE command sudo -u wasadmin /opt/IBM/WebSphere/UpdateInstaller/update.sh -silent -options /opt/build/maintenance/was.maintenance.rsp

But when I change the second command to also use a variable in place of the full path to the script, like so (see highlighed change):  
command: “sudo -u wasadmin {{ was\_install\_dir }}/profiles/{{ was\_profile\_name }}/bin/stopServer.sh {{ was\_server\_name }}”  
command: “sudo -u wasadmin {{ was\_updi\_update\_sh }} -silent -options {{ was\_maintenance\_response\_file }}”

… the second command doesn’t run successfully. In the playbook output we see that the substitution of the variable {{ was\_updi\_update\_sh }} has gone terribly wrong as shown by the highlighted section.  
\<[ecwlapp1qa4.hhgregg.com](http://ecwlapp1qa4.hhgregg.com)\> REMOTE\_MODULE command sudo -u wasadmin /opt/IBM/WebSphere/AppServer/profiles/AppSrv01/bin/stopServer.sh server1  
\<[ecwlapp1qa4.hhgregg.com](http://ecwlapp1qa4.hhgregg.com)\> REMOTE\_MODULE command sudo -u wasadmin {‘invocation’: {‘module\_name’: u’stat’, ‘module\_complex\_args’: {}, ‘module\_args’: u’path=/opt/IBM/WebSphere/UpdateInstaller/update.sh’}, ‘stat’: {‘uid’: 522, ‘exists’: True, ‘woth’: False, ‘mtime’: 1442612966, ‘inode’: 5931018, ‘isgid’: False, ‘size’: 1286, ‘isuid’: False, ‘isreg’: True, ‘pw\_name’: ‘wasadmin’, ‘gid’: 522, ‘ischr’: False, ‘wusr’: True, ‘xoth’: True, ‘rusr’: True, ‘nlink’: 1, ‘issock’: False, ‘rgrp’: True, ‘gr\_name’: ‘wasadmin’, ‘path’: ‘/opt/IBM/WebSphere/UpdateInstaller/update.sh’, ‘xusr’: True, ‘atime’: 1456861468, ‘md5’: ‘142ed49388e43e9f2a7174e9f0974809’, ‘isdir’: False, ‘ctime’: 1442612966, ‘isblk’: False, ‘wgrp’: False, ‘xgrp’: True, ‘dev’: 64773, ‘roth’: True, ‘isfifo’: False, ‘mode’: ‘0755’, ‘checksum’: ‘3f15c2e57403d7d38d5ab5e1111fbc6b3f9c62e5’, ‘islnk’: False}, ‘changed’: False} -silent -options /opt/build/maintenance/was.maintenance.rsp

The variables are all defined in the same way. They are defined in a file like so:

`  
-sh-4.1$ cat roles/worklight\_maintenance/vars/worklight.yml

---

<div class="post-metadata">

**Author:** ![David\_MacBride](https://avatars.discourse-cdn.com/v4/letter/d/ebca7d/32.png) [@David\_MacBride](https://forum.ansible.com/u/David_MacBride)\
**Post date:** [March 2, 2016, 1:10am UTC](https://forum.ansible.com/t/incorrect-variable-substition-in-command-module-cause-command-to-fail/20733/2 "2016-03-02T01:10:58Z")

</div>

I solved the problem. In addition to defining the variable {{ was\_updi\_update\_sh }} in the vars/worklight.yml file I was also using the same name to register the output of a file test (testing whether that file existed). By the time the command was being executed the content of the variable {{ was\_updi\_update\_sh }} had changed.
