ansible - 由于权限问题,用户创建的剧本执行失败

Vas*_*K V 1 ansible

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

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

须藤 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
}
Run Code Online (Sandbox Code Playgroud)

但是当我像下面这样使用 --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"
}
Run Code Online (Sandbox Code Playgroud)

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

现在下一步是执行剧本我创建了一个剧本如下

 hosts: all
  tasks:
  - name: Ansible create user example.
    user:
      name: vasanth
      password: vasanth
Run Code Online (Sandbox Code Playgroud)

当我执行我得到以下结果

    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  
Run Code Online (Sandbox Code Playgroud)

这里有什么问题?

lar*_*sks 5

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

hosts: all
become: true
tasks:
  - name: Ansible create user example.
    user:
      name: vasanth
      password: vasanth
Run Code Online (Sandbox Code Playgroud)

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

hosts: all
tasks:
  - name: Ansible create user example.
    become: true
    user:
      name: vasanth
      password: vasanth
Run Code Online (Sandbox Code Playgroud)

become键不是特权升级专用; 它可用于要求 Ansible 以任何用户身份与become_user密钥组合运行 。您可以在文档中阅读更多内容。