首页 文章

在ansible中运行playbook时出现sudo错误

提问于
浏览
1

我试图在没有密码的情况下使用ansible . 这不是我第一次使用ansible,但这是我第一次遇到--become和--become_method的问题 . 我的问题非常类似于另一个堆栈溢出问题,但有一些区别:Ansible playbook: Requires sudo password

我运行的系统是一个Ubuntu 16.04衍 生产环境 品,我试图用ansible配置的服务器运行Centos 7 .

我对ansible.cfg文件的唯一修改是启用了“nocows = 1” . 在该设置之外,其他一切都是默认的 .

我能够在不需要密码的情况下ssh到服务器并使用sudo切换到root用户:

admin@linuxdesktop ~/Documents/ansible/test1 $ ansible --version
ansible 2.1.1.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
admin@linuxdesktop ~/Documents/ansible/test1 $ ssh ansible@server
Last login: Wed Oct 18 17:51:21 2017 from 10.4.1.28
[ansible@server ~]$ sudo su -
Last login: Wed Oct 18 17:53:41 CDT 2017 on pts/1
[root@server ~]# cat /etc/sudoers.d/ansible 
ansible ALL=(ALL) NOPASSWD:ALL
[root@server ~]#

这是我的test.yml文件的内容:

---
- hosts: server
  become: yes
  become_method: sudo
  tasks:
    - shell: echo "hello world!"

这是我运行test.yml文件时的结果:

admin@linuxdesktop ~/Documents/ansible/test1 $ ansible-playbook -i dev test.yml 

PLAY [server] ******************************************************************

TASK [setup] *******************************************************************
fatal: [server]: FAILED! => {"changed": false, "failed": true, "module_stderr": "", "module_stdout": "sudo: a password is required\r\n", "msg": "MODULE FAILURE", "parsed": false}

NO MORE HOSTS LEFT *************************************************************
 [WARNING]: Could not create retry file 'test.retry'.         [Errno 2] No such file or directory: ''


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

这就是我为了让事情适合我而做的事情:

我能够将我的test.yml文件修改为以下内容以使其工作:

---
- hosts: server
  remote_user: ansible
  become: yes
  become_method: sudo
  tasks:
    - shell: echo "hello world!"

我还发现,如果您在所有服务器上使用相同的用户,您也可以在/etc/ansible/ansible.cfg文件中设置以下选项:

remote_user = ansible

2 回答

  • 0

    这是我的输出 test.yml ansible-playbook test.yml

    我认为你在库存(dev)文件中存在问题..我的是

    [server]
    server ansible_host=ip_address ansible_user=ubuntu ansible_private_key_file=/path_to_keyfile/docker-key.pem
    
  • 1

    上次我遇到这个问题是因为提供用户组sudo权限的行是在包含sudoers.d之后 . 修复是切换线:

    deployer@linoidbc:/etc$ sudo cat sudoers
    ...
    
    %deployers ALL=(ALL) ALL
    #includedir /etc/sudoers.d
    
    deployer@linoidbc:/etc$ sudo cat sudoers.d/deployer
    deployer ALL=NOPASSWD: ALL
    

相关问题