首页 > 解决方案 > ansible - 用户创建的剧本执行失败,出现权限问题

问题描述

我是 devops 的新手,曾经尝试过 puppet,现在正在检查 ansible。我已经按照大多数教程 1> 下载的 EPL 2> 安装的 ansible 3> 在控制和目标机器之间交换 ssh 密钥的常规方式设置了 ansible。4> 正确配置 sshd_conf 文件

现在是我测试下面的 ping 的时候了

sudo ansible testservers -u admin -m ping

但是当我这样做时,我得到如下输出

ansible 2.6.2
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.6.6 (r266:84292, Aug  9 2016, 06:11:56) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)]
Using /etc/ansible/ansible.cfg as config file
Parsed /etc/ansible/hosts inventory source with ini plugin
META: ran handlers
<admin@10.0.0.47> ESTABLISH SSH CONNECTION FOR USER: admin
<admin@10.0.0.47> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=admin -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/bbbace40d6 admin@10.0.0.47 '/bin/sh -c '"'"'echo ~admin && sleep 0'"'"''
<admin@10.0.0.47> (255, '', 'Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).\r\n')
admin@10.0.0.47 | UNREACHABLE! => {
    "changed": false, 
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).\r\n", 
    "unreachable": true
}

但是当我像下面那样使用 --ask-pas 并提供密码时,它可以正常工作(这在自动化时没有用)

sudo ansible testservers -m user -a'name=vasanth state=absent' --become --ask-pas
SSH password: 
admin@10.0.0.47 | SUCCESS => {
    "changed": false, 
    "name": "vasanth", 
    "state": "absent"
}

为了解决这个问题,我在 /etc/ansible/hosts 文件中添加了“ansible_ssh_pass”并解决了。没有--ask-pas ping 是成功的

现在下一步是执行我创建的剧本,如下所示

 hosts: all
  tasks:
  - name: Ansible create user example.
    user:
      name: vasanth
      password: vasanth

当我执行时,我得到以下结果

    sudo ansible-playbook userCreate.yml -v
Using /etc/ansible/ansible.cfg as config file

PLAY [all] ************************************************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************************************
ok: [10.0.0.47]

TASK [Ansible create user example.] ***********************************************************************************************************************************************************
fatal: [10.0.0.47]: FAILED! => {"changed": false, "cmd": "/usr/sbin/useradd -p VALUE_SPECIFIED_IN_NO_LOG_PARAMETER -m VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "msg": "[Errno 13] Permission denied", "rc": 13}
    to retry, use: --limit @/home/admin/userCreate.retry

PLAY RECAP ************************************************************************************************************************************************************************************
10.0.0.47                  : ok=1    changed=0    unreachable=0    failed=1  

这里有什么问题?

标签: ansible

解决方案


如果您没有以 身份连接到远程主机root,那么您需要告诉 Ansible 在root使用该become:密钥运行任务时成为成为,该密钥可以放置在一个游戏中以使用提升的权限运行该游戏中的所有任务:

hosts: all
become: true
tasks:
  - name: Ansible create user example.
    user:
      name: vasanth
      password: vasanth

或者可以将其放置在单个任务上以仅运行具有提升权限的那些任务:

hosts: all
tasks:
  - name: Ansible create user example.
    become: true
    user:
      name: vasanth
      password: vasanth

become密钥并非专门用于提权;它可用于要求 Ansible 结合 become_user密钥以任何用户身份运行。您可以在文档中阅读更多内容。


推荐阅读