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.

 

image-20210729113427170

 

報錯2

 

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

image-20210729113638171

 

 

解決

 

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

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

image-20210729113859878

 

那么在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

image-20210729114003463

 

修改后

image-20210729140541021

 

 

ansible普通用戶執行命令

 

生產中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的密碼),然后執行成功。

 

image-20210810142045819

 

 

附上其他兩個配置文件參考:

ansible主機配置文件

image-20210810142634304

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文件

image-20210810142715952

 

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

 


免責聲明!

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



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