我希望根据运行 ansible playbook 的平台使用不同的用户。
例如,我有一个这样的剧本:
---
- hosts: all
user: deploy
tasks:
- name: hello world
shell: echo "Hello World"
我希望该用户在 RedHat 上运行时“部署”,并在 Darwin/Mac 系统上运行环境变量 $USER。
我该如何设置?
最佳答案
关于服务器故障有一个很好的问题,可以通过几种方式回答这个问题:ansible playbook, different user by operating system ,但它不是重复的,因为它是不同的“板”。
这也是 Ansible 常见问题解答中的首要问题,How do I handle different machines needing different user accounts or ports to log in with? ,建议使用 ansible_ssh_user
将其放入主机文件中。
"Ansible best practices" file suggests using group_vars
,这是最干净的选项。
对于简单的情况,您还可以有选择地运行任务:
- name: run this on debian type systems
debug msg="hello from debian"
sudo: yes
sudo_user: ubuntu
when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu'
- name: run this on OSX type systems
sudo: yes
sudo_user: "{{ lookup('env','USER') }}"
debug: msg="hello from osx"
when: ansible_distribution == 'MacOSX'
关于基于平台的 Ansible 条件用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26226609/