Attempting to run a command with shell returns syntax error

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

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.

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

James

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

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