2014-10-06 29 views
2

我希望有一个不同的用户可以使用基于平台上运行的ansible playbook。基于平台的Ansible条件用户

比如我有一个这样的剧本:

--- 
- hosts: all 
    user: deploy 
    tasks: 
    - name: hello world 
    shell: echo "Hello World" 

我希望该用户在上一个RedHat跑是“部署”,和达尔文/ Mac系统环境变量$ USER。

我该如何设置?

回答

5

在serverfault上有一个很好的问题,它在几个方面回答了这个问题: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' 
+0

我发现'ansible_os_family'是更好地与'使用when'因为'ansible_os_family ==“Debian''涵盖了Ubuntu和Linux Mint的例如。 – darkwing 2018-02-23 18:39:05

0

如果你只需要知道,如果你正在处理的Linux或Mac,即可使用uname输出。在一个剧本这可能如下所示:

- name: check operating system 
    shell: uname 
    ignore_errors: yes 
    register: uname_result 
    - name: install tensorflow 
    sudo: yes 
    shell: pip3 install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.11.0rc0-cp34-cp34m-linux_x86_64.whl 
    when: uname_result.stdout == 'Linux' 
    - name: install tensorflow 
    sudo: yes 
    shell: pip3 install --upgrade https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.11.0rc0-py3-none-any.whl 
    when: uname_result.stdout == 'Darwin'