操作系统不同的用户

我正开始玩耍。 我有一堆服务器(Ubuntu,Centos,红帽子…)。

服务器有不同的用户。 对于使用sudo用户的ubuntu服务器,其余的我使用root用户。

我怎么能通过操作系统指定用户?

文档中提到了仅针对特定操作系统执行的任务。 你可以调整这个设置每个操作系统的用户。 例如

 - hosts: CentOS user: centos tasks: - # some tasks - hosts: Debian user: default - # some tasks - hosts: Ubuntu user: ubuntu - # some tasks 

在资料来源中,似乎这些操作系统是受支持的,尽pipe在撰写本文时我无法在官方文档中证实这一点。

  • 红帽
  • Fedora的
  • CentOS的
  • 科学
  • SLC
  • Ascendos
  • CloudLinux
  • PSBM
  • OracleLinux
  • OVS
  • OEL
  • 亚马逊
  • XenServer的
  • Ubuntu的
  • Debian的
  • SLES
  • 雪橇
  • OpenSuSE的
  • SuSE的
  • Gentoo的
  • ArchLinux的
  • 的Mandriva
  • Mandrake的
  • 的Solaris
  • Nexenta的
  • OmniOS
  • 的OpenIndiana
  • SmartOS

您也可以在vars/<os-name>.yaml文件中定义per-OSvariables,并使用ansible_os_family模板variables有条件地使用它们,如下所示。

CentOS等不是自动的。 这是一个超过所提供的variables的组。 这里是一个完整的例子,处理CentOS加虚拟机或不虚拟机。

 --- - name: RG Ansible for ALL hosts: all tasks: - name: group by OS versions group_by: key="{{ ansible_distribution }}_{{ ansible_distribution_version.split('.')[0] }}" - name: group by physical/virtual machine group_by: key="{{ ansible_virtualization_role }}" ... - name: RG Ansible for CentOS 6 hosts: CentOS_6 gather_facts: false tasks: ... - name: RG Ansible for CentOS 5 hosts: CentOS_5 gather_facts: false tasks: - name: RG Ansible for VM hosts: guest gather_facts: false tasks: - service: name=acpid state=stopped enabled=no - service: name=cpuspeed state=stopped enabled=no 

在命令行中指定用户:

 ansible-playbook foo.yml --extra-vars "user=bar" ----- - user: '{{ user }}' 

/etc/ansible/hosts 每个主机组指定一个不同的user

 [targets] localhost ansible_connection=local other1.example.com ansible_connection=ssh ansible_ssh_user=mpdehaan other2.example.com ansible_connection=ssh ansible_ssh_user=mdehaan