zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    信息产品是信息化理念的凝缩的精华
    自然科学技术表面上是反应人与自然的关系,更深层还是人与人之间的关系
    思与在,为何没有行
    haproxysocket 参数记录
    zabbix 监控 haproxy 记录
    Centos6.5安装OpenLDAP
    ansible mysql模块的使用今年
    haproxy 官方文档查看
    centos 7 部署 mysql 报错记录
    ansible playbook学习
  • 原文地址:https://www.cnblogs.com/yum777/p/6491254.html
Copyright © 2011-2022 走看看