應用場景:當被控端的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
轉載需注明出處,否則追究法律責任