Ansible 基于平台的可变条件用户
我想在ansible playbook运行的平台上使用不同的用户 例如,我有一本这样的剧本:Ansible 基于平台的可变条件用户,ansible,ansible-playbook,Ansible,Ansible Playbook,我想在ansible playbook运行的平台上使用不同的用户 例如,我有一本这样的剧本: --- - hosts: all user: deploy tasks: - name: hello world shell: echo "Hello World" 我希望在RedHat上运行时,该用户是“deploy”,在Darwin/Mac系统上运行环境变量$user 我该如何设置它?关于serverfault有一个很好的问题,可以用几种方式回答这个问题:,但它不是重复的,因为
---
- hosts: all
user: deploy
tasks:
- name: hello world
shell: echo "Hello World"
我希望在RedHat上运行时,该用户是“deploy”,在Darwin/Mac系统上运行环境变量$user
我该如何设置它?关于serverfault有一个很好的问题,可以用几种方式回答这个问题:,但它不是重复的,因为它是一个不同的“板” 这也是Ansible常见问题解答中的首要问题,建议将其与
Ansible\u ssh\u user
一起放在主机文件中
是最干净的选项
对于一般情况,您还可以有选择地运行任务:
- 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\u os\u家族
在时最好与一起使用,因为ansible\u os\u家族=='Debian'
包括Ubuntu和Linux Mint。