【问题标题】:ansible - playbook execution fails for user creation with permission issueansible - 用户创建的剧本执行失败,出现权限问题
【发布时间】:2018-11-13 11:25:33
【问题描述】:

我是 devops 的新手,曾经尝试过 puppet,现在正在检查 ansible。 我已经按照大多数教程中描述的常规方式设置了 ansible 1>下载的EPL 2>安装ansible 3> 在控制和目标机器之间交换 ssh 密钥。 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


    【解决方案1】:

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

    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 键。你可以阅读更多in the docs。

    【讨论】:

    • 我已添加成为:true,然后创建成功,但我无法在目标计算机上使用该用户登录。它说密码不正确。我在网上阅读了很多地方,他们说我必须将散列密钥作为剧本中的密码 - 是真的吗?如果是,有没有办法我可以从剧本(对于给定的纯文本密码)以编程方式创建哈希,然后创建用户?
    • @VasanthNagKV 密码需要加密,参考。 docs.ansible.com/ansible/… 这在模块密码参数的描述中提到,参考。 docs.ansible.com/ansible/2.5/modules/user_module.html
    【解决方案2】:

    这里有两个不同的问题:

    1。整理你的 SSH 密钥

    Ansible 正在尝试使用 SSH 密钥进行连接,但失败了:

    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).\r\n"
    

    我建议你先在你的服务器上尝试 ssh:ssh admin@10.0.0.47

    这可能会失败,因为您的公钥不在服务器的authorized_keys 中,或者因为您的~/.ssh/config 中的键映射。

    一旦您能够在服务器中ssh,Ansible 应该也可以。

    2。创建用户

    查看 ansible 输出:

    ...
    "msg": "[Errno 13] Permission denied"
    

    您的用户没有足够的权限来创建用户。如果您的admin 用户属于wheel 组,则可以使用become 以root 身份运行任务:

    hosts: all
    tasks:
      - name: Ansible create user example.
        become: yes
        user:
          name: vasanth
          # ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-17
      • 2023-04-04
      • 2018-11-23
      • 2016-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多