如何在不同的主机上创建不同的用户

时间:2019-05-23 08:31:43

标签: ansible

主机列表: 192.168.110.1 ....... 192.198.110.10。

您需要在每个用户上创建一个用户: 鲍勃 ... 爱丽丝

我想在一个剧本中实现它,以免每个主机运行10次。

用户创建类似这样的内容:

vars_prompt:
 - name: "user_name"
   prompt: "User name"    
   private: no   
 - name: "user_password"    
   prompt: "Enter a password for the user"    
   private: yes    
   encrypt: "md5_crypt"    
   confirm: yes    
   salt_size: 7
tasks:
 - name: "add new user" 
   user: 
     name: "{{user_name}}" 
     password: "{{user_password}}" 
     shell: /bin/bash

1 个答案:

答案 0 :(得分:1)

您可以在inventory文件中添加不同的主机组:

[alice_hosts]
192.168.110.2

[bob_hosts]
192.168.110.1 

然后仅在以下特定组上的playbook.yml中运行一些命令:

- hosts: all

  tasks:

    # Do some common stuff

- hosts: alice_hosts

  tasks:

   - name: "add new user" 
     user: 
       name: alice
       password: alice_password
       shell: /bin/bash

- hosts: bob_hosts

  tasks:

   - name: "add new user" 
     user: 
       name: bob
       password: bob_password
       shell: /bin/bash