ansible 執行sudo切換


 

應用場景:當被控端的root被禁止遠程登錄,需要通過普通用戶先連接上,然后進行提前

 

一、基於命令做操作

###############
ansible all -u wang -b -m script -a '/root/script/touch.sh' --ask-become-pass

 ansible tomcat_first -m script -a 'tomcat_reinforce.sh' -u sysadmin -b -K

-u  指定普通用戶
-b  指定提權的用戶,-b 代表root
-m  指定要使用哪個模塊
-a  指定要執行的命令參數
-K 指定提權是使用的口令
--ask-become-pass: 輸入密碼進行提權

 

二、ploybook實現sudo提前並執行腳本

##############

---
- hosts: all
remote_user: wang
gather_facts: No
become: yes
become_method: sudo
become_user: root
tasks:
- name: "create a new file"
script: '/root/script/touch.sh'

 

執行時加-K

ansible-playbook touch.yml -K

 

轉載需注明出處,否則追究法律責任


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM