1. 程式人生 > 其它 >ansible報錯解決python依賴,Ansible普通使用者執行命令

ansible報錯解決python依賴,Ansible普通使用者執行命令

問題

報錯1

執行ansible報錯python這一個

[DEPRECATION WARNING]: Distribution Ubuntu 16.04 on host k8s-node01 should use /usr/bin/python3, but is using /usr/bin/python for backward 
compatibility with prior Ansible releases. A future Ansible release will default to using the discovered platform python for this host. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. This feature will be removed in
version 2.12. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.

報錯2

"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},

解決

從上面可知基本都是意思遠端機器呼叫的python2,建議python3,看機器也是有的。

root@k8s-node01:/home/appdeploy# ls /usr/bin/python
python python2 python2.7 python3 python3.5 python3.5m python3m

那麼在ansible的host內呼叫即可

k8s-node01 ansible_ssh_user=appdeploy ansible_ssh_port=51000 ansible_python_interpreter=/usr/bin/python3.5
k8s-node02 ansible_ssh_user=appdeploy ansible_ssh_port=51000 ansible_python_interpreter=/usr/bin/python3.5

修改後

ansible普通使用者執行命令

[參考連結] https://blog.csdn.net/qq_34939308/article/details/110454215

生產中root都是禁止直接遠端登入得。很多命令root許可權才能執行,指令碼使用expect提權又麻煩,還好ansible自帶有這功能。

修改下列配置檔案

root@k8s-master01:~# cat /etc/ansible/ansible.cfg 
[defaults]
timeout = 30
remote_user = root
[inventory]
[privilege_escalation]
become=True ###表示開啟become開關,也就是輸入密碼那一欄
become_method=su ###表示用什麼方式將普通賬戶切換到root或所需的其他賬戶,這裡可以用su或sudo
become_user=root ##***設定為root賬戶,相當於我們以普通賬戶登入到遠端主機時,再使用su - root切換為root賬戶。
become_ask_pass=True ###表示詢問密碼
[paramiko_connection]
[ssh_connection]
[persistent_connection]
[accelerate]
[selinux]
[colors]
[diff]

修改後執行root許可權得命令會彈出讓你輸入BECOME密碼(也就是你遠端主機root的密碼),然後執行成功。

附上其他兩個配置檔案參考:

ansible主機配置檔案

root@k8s-master01:~# cat /etc/ansible/hosts  |grep -Ev '^$|#'
[k8s]
k8s-node01 ansible_ssh_user=appdeploy ansible_ssh_port=51000 ansible_python_interpreter=/usr/bin/python3.5
k8s-node02 ansible_ssh_user=appdeploy ansible_ssh_port=51000 ansible_python_interpreter=/usr/bin/python3.5
root@k8s-master01:~# fengliuxiaosanyuanchuang

系統主機hosts檔案

root@k8s-master01:~# cat /etc/hosts  |grep -Ev '^$|#'
127.0.0.1 localhost
127.0.1.1 ubuntu
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
192.168.202.128 k8s-master01
192.168.202.132 k8s-node01
192.168.202.130 k8s-node02

專業從事搬磚多年,還是在繼續搬磚中,厚積薄發~