hercules-team / augeas

A configuration editing tool and API
http://augeas.net/
GNU Lesser General Public License v2.1
486 stars 199 forks source link

defvar not working in command file #414

Closed bsutton closed 8 years ago

bsutton commented 8 years ago

I'm using augtool version Xxx. I have the following aug file: augmaster.aug

defvar mysqldconf /home/me/dr/test/mysqld.cnf
set /augeas/load/Mysql/lens "mysql.lns"
set /augeas/load/Mysql/incl $mysqldconf
load
set /files/$mysqldconf/target[2]/bind-address 0.0.0.0
set /files/$mysqldconf/target[2]/log-bin /var/log/mysql/mysql-bin.log

set /files/$mysqldconf/target[2]/server-id 1
set /files/$mysqldconf/target[2]/expire_logs_days 10
set /files/$mysqldconf/target[2]/binlog_format ROW
set /files/$mysqldconf/target[2]/logn_bin_trust_function_creators 1

I run the above aug file with:

augtool -s --new --noautoload --file augmaster.aug -i

The problem is that whilst defvar sets mysqldconf correctly and the first reference to $mysqldconf works as expected in the line:

set /augeas/load/Mysql/incl $mysqldconf

subsequent references to $mysqldconf in each of the set commands are not expanded. Is it a problem with spacing?

What I doing something wrong here.

P.S. the documentation needs a bit of work :)

bsutton commented 8 years ago

Oh and I'm using

augtool --version
augtool 1.4.0 <http://augeas.net/>
Copyright (C) 2007-2011 David Lutterkort
License LGPLv2+: GNU LGPL version 2.1 or later
                 <http://www.gnu.org/licenses/lgpl-2.1.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by David Lutterkort
bsutton commented 8 years ago

on ubuntu 12.04

lutter commented 8 years ago

The problem is a misunderstanding of how variables work in Augeas: variables are not simply strings, and when you evaluate /files/$x they don't get expanded by interpolating a string.

Instead, what happens is that when you do defvar x /files/etc/hosts, x gets set to whatever nodes match that path, i.e., x now holds references to nodes in the tree. In your example, when you do defvar mysqldconf /home/me/dr/test/mysqld.cnf, Augeas searches the tree for a node at that path, which doesn't exist and mysqldconf now references no nodes at all. What you want to do instead is something like

defvar mysqldconf /files/home/me/dr/test/mysqld.cnf
set /augeas/load/Mysql/lens "mysql.lns"
set /augeas/load/Mysql/incl /home/me/dr/test/mysqld.cnf
load
set $mysqldconf/target[2]/bind-address 0.0.0.0
set $mysqldconf/target[2]/log-bin /var/log/mysql/mysql-bin.log

set $mysqldconf/target[2]/server-id 1
set $mysqldconf/target[2]/expire_logs_days 10
set $mysqldconf/target[2]/binlog_format ROW
set $mysqldconf/target[2]/logn_bin_trust_function_creators 1

Let me know if the above works.

If variables that just do string interpolation are really important to you, could you please file a separate ticket ? So far, most people have done that by having something manipulate the sequence of commands they want to issue (e.g., from a shell script that wraps the Augeas invocation)

And yes, I totally agree that the documentation needs work, and I would say not just a bit, but major amounts ;)

bsutton commented 8 years ago

OK, got it working but not really what I was expecting. I should note that the doco states:

defvar
Variables can be defined for later use in path expressions

So some clarity around that would be good.

I will create a separate issue for creating variables.

I should perhaps add that I just about gave up on using augtool. I was expecting to be able to modify any ini file not just files defined by a lens. I also note that the standard ini lens is not usable by default. I understand that ini isn't much of a standard and I like the ideal of lens but as a walkup start its a pain. I will raise a separate issue on this topic.

Thanks for the prompt response.