Ansible SSH作为一个用户,而Sudo作为另一个用户

我在configurationAnsible和sudo时遇到问题,无法让SSH进入服务器并以另一个用户身份运行命令。

我已经通过下面的问题/答案和Ansible文档在这里: http ://docs.ansible.com/intro_inventory.html

但我仍然无法工作。

请有人把我放在正确的轨道上。

参考文献:

https://stackoverflow.com/questions/24743085/ansible-ssh-as-one-user-and-sudo-as-another Ansible:使用不同的sudo用户为不同的主机

我正在尝试这样做:

server-01 client-01 --------- ----------- foo ----> ssh ---> foo bar - sudo as root user 

使用Ansible,从:server-01连接到:client-01

使用用户foo,然后使用sudo运行命令作为用户栏用户栏设置为可以运行任何命令。

但是,我不确定问题在哪里,用sudo或Ansible。 我认为问题是与sudo,但我不知道在哪里。

这个可靠的命令起作用:

 [foo@server-01 ~]$ **ansible client-01 -m raw -a "touch /var/tmp/testfile" --ask-sudo-pass** sudo password: ********* ( password for foo user ) client-01 | success | rc=0 >> 

这一个不起作用:

 [foo@server-01 ~]$ ansible client-01 -m raw -a "touch /etc/testfile" --ask-sudo-pass sudo password: ********* ( password for foo user ) client-01 | FAILED | rc=1 >> touch: cannot touch `/etc/testfile': Permission denied 

我有SSHvalidation,没有密码设置之间的服务器01和客户端01为用户富,它的工作正常。

 [foo@server-01 ~]$ id uid=96(foo) gid=96(foo) groups=96(foo) [foo@server-01 ~]$ su - foo Password: [foo@server-01 ~]$ [foo@server-01 ~]$ [foo@server-01 ~]$ ssh client-01 Last login: Thu Jan 15 16:32:05 2015 from server-01 [foo@client-01 ~]$ 

这是我的设置:

 server-01: /etc/ansible/hosts ------------------------------------- [clients] client-01 ansible_sudo=true ansible_ssh_user=foo ansible_sudo_user=bar client-01: /etc/sudoers ------------------------------------- [root@client-01 ~]# cat /etc/sudoers # root ALL=(ALL) ALL bar ALL=(ALL) ALL foo ALL=(bar) NOPASSWD: ALL 

在目标服务器上(client-01),我可以testingsudo。 我认为sudo是不工作的部分,但是由于这个原因,我无法安装任何Ansible Playbooks。

 [root@client-01 ~]# id uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel) [root@client-01 ~]# su - bar [bar@client-01 ~]$ sudo -l [sudo] password for bar: User bar may run the following commands on this host: (ALL) ALL [bar@client-01 ~]$ sudo touch /etc/tempfile [bar@client-01 ~]$ ls -alp /etc/tempfile -rw-r--r-- 1 root root 0 Jan 15 17:08 /etc/tempfile [bar@client-01 ~]$ sudo rm /etc/tempfile [bar@client-01 ~]$ exit logout [root@client-01 ~]# su - foo [foo@client-01 ~]$ sudo -l User foo may run the following commands on this host: (bar) NOPASSWD: ALL [foo@client-01 ~]$ sudo touch /etc/tempfile [sudo] password for foo: Sorry, user foo is not allowed to execute '/bin/touch /etc/tempfile' as root on client-01. [foo@client-01 ~]$ exit logout 

也许这是失败的路线:

  foo ALL=(bar) NOPASSWD: ALL 

我感谢你的帮助。

  • 你能正常SSH进入框并运行这两个命令? 鉴于唯一不同的是你试图touchpath,我猜这是一个文件系统问题,而不是一个可靠的问题。

更新:

现在尝试用ALLreplaceSudoers文件中的(bar) ,看看是否有效?