問題描述
我想知道社區中是否有人使用 Ansible 操作過 Microsoft SQL Server 數據庫?
I would like to find out if anyone in the community has used Ansible to manipulate a Microsoft SQL Server database?
我們想向我們用 Ansible 編寫的環境配置腳本添加一個任務,該腳本將 INSERT
行插入到 SQL Server 表中.這些行將具有在我們的變量文件中指定的參數.
We want to add a task to our environment configuration scripts written in Ansible that will INSERT
rows into a SQL Server table. The rows will have parameters that are specified in our variables files.
我一直無法找到實現此目的的特定 Ansible 模塊,所以想聽聽是否有人在其他方面取得了成功?
I have been unable to locate a specific Ansible module for achieving this so would like to hear if anyone has has success in some other way?
推薦答案
為了完整起見,這里是 Ansible to SELECT & 中使用的解決方案的語法.插入 Microsoft SQL Server:
For completeness here is the syntax of the solution used in Ansible to SELECT & INSERT for Microsoft SQL Server:
插入
- name: 'insert row to SQL server DB'
win_shell: "invoke-sqlcmd -username \"{{db_user}}\" -password \"{{db_pass}}\" -Query \"INSERT INTO Addresses (DoorNum,Street,Town,PostCode) VALUES ({{ item.doornum }},'{{ item.street }}','{{ item.town }}''{{ item.postcode }}')\""
選擇
- name: 'select from SQL server DB'
win_shell: "invoke-sqlcmd -username \"{{db_user}}\" -password \"{{db_pass}}\" -Query \"SELECT ID FROM Addresses WHERE PostCode = '{{ item.postcode }}'\" | Select-Object * -ExcludeProperty ItemArray, Table, RowError, RowState, HasErrors | ConvertTo-Json"
register: response
- set_fact:
ids: "{{ response.stdout|from_json}}"
- debug:
var: ids
這篇關于Ansible 中的 SQL Server 數據庫查詢的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!