基于平台的 Ansible 条件用户

标签 ansible

我希望根据运行 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/

相关文章:

linux - Ansible 剧本 : check linux process count

linux - nohup 和 & 在运行 bash 命令时有什么区别?

linux - Ansible服务器无法连接aws云主机

ansible - 正则表达式匹配并转换为 Ansible 事实

ansible - 将变量从就地渲染的 jinja2 模板包含到剧本中,无需中间文件

ansible - 如何按标签顺序运行 Ansible playbook 任务

ansible - AWX - 使用 Ansible 集合

ansible - 在 Ansible 中使用模板模块附加文件

安塞 bool : evaluate variable inside a variable with register

ansible - 如何使用ansible生成单个可重复使用的随 secret 码