ansible普通用戶su切換問題 普通用戶免密登陸 sudo提權


在現網應用中,安全加固后的主機是不允許直接以root用戶登陸的,而很多命令又需要root用戶來執行,在不改造現網的情況下。希望通過一個普通用戶先登陸,再su切到root執行。而且每台主機的普通用戶和root用戶的密碼又不同。希望在通過ansible執行的時候不需要交互輸入密碼,而是直接執行后輸出結果。

一、ansible hosts配置文件

在之前的系列文章中我們提到,可以把密碼寫到hosts配置文件,通過查詢官網的相關信息了解了,其除了ansible_ssh_user、ansible_ssh_pass變量外,還為su切換提供了ansible_su_pass變量,通過該變量我們可以把root密碼直接寫到配置文件中。具體如下:

 

  1. [root@361way.com ~]# cat /etc/ansible/hosts
  2. [test01]
  3. 10.212.52.14 ansible_ssh_user=test ansible_ssh_pass=111111 ansible_su_pass=*I2145
  4. 10.212.52.16 ansible_ssh_user=test ansible_ssh_pass=xyz123 ansible_su_pass=mn1Pokm
  5. 10.212.52.252 ansible_ssh_user=amos ansible_ssh_pass=asdf ansible_su_pass=xyzp)okm

注:我測試使用的ansible版本是1.9版的,在新的2.0版本中,變量也做了變化ansible_become_pass替換了之前的ansible_sudo_pass or ansible_su_pass ,具體可以參看官方文檔

二、ansible命令參數

在執行ansible -h查看時,會看到如下條目:

 

  1. -S, --su run operations with su (deprecated, use become)
  2. -R SU_USER, --su-user=SU_USER
  3. run operations with su as this user (default=root)
  4. (deprecated, use become)

 

三、su切換執行

所以結合上面兩塊,我們做下簡單的測試:

 

  1. [root@361way.com ~]# ansible all -S -R root -m shell -a "uptime"
  2. 10.212.52.252 | success | rc=0 >>
  3. 16:13pm up 34 days 5:40, 2 users, load average: 0.08, 0.21, 0.30
  4. 10.212.52.16 | success | rc=0 >>
  5. 16:26pm up 538 days 23:17, 2 users, load average: 0.00, 0.01, 0.05
  6. 10.212.52.14 | success | rc=0 >>
  7. 16:24pm up 538 days 22:39, 2 users, load average: 0.00, 0.01, 0.05

這里需要注意的是,普通用戶的家目錄是要存在,並切該普通用戶要有寫的權限的,不然會出現類似如下的報錯:

 

  1. 10.212.52.252 | FAILED => Authentication or permission failure.
  2. In some cases, you may have been able to authenticate and did not have permissions on the remote directory.
  3. Consider changing the remote temp path in ansible.cfg to a path rooted in "/tmp".
  4. Failed command was: mkdir -p $HOME/.ansible/tmp/ansible-tmp-1449456070.96-212322517029279 && echo $HOME/.ansible/tmp/ansible-tmp-1449456070.96-212322517029279,
  5. exited with result 1: mkdir: cannot create directory `/home/amos/.ansible': Permission denied

當然,如果這個普通用戶沒有家目錄或者家目錄沒有寫權限在不修改遠端主機也有辦法可以搞定,修改ansible主機的ansible.cfg配置文件,如下:

 

  1. [root@361way.com ~]# vim /etc/ansible/ansible.cfg
  2. 找到如下行:
  3. remote_tmp = $HOME/.ansible/tmp
  4. 修改為
  5. remote_tmp = /tmp/.ansible/tmp

tmp目錄一般都有寫的權限吧,改成臨時目錄為/tmp下即可。

再下為我們再看看遠程主機的message日志文件確認下是否真的是通過普通用戶切換的:

 

  1. Dec 3 11:36:20 linux su: (to root) test on /dev/pts/1 //由普通用戶test切換為su切換為root的日志
  2. Dec 3 11:36:20 linux ansible-command: Invoked with creates=None executable=None chdir=None args=uptime removes=None NO_LOG=None shell=True warn=True //ansible執行的內容

功能實現了,最后要說的是,由於該配置文件中涉及到多台主機的用戶名密碼,所以該文件的安全工作一定要做好。

 

 

ansible使用普通用戶免密登陸+sudo提權:

前提:從ansible控制端使用test用戶可以免密登陸所有被控制端,並且被控端test用戶支持sudo提權

# ansible主機清單
cat /etc/ansible/hosts

[online-a1]
172.17.149.162 ansible_ssh_port=2222 ansible_ssh_user=test

[online-a2]
172.17.149.140 ansible_ssh_user=test

[online-a3]
172.17.149.120 ansible_ssh_user=test

[online-a4]
172.17.149.145 ansible_ssh_user=test

[online-as]
172.17.149.162 ansible_ssh_user=test
172.17.149.140 ansible_ssh_user=test
172.17.149.120 ansible_ssh_user=test
172.17.149.145 ansible_ssh_user=test

# ansible配置文件
/etc/ansible/ansible.cfg

取消注釋的行:
remote_tmp     = ~/.ansible/tmp
inventory      = /etc/ansible/hosts
host_key_checking = False

# yaml文件內容
cat test.yaml
- hosts: online-as
  become: true
  tasks:
    - name: just test
      shell: mkdir -p /root/test/test2

# 命令行ansible-doc測試
ansible online-as -m shell -a 'ls -lh /root/' -become=true

 


免責聲明!

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



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