Skip to content

Commit

Permalink
Allow accessing the regular mine/event bus from salt-ssh
Browse files Browse the repository at this point in the history
  • Loading branch information
lkubb committed Nov 29, 2023
1 parent a686ce0 commit 837e249
Show file tree
Hide file tree
Showing 6 changed files with 439 additions and 60 deletions.
1 change: 1 addition & 0 deletions changelog/40943.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allowed publishing to regular minions from the SSH wrapper
1 change: 1 addition & 0 deletions changelog/65645.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allowed accessing the regular mine from the SSH wrapper
100 changes: 73 additions & 27 deletions salt/client/ssh/wrapper/mine.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,35 @@
Wrapper function for mine operations for salt-ssh
.. versionadded:: 2015.5.0
.. versionchanged:: 3007.0
In addition to mine returns from roster targets, this wrapper now supports
accessing the regular mine as well.
"""


import copy

import salt.client.ssh
import salt.daemons.masterapi


def get(tgt, fun, tgt_type="glob", roster="flat"):
def get(
tgt, fun, tgt_type="glob", roster="flat", ssh_minions=True, regular_minions=False
):
"""
Get data from the mine based on the target, function and tgt_type
This will actually run the function on all targeted minions (like
This will actually run the function on all targeted SSH minions (like
publish.publish), as salt-ssh clients can't update the mine themselves.
We will look for mine_functions in the roster, pillar, and master config,
in that order, looking for a match for the defined function
in that order, looking for a match for the defined function.
Targets can be matched based on any standard matching system that can be
matched on the defined roster (in salt-ssh) via these keywords::
matched on the defined roster (in salt-ssh).
Regular mine data will be fetched as usual and can be targeted as usual.
CLI Example:
Expand All @@ -30,30 +39,67 @@ def get(tgt, fun, tgt_type="glob", roster="flat"):
salt-ssh '*' mine.get '*' network.interfaces
salt-ssh '*' mine.get 'myminion' network.interfaces roster=flat
salt-ssh '*' mine.get '192.168.5.0' network.ipaddrs roster=scan
salt-ssh myminion mine.get '*' network.interfaces ssh_minions=False regular_minions=True
salt-ssh myminion mine.get '*' network.interfaces ssh_minions=True regular_minions=True
tgt
Target whose mine data to get.
fun
Function to get the mine data of. You can specify multiple functions
to retrieve using either a list or a comma-separated string of functions.
tgt_type
Target type to use with ``tgt``. Defaults to ``glob``.
See :ref:`targeting` for more information for regular minion targets, above
for SSH ones.
roster
The roster module to use. Defaults to ``flat``.
ssh_minions
.. versionadded:: 3007.0
Target minions from the roster. Defaults to true.
regular_minions
.. versionadded:: 3007.0
Target regular minions of the master running salt-ssh. Defaults to false.
"""
# Set up opts for the SSH object
opts = copy.deepcopy(__context__["master_opts"])
minopts = copy.deepcopy(__opts__)
opts.update(minopts)
if roster:
opts["roster"] = roster
opts["argv"] = [fun]
opts["selected_target_option"] = tgt_type
opts["tgt"] = tgt
opts["arg"] = []

# Create the SSH object to handle the actual call
ssh = salt.client.ssh.SSH(opts)

# Run salt-ssh to get the minion returns
rets = {}
for ret in ssh.run_iter(mine=True):
if regular_minions:
masterapi = salt.daemons.masterapi.RemoteFuncs(__context__["master_opts"])
load = {
"id": __opts__["id"],
"fun": fun,
"tgt": tgt,
"tgt_type": tgt_type,
}
ret = masterapi._mine_get(load)
rets.update(ret)

cret = {}
for host in rets:
if "return" in rets[host]:
cret[host] = rets[host]["return"]
else:
cret[host] = rets[host]
return cret
if ssh_minions:
# Set up opts for the SSH object
opts = copy.deepcopy(__context__["master_opts"])
minopts = copy.deepcopy(__opts__)
opts.update(minopts)
if roster:
opts["roster"] = roster
opts["argv"] = [fun]
opts["selected_target_option"] = tgt_type
opts["tgt"] = tgt
opts["arg"] = []

# Create the SSH object to handle the actual call
ssh = salt.client.ssh.SSH(opts)

# Run salt-ssh to get the minion returns
mrets = {}
for ret in ssh.run_iter(mine=True):
mrets.update(ret)

for host in mrets:
if "return" in mrets[host]:
rets[host] = mrets[host]["return"]
else:
rets[host] = mrets[host]
return rets

0 comments on commit 837e249

Please sign in to comment.