Bret
Bret

Reputation: 1475

Text processing in a playbook

I'm working on an ansible playbook to start our provisioning process, and I'm accepting user input for a number of variables, including hostname and ip. Once I have ip, I can calculate the gateway which I need to give to Cobbler, but I can't seem to work out how to turn the ip into a gateway within the constrictions of an ansible playbook. It sure seems like this ought to be possible, but I'm just not seeing it.

Actually, the only solution I've come up with so far is to create a local file using a template (which seems to allow more complex templating than just filters) and then use local_action to execute that file. But is there a better way?

Upvotes: 0

Views: 665

Answers (1)

Bruce P
Bruce P

Reputation: 20749

If all you need to do is change something like 192.168.123.234 to 192.168.1.1 then something along the following would work:

vars:
  ip: '192.168.123.234'

tasks:
- local_action: shell echo {{ ip }} | sed -e 's/\.[0-9]*.[0-9]*.$/.1.1/'
  register: result

- local_action: debug msg={{ result.stdout }}

It's a little ugly but it gets the job done.

Upvotes: 2

Related Questions