我想使用 saltstack 从数据库表(这里是 mysql)中选择数据。在我的测试场景中,我有一个名为 salt-master 的 Linux 主机。在此主机上,数据库、主服务器和从服务器正在运行。
我的目标是用 minion 选择数据,但这在我的环境中不起作用。
1) 您看到表格中的数据。数据本身与场景无关。
MariaDB [salt]> SELECT minion_id,qmgr,object_type,value from mqobjects; +--------------+-------+-------------+-------+ | minion_id | qmgr | object_type | value | +--------------+-------+-------------+-------+ | salt-minion2 | QM200 | port | 14200 | | salt-minion2 | QM201 | port | 14201 | | salt-minion2 | QM201 | port | 15201 | | salt-minion2 | QM201 | qlocal | QL1 | | salt-minion2 | QM201 | qlocal | QL2 | | salt-minion3 | QM301 | port | 14300 | | salt-minion3 | QM301 | port | 14301 | | salt-minion3 | QM302 | port | 14302 | +--------------+-------+-------------+-------+ 8 rows in set (0.00 sec)
2)将执行模块mysql与运行器一起使用可以得到我想要的结果。
salt-master:/srv/salt # salt-run salt.cmd mysql.query salt "SELECT minion_id,qmgr,object_type,value from mqobjects" columns: - minion_id - qmgr - object_type - value query time: ---------- human: 0.4ms raw: 0.00037 results: |_ - salt-minion2 - QM200 - port - 14200 |_ - salt-minion2 - QM201 - port - 14201 |_ - salt-minion2 - QM201 - port - 15201 |_ - salt-minion2 - QM201 - qlocal - QL1 |_ - salt-minion2 - QM201 - qlocal - QL2 |_ - salt-minion3 - QM301 - port - 14300 |_ - salt-minion3 - QM301 - port - 14301 |_ - salt-minion3 - QM302 - port - 14302 rows returned: 8 salt-master:/srv/salt #
3)对 minion 执行相同操作不会引发错误,但也不会产生任何结果。为什么?
salt-master:/srv/salt # salt salt-master mysql.query salt "SELECT minion_id,qmgr,object_type,value from mqobjects" salt-master: ---------- salt-master:/srv/salt #
4)我也尝试使用 sls 文件。
a)检查数据库是否存在
salt-master:/srv/salt # cat cmdb/MqObjects2Grains.sls queryMqObjects: mysql_query.run: - name: salt - database: mysql - query: "SELECT minion_id,qmgr,object_type,value from mqobjects" - check_db_exists: True salt-master:/srv/salt # salt salt-master state.apply cmdb.MqObjects2Grains salt-master: ---------- ID: queryMqObjects Function: mysql_query.run Name: salt Result: None Comment: Database salt is not present Started: 10:32:33.146025 Duration: 31.427 ms Changes:
Summary for salt-master ------------ Succeeded: 1 (unchanged=1) Failed: 0 ------------ Total states run: 1 Total run time: 31.427 ms salt-master:/srv/salt #
b) 不检查数据库是否存在
salt-master:/srv/salt # cat cmdb/MqObjects2Grains.sls queryMqObjects: mysql_query.run: - name: salt - database: mysql - query: "SELECT minion_id,qmgr,object_type,value from mqobjects" - check_db_exists: False salt-master:/srv/salt # salt salt-master state.apply cmdb.MqObjects2Grains salt-master: ---------- ID: queryMqObjects Function: mysql_query.run Name: salt Result: True Comment: {} Started: 10:39:18.539604 Duration: 11.422 ms Changes:
---------- query: Executed Summary for salt-master ------------ Succeeded: 1 (changed=1) Failed: 0 ------------ Total states run: 1 Total run time: 11.422 ms salt-master:/srv/salt #
有人能给我一些提示,我做错了什么吗?那就太好了!
答案1
你可以尝试使用
salt-call mysql.query salt "SELECT minion_id,qmgr,object_type,value from mqobjects"
在 minion 上而不是从 master 调用 minion?看来您获得的数据没有通过总线传输。检查您的模块输出格式。