# Attempting to run a command with shell returns syntax error

**URL:** <https://forum.ansible.com/t/attempting-to-run-a-command-with-shell-returns-syntax-error/12343>\
**Category:** Ansible Project\
**Tags:** ansible-project\
**Created:** [February 12, 2013, 8:22pm UTC](https://forum.ansible.com/t/attempting-to-run-a-command-with-shell-returns-syntax-error/12343 "2013-02-12T20:22:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [February 12, 2013, 8:22pm UTC](https://forum.ansible.com/t/attempting-to-run-a-command-with-shell-returns-syntax-error/12343/1 "2013-02-12T20:22:24Z")

</div>

Hi Dale,

If you want to get the last octet of the IP, I would probably try to  
get it from one of the built in facts, like $ansible\_ipaddress\_eth0.

You can see available facts from "ansible hostname -m setup".

These are (by default) gathered automatically when each play runs.

Do you want to use that value in a template or some other way?

To use that in a template should be pretty straightforward, i.e.

{{ ansible\_all\_ipv4\_addresses[0].split(".")[-1] }}

Let me know if that helps or you need more info!

--Michael

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [February 12, 2013, 8:38pm UTC](https://forum.ansible.com/t/attempting-to-run-a-command-with-shell-returns-syntax-error/12343/2 "2013-02-12T20:38:48Z")

</div>

I think you are missing escaping the quotes around echo  
"IPADDR=10.1.1.$privip".

In any case, using template or lineinfile modules with ansible facts  
should be easier to use.

---

<div class="post-metadata">

**Author:** ![James8](https://avatars.discourse-cdn.com/v4/letter/j/ec9cab/32.png) [@James8](https://forum.ansible.com/u/James8)\
**Post date:** [February 12, 2013, 8:42pm UTC](https://forum.ansible.com/t/attempting-to-run-a-command-with-shell-returns-syntax-error/12343/3 "2013-02-12T20:42:41Z")

</div>

Dale and I were under the impression that you couldn’t use a variable with lineinfile?

James

---

<div class="post-metadata">

**Author:** ![Dale\_Campbell](https://avatars.discourse-cdn.com/v4/letter/d/e79b87/32.png) [@Dale\_Campbell](https://forum.ansible.com/u/Dale_Campbell)\
**Post date:** [February 12, 2013, 9:06pm UTC](https://forum.ansible.com/t/attempting-to-run-a-command-with-shell-returns-syntax-error/12343/4 "2013-02-12T21:06:57Z")

</div>

Thanks for the quick response Michael,

I tried a rewrite and I got to the point with lineinfile where it inserts the total eth0 IP address. However I just can’t seem to get the last octet. I considered a template, but some of the other entries in the file I want to keep such as HWADDR= and think it overkill to have to pull all those items each time when they are pre-populated. As James stated we originally believed we couldn’t pass variables.

Can i get just the last octet with the lineinfile using ansible variables or is my best solution writing out a template?

Thanks

---

<div class="post-metadata">

**Author:** ![Timothy\_Gerla](https://avatars.discourse-cdn.com/v4/letter/t/977dab/32.png) [@Timothy\_Gerla](https://forum.ansible.com/u/Timothy_Gerla)\
**Post date:** [March 20, 2013, 6:10pm UTC](https://forum.ansible.com/t/attempting-to-run-a-command-with-shell-returns-syntax-error/12343/5 "2013-03-20T18:10:14Z")

</div>

> > To use that in a template should be pretty straightforward, i.e.
> > 
> > {{ ansible\_all\_ipv4\_addresses[0].split(".")[-1] }}
> 
> how i have to do this when i want to have the first three octets?  
> how i that done with j2?

You should be able to something like:

{{ ".".join(ansible\_all\_ipv4\_addresses[0].split(".")[0:3]) }}

(Python-style)

Or maybe:

{{ ansible\_all\_ipv4\_addresses[0].split(".")[0:3]|join(".") }}

(Jinja2 style)

The [0:3] is a Python idiom for getting sublists from lists.

-Tim
