Ansible:有 sudo 但没有 root

Ansible: have sudo but no root

我想使用 Ansible 来管理我们的 Hadoop 集群的配置 (运行ning Red Hat)。

我有 sudo 访问权限,可以手动 ssh 进入节点执行命令。但是,当我尝试 运行 Ansible 模块执行相同任务时遇到问题。 尽管我有 sudo 访问权限,但我无法成为 root。当我尝试执行需要提升权限的 Ansible 脚本时,出现如下错误:

Sorry, user awoolford is not allowed to execute '/bin/bash -c echo BECOME-SUCCESS- […] /usr/bin/python /tmp/ansible-tmp-1446662360.01-231435525506280/copy' as awoolford on [some_hadoop_node].

查看 documentation,我认为 become_allow_same_user 属性 可能会解决这个问题,因此我将以下内容添加到 ansible.cfg

[privilege_escalation]
become_allow_same_user=yes

不幸的是,它没有用。

这个 post 表明我需要 sudo /bin/sh(或其他一些 shell)的权限。不幸的是,出于安全原因,这是不可能的。这是来自 /etc/sudoers 的片段:

root            ALL=(ALL)   ALL
awoolford       ALL=(ALL)   ALL, !SU, !SHELLS, !RESTRICT

Ansible 能在这样的环境下工作吗?如果是这样,我做错了什么?

好吧,您根本无法像 /etc/sudoers 显示的那样执行 /bin/sh/bin/bash。您可以做的是将 ansible 的默认 shell 更改为其他内容(ansible.conf 中的变量 executable)。

因为你的 sudo 策略默认允许一切(对我来说似乎并不安全),我想 ansible 期望一个 sh 兼容 shell,作为一个非常肮脏的黑客你可以复制 /bin/bash 到其他 path/name 并相应地设置 executable 变量(未测试)。

在剧本 (some.yml) 文件中,设置

runthisplaybook.yml

---
- hosts: label_which_will_work_on_some_servers
  sudo: yes

  roles: 
    - some_role_i_want_to_run

接下来,在 role//tasks/main.yml 中你必须 运行 作为 sudo 的动作.. 使用类似 become_user(其中 common_user 是在某个角色的 defaults\main.yml 文件中定义为 common_user 的变量:"this_user_can_sudo":

- name: Run chkconfig on init script
  command: "sudo -u root /sbin/chkconfig --add tomcat" 

# Set execute permission on run_jmeter_test.sh
- name: Set execute permission on run_jmeter_test.sh
  command: "chmod -R 755 {{ jmeter_perf_tests_results }}"
  become_user: "{{ common_user }}"

# OR Set execute permission on run_jmeter_test.sh
- name: Set execute permission on run_jmeter_test.sh
  command: "sudo -u firstuser sudo -u seconduser chmod -R 755 {{ jmeter_perf_tests_results }}"
  become_user: "{{ common_user }}"

# OR Set execute permission on run_jmeter_test.sh
- name: Set execute permission on run_jmeter_test.sh
  command: "chmod -R 755 {{ jmeter_perf_tests_results }}"
  become_user: "{{ common_user }}"

PS:虽然 运行ning ansible-playbook,

ansible-playbook runthisplaybook.yml --sudo-user=this_user_can_sudo -i hosts.yml -u user_which_will_connect_from_source_machine --private-key ${ DEPLOYER_KEY_FILE} --extra-vars "target_svr_type=${server_type} deploy_environment=${DEPLOY_ENVIRONMENT} ansible_user=${ANSIBLE_USER}"

我认为现在 sudo: yes 已被删除并替换为 become: yes

---
- hosts: servers_on_which_you_want_to_run
  become: yes

  roles: 
    - some_role

smiplist 解决方案只是在你的 playbook 目录中创建一个 ansible.cfg 并包含以下内容,如果它不接受 root 用户:

[defaults]
sudo_user      = UsernameToWhichYouWantToUse

希望这能解决您的问题。

在对该主题进行研究之后,从 Ansible 2.8 开始,您似乎无法使用 [=11] 作为其他用户 运行 命令=]没有root权限。

还有另一种方法可以实现你所要求的而不是这样,怎么说呢,'hacky'。

您可以将 shell 模块与 sudo su - <user> -c "COMMAND" 一起使用,以不同的用户身份执行命令,而不需要对原始用户进行 root 访问 .

例如,

  1 --- 
  2 - hosts: target_host
  3
  4   tasks:
  5   - shell: 'sudo su EXEC_USER -c "whoami"'
  6     register: x
  7
  8   - debug:
  9       msg: "{{ x.stdout_lines }}"   # This returns EXEC_USER

但是,如果您的游戏很复杂,则需要将其分解并仅包装需要以不同用户身份执行的命令。

这不是最佳实践(使用 sudo + shell 而不是 become),但这是一个解决方案,在我看来,这比在您管理的每个节点上创建虚拟 shell 更好.