我希望有一個不同的用戶可以使用基於平臺上運行的ansible playbook。基於平臺的Ansible條件用戶
比如我有一個這樣的劇本:
---
- hosts: all
user: deploy
tasks:
- name: hello world
shell: echo "Hello World"
我希望該用戶在上一個RedHat跑是「部署」,和達爾文/ Mac系統環境變量$ USER。
我該如何設置?
我希望有一個不同的用戶可以使用基於平臺上運行的ansible playbook。基於平臺的Ansible條件用戶
比如我有一個這樣的劇本:
---
- hosts: all
user: deploy
tasks:
- name: hello world
shell: echo "Hello World"
我希望該用戶在上一個RedHat跑是「部署」,和達爾文/ Mac系統環境變量$ USER。
我該如何設置?
在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'
如果你只需要知道,如果你正在處理的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'
我發現'ansible_os_family'是更好地與'使用when'因爲'ansible_os_family ==「Debian''涵蓋了Ubuntu和Linux Mint的例如。 – darkwing 2018-02-23 18:39:05