簡(jiǎn)介
ansible是自動(dòng)化運(yùn)維工具,基于Python開(kāi)發(fā)。
ansible目前針對(duì)golang提供對(duì)應(yīng)的SDK、API之類(lèi)的。
我們這里可以采用直接調(diào)用ansible-playbook這個(gè)命令執(zhí)行我們的任務(wù)。
python
ansible
ansible
利用golang的os/exec來(lái)執(zhí)行ansible-playbook這個(gè)命令,實(shí)現(xiàn)變量的拼接,由于我習(xí)慣將對(duì)象存在hosts這個(gè)變量里面,因此操作的時(shí)候需要指定hosts這個(gè)變量。
package ansible?import ( "os/exec" "strings" "time")?// RunPlayBook 執(zhí)行通過(guò)ansible-playbook命令執(zhí)行ansible任務(wù)func RunPlayBook(ansiblePath, inventory, yamlfile string, vars []string) (result string, ok bool) { startTime := time.Now() commandStr := []string{ansiblePath, "--ssh-common-args='-o StrictHostKeyChecking=no'", "-i", inventory, yamlfile} for _, k := range vars { commandStr = append(commandStr, "-e") commandStr = append(commandStr, k) } // fmt.Println(commandStr) command := exec.Command(commandStr[0], commandStr[1:]...) errString := "" output, err := command.CombinedOutput() if err != nil { errString = "ERROR: " err.Error() } // fmt.Println(string(output)) recapFlag := False // ok := false for _, line := range strings.Split(string(output), "n") { if strings.TrimSpace(line) == "" { continue } if strings.HasPrefix(line, "PLAY RECAP *") { recapFlag = true } if recapFlag { if strings.Contains(line, "unreachable=0") && strings.Contains(line, "failed=0") { ok = true } } } return strings.Join(commandStr, " ") "n " errString "n " string(output) "n=======================================n開(kāi)始時(shí)間:" startTime.Format("2006-01-02 15:04:05") "n結(jié)束時(shí)間:" time.Now().Format("2006-01-02 15:04:05") "n耗時(shí):" time.Now().Sub(startTime).String(), ok}?
測(cè)試
準(zhǔn)備playbook文件
vim /data/ansible/test.yaml
做一個(gè)簡(jiǎn)單的測(cè)試,將要執(zhí)行的對(duì)象放到hosts這個(gè)變量里面
- name: 測(cè)試ansible任務(wù) hosts: "{{ hosts }}" remote_user: root gather_facts: False tasks: - name: hostname command: hostname register: hostname - name: echo debug: msg: "get hostname from {{ hostname }} by {{ powerby }} "
coding
Go執(zhí)行文件
package main?import ( "ansible/ansible" "fmt")?func main() { result, ok := ansible.RunPlayBook("/data/apps/python3/bin/ansible-playbook", "/etc/ansible/hosts", "/data/ansible/test.yaml", []string{ "hosts=127.0.0.1", "powerby=Golang", }) if ok { fmt.Println("執(zhí)行成功") } else { fmt.Println("執(zhí)行失敗") } fmt.Println(result)}?
執(zhí)行輸出
執(zhí)行成功/data/apps/python3/bin/ansible-playbook --ssh-common-args='-o StrictHostKeyChecking=no' -i /etc/ansible/hosts /data/ansible/test.yaml -e hosts=127.0.0.1 -e powerby=Golang [WARNING]: Found variable using reserved name: hosts?PLAY [測(cè)試ansible任務(wù)] *************************************************************?TASK [hostname] ****************************************************************changed: [127.0.0.1]?TASK [echo] ********************************************************************ok: [127.0.0.1] => { "msg": "get hostname from {'cmd': ['hostname'], 'stdout': 'tosomeone', 'stderr': '', 'rc': 0, 'start': '2020-08-20 22:23:29.851614', 'end': '2020-08-20 22:23:29.852629', 'delta': '0:00:00.001015', 'changed': True, 'stdout_lines': ['linuxopcai'], 'stderr_lines': [], 'failed': False} by Golang "}?PLAY RECAP *********************************************************************127.0.0.1 : ok=2 changed=1 unreachable=0 failed=0 ??=======================================開(kāi)始時(shí)間:2020-08-20 22:23:28結(jié)束時(shí)間:2020-08-20 22:23:29耗時(shí):1.067408099s
總結(jié)
golang
由于歷史原因,很多任務(wù)都還是ansible的任務(wù),只能采用這種方式進(jìn)行過(guò)度。
在Go的生態(tài)中,大家有什么推薦的批量自動(dòng)化任務(wù)的包或者工具呢?
版權(quán)聲明:本文內(nèi)容由互聯(lián)網(wǎng)用戶(hù)自發(fā)貢獻(xiàn),該文觀(guān)點(diǎn)僅代表作者本人。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如發(fā)現(xiàn)本站有涉嫌抄襲侵權(quán)/違法違規(guī)的內(nèi)容, 請(qǐng)發(fā)送郵件至 舉報(bào),一經(jīng)查實(shí),本站將立刻刪除。