使用Ansible将用户添加到组

时间:2017-07-13 11:46:58

标签: ansible

我已在服务器上安装并配置Ansible以进行配置管理。

/ etc / ansible / hosts 文件下,我添加了[servers]部分,并提到了我需要维护的所有服务器

我有一个 Ubuntu 服务器

在我的 test.yml 文件中,我编写了这段代码,

---
- hosts: servers
  become: yes
  tasks:
  - name: Add user
    user:
      name: james
      shell: /bin/bash
     groups: wheel
     state: present
    remove: yes

问题是在Ubuntu中没有 wheel 组,但 sudo 是。

所以我希望该服务器在sudo组中添加用户,并让其他人添加到wheel组

我如何在该代码的ansible中做条件

1 个答案:

答案 0 :(得分:3)

您可以根据ref的分布情况包含变量。

Ansible在description = et.fromstring("<volltext>%s</volltext>" % cls.escape(job.description)) 事实中保存目标系统分布的值。

在您的任务中使用变量(ansible_distribution)而不是字符串:

group_for_james

在游戏中为用户定义默认值:

- name: Add user
  user:
    name: james
    shell: /bin/bash
    groups: "{{ group_for_james }}"
    state: present
    remove: yes

然后为Ubuntu创建一个vars文件(例如将其保存为vars: group_for_james: wheel ):

vars/ubuntu.yml

添加一个任务(在group_for_james: sudo 任务之前),如果目标正在运行Ubuntu,它将覆盖该值:

Add user

其他包含模式也是可能的,请参阅链接文档页面上的第三个示例。

相关问题