grains.delval doesn't work as advertised/expected
I spoke with Seth and he confirmed it was a bug and asked that I file it as an issue. Without further ado...
Deleting grains doesn't seem to work like it should in v.17. I run grains.delval but it doesn't actually delete the grain but rather only it’s value. This matters since I have code that not only looks at the value of a grain but acts differently if the grain is present vs. not present (I'm using the grains to encode minion state in a state machine sense, and I need to be able to discretely move between states).
Here’s an example:
[root@salt-master base]# salt 'salt-minion-00*' -v grains.setval foo bar --out json
Executing job with jid 20131009201650172154
-------------------------------------------
{
"salt-minion-00": {
"foo": "bar"
}
}
[root@salt-master base]# salt 'salt-minion-00*' -v grains.delval foo --out json
Executing job with jid 20131009201720910261
-------------------------------------------
{
"salt-minion-00": null
}
[root@salt-master base]# salt 'salt-minion-00*' -v cmd.run "cat /etc/salt/grains"
Executing job with jid 20131009201748806515
-------------------------------------------
salt-minion-00:
foo: null
Although not related to this issue, I'm a big fan of the new grains functions introduced in v.17 (delval, remove, append). Will these functions be made available in the grains states api as well? At present only "present" is available which reproduces the grains.setval functionality.
Thanks.
Adding these additional state functions should be straightforward, someone just needs to do it. I would recommend opening another issue for those functions.
As far as this bug report goes, that definitely looks wrong, and we will look into it and fix it.
Done. Thanks.
Looks like this is fixed with the above pull req!
This should have been fixed in v.17.2 right? It's the first bug fix listed in the v.17.2 release notes: http://docs.saltstack.com/topics/releases/0.17.2.html.
[root@salt-master ~]# salt -v 'salt-minion-0*' grains.setval foo ['bar']
Executing job with jid 20131206180146817732
-------------------------------------------
salt-minion-00:
foo:
bar
salt-minion-01:
foo:
bar
[root@salt-master ~]# salt -v 'salt-minion*' grains.delval foo 'bar' --out json
Executing job with jid 20131206180227034444
-------------------------------------------
{
"salt-minion-00": null
}
{
"salt-minion-01": null
}
[root@salt-master ~]# salt -v 'salt-minion*' cmd.run 'cat /etc/salt/grains'
Executing job with jid 20131206180250256531
-------------------------------------------
salt-minion-01:
foo: null
salt-minion-00:
foo: null
It doesn't look like this bug fix made it into v.17.2. The problem comes in when I delete the last element of a list and then the grain becomes a non-list which cannot be appended later:
[root@salt-master ~]# salt -v 'salt-minion-0*' grains.append foo 'bar2'
Executing job with jid 20131206180816296147
-------------------------------------------
salt-minion-01:
The key foo is not a valid list
salt-minion-00:
The key foo is not a valid list
At the moment, I need to, post initialization, check to see that the grain remains a list before executing every append (since deletes are interleaved) which isn't pretty.
BTW it doesn't matter if I execute delval with the specific string literal I want removed from the list or without specifying the literal. The same outcome results when the last item in a list is deleted:
[root@salt-master ~]# salt -v 'salt-minion-0*' grains.setval foo ['bar']
Executing job with jid 20131206181459576185
-------------------------------------------
salt-minion-00:
foo:
bar
salt-minion-01:
foo:
bar
[root@salt-master ~]# salt -v 'salt-minion-0*' grains.delval foo --out json
Executing job with jid 20131206181511395830
-------------------------------------------
{
"salt-minion-00": null
}
{
"salt-minion-01": null
}
[root@salt-master ~]# salt -v 'salt-minion-0*' cmd.run 'cat /etc/salt/grains'
Executing job with jid 20131206181517182551
-------------------------------------------
salt-minion-00:
foo: null
salt-minion-01:
foo: null
This is also not list specific:
[root@salt-master ~]# salt -v 'salt-minion-0*' grains.setval foo bar
Executing job with jid 20131206182352094181
-------------------------------------------
salt-minion-00:
foo: bar
salt-minion-01:
foo: bar
[root@salt-master ~]# salt -v 'salt-minion-0*' grains.delval foo --out json
Executing job with jid 20131206182357092793
-------------------------------------------
{
"salt-minion-00": null
}
{
"salt-minion-01": null
}
[root@salt-master ~]# salt -v 'salt-minion*' cmd.run 'cat /etc/salt/grains'
Executing job with jid 20131206182402202915
-------------------------------------------
salt-minion-00:
foo: null
salt-minion-01:
foo: null
minion/master versions:
[root@salt-master ~]# salt -v 'salt-minion-0*' cmd.run 'salt-call --versions-report'
Executing job with jid 20131206182831281992
-------------------------------------------
salt-minion-00:
Salt: 0.17.2
Python: 2.6.6 (r266:84292, Sep 4 2013, 07:46:00)
Jinja2: 2.2.1
M2Crypto: 0.20.2
msgpack-python: 0.1.13
msgpack-pure: Not Installed
pycrypto: 2.0.1
PyYAML: 3.10
PyZMQ: 2.2.0.1
ZMQ: 3.2.4
salt-minion-01:
Salt: 0.17.2
Python: 2.6.8 (unknown, Nov 7 2012, 14:47:45)
Jinja2: unknown
M2Crypto: 0.21.1
msgpack-python: 0.1.12
msgpack-pure: Not Installed
pycrypto: 2.3
PyYAML: 3.08
PyZMQ: 2.1.9
ZMQ: 2.2.0
[root@salt-master ~]# salt -v 'salt-master' cmd.run 'salt-call --versions-report'
Executing job with jid 20131206182902966166
-------------------------------------------
salt-master:
Salt: 0.17.2
Python: 2.6.6 (r266:84292, Sep 4 2013, 07:46:00)
Jinja2: 2.2.1
M2Crypto: 0.20.2
msgpack-python: 0.1.13
msgpack-pure: Not Installed
pycrypto: 2.0.1
PyYAML: 3.10
PyZMQ: 2.2.0.1
ZMQ: 3.2.4
I cherry-picked the fix. It looks like the fix might not have completely fixed your issue, however. What's the behavior you wanted in the examples you listed above?
The delval call that would result in the grain taking on a "null" or None value should result in the removal of the grain instead.
Keeping a null value in the grain doesn't accomplish anything. Actually from what I'm seeing, I can't actually see that there has been any change in behavior post bug fix.
I would like to see something like this:
#grains.setval foo bar
#cat /etc/salt/grains
some_other_custom_grain: bar2
foo: bar
#grains.delval foo
#cat /etc/salt/grains
some_other_custom_grain: bar2
Similarly with lists. If the last element in the list is deleted, recording null in the grain is counter-productive. It breaks the other grains functions which depend on the list being a list. The list should just be removed.
#grains.setval foo ['bar']
#cat /etc/salt/grains
some_other_grain: bar2
foo:
- bar
#grains.delval foo
#cat /etc/salt/grains
some_other_grain: bar2
salt '*' grains.delval foo destroy=True On Dec 6, 2013 4:50 PM, "shantanub" [email protected] wrote:
The delval call that would result in the grain taking on a "null" or None value should result in the removal of the grain instead.
Keeping a null value in the grain doesn't accomplish anything. Actually from what I'm seeing, I can't actually see that there has been any change in behavior post bug fix.
I would like to see something like this:
#grains.setval foo bar
#cat /etc/salt/grains some_other_custom_grain: bar2 foo: bar
#grains.delval foo
#cat /etc/salt/grains some_other_custom_grain: bar2
Similarly with lists. If the last element in the list is deleted, recording null in the grain is counter-productive. It breaks the other grains functions which depend on the list being a list. The list should just be removed.
#grains.setval foo ['bar']
#cat /etc/salt/grains some_other_grain: bar2 foo: - bar
#grains.delval foo
#cat /etc/salt/grains some_other_grain: bar2
— Reply to this email directly or view it on GitHubhttps://github.com/saltstack/salt/issues/7872#issuecomment-30039958 .
Does the destroy flag exist?
[root@salt-master ~]# salt -v 'salt-minion-0*' grains.setval foo bar
Executing job with jid 20131207093552073695
-------------------------------------------
salt-minion-01:
foo: bar
salt-minion-00:
foo: bar
[root@salt-master ~]# salt -v 'salt-minion-0*' grains.delval foo destroy=True
Executing job with jid 20131207093612254531
-------------------------------------------
salt-minion-01:
ERROR executing grains.delval: The following keyword arguments are not valid: destroy=True
salt-minion-00:
ERROR executing grains.delval: The following keyword arguments are not valid: destroy=True
Just went through the docs and it looks like the new flag is "destructive" rather than destroy. It's the same flag for delval and remove.
[root@salt-master ~]# salt -v 'salt-minion-0*' grains.setval foo ['bar']
Executing job with jid 20131207094559379800
-------------------------------------------
salt-minion-01:
----------
foo:
- bar
salt-minion-00:
----------
foo:
- bar
[root@salt-master ~]# salt -v 'salt-minion-0*' grains.delval foo destructive=True
Executing job with jid 20131207100238686073
-------------------------------------------
salt-minion-01:
None
salt-minion-00:
None
It looks like this has been resolved. Cool. Thanks guys.
Sorry, destructive=True
http://docs.saltstack.com/ref/modules/all/salt.modules.grains.html#salt.modules.grains.delval On Dec 7, 2013 7:48 AM, "shantanub" [email protected] wrote:
Does the destroy flag exist already?
[root@salt-master ~]# salt -v 'salt-minion-0*' grains.setval foo bar
Executing job with jid 20131207093552073695
salt-minion-01: foo: bar Execution is still running on salt-minion-00.trailmix.net salt-minion-00: foo: bar
[root@salt-master ~]# salt -v 'salt-minion-0*' grains.delval foo destroy=True
Executing job with jid 20131207093612254531
salt-minion-01: ERROR executing grains.delval: The following keyword arguments are not valid: destroy=True salt-minion-00: ERROR executing grains.delval: The following keyword arguments are not valid: destroy=True
Now with grains which are lists, it's perfectly valid for the "last" delete to leave the list as an empty list (this is preferable to null as well). If you would prefer that to deleting the grain itself.
This would look something like this:
#grains.setval foo ['bar','bar2']
#cat /etc/salt/grains some_other_custom_grain: some_value foo: - bar - bar2
#grains.delval foo 'bar'
#cat /etc/salt/grains some_other_grain: bar2 foo: - bar2
#grains.delval foo 'bar2'
#cat /etc/salt/grains some_other_grain: bar2 foo: []
I'm leveraging the fact that grains.append will create a grain if it doesn't exist for the first append, which may not be the best semantic crutch to lean on.
— Reply to this email directly or view it on GitHubhttps://github.com/saltstack/salt/issues/7872#issuecomment-30056300 .
Yup. I saw that when I crawled through the docs a tad bit earlier.
Since this issue was listed as a bug, I assumed (incorrectly) that the default behavior would be changed rather than an additional flag needing to be passed.
It looks like the issue has been resolved. Thanks.
Ah, right, I forgot that it was added with a flag (to preserve existing behavior). Glad it's fixed!