Ansible 如何在特定主机上创建特定用户

Ansible 如何在特定主机上创建特定用户,ansible,Ansible,网络上有多台计算机,在每台计算机上,您需要创建一个具有特定登录名和密码的用户 我创建的用户如下所示: 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"

网络上有多台计算机,在每台计算机上,您需要创建一个具有特定登录名和密码的用户

我创建的用户如下所示:

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
因为有很多电脑,我不想让一个剧本运行很多次。理想情况下,我希望实现主机(计算机)列表和用户列表的输入。密码,原则上,您可以在任何地方执行相同的操作。

循环任务

tasks:
  - name: "add new user" 
    user: 
      name: "{{ item.user_name }}" 
      password: "{{ item.user_password }}" 
      shell: /bin/bash
    loop: "{{ my_users }}"
并将my_用户的变量

将普通用户置于

用于加密密码。

循环任务

tasks:
  - name: "add new user" 
    user: 
      name: "{{ item.user_name }}" 
      password: "{{ item.user_password }}" 
      shell: /bin/bash
    loop: "{{ my_users }}"
并将my_用户的变量

将普通用户置于


用于加密密码。

以下是您可以尝试的示例。适应你的需要

注意:如果每个主机的用户列表不同,只需执行playbook几次。在ansible中将此作为一个可接受的游戏来实现将是一个彻底的痛苦,并且仅仅是无法使用的

在下面的示例中,
test1
test2
指向我在
demo\u inventory.yml
中添加的2个docker容器

all:
  hosts:
    test1:
      ansible_connection: docker
    test2:
      ansible_connection: docker
ansible需要正确知道您输入的主机,才能使其正常工作

这是演示手册
test.yml

---
- name: Gather needed information
  hosts: localhost

  vars_prompt:

    - name: hosts_entry
      prompt: Enter comma separated list of hosts to target
      private: false

    - name: users_entry
      prompt: Enter comma separated list of users to create
      private: false

    - name: user_password
      prompt: Enter initial password applied to all users
      encrypt: md5_crypt
      confirm: true
      salt_size: 7


  tasks:
    - name: Create a dynamic whatever_group with entered hosts
      add_host:
        name: "{{ item | trim }}"
        groups:
          - whatever_group
      loop: "{{ hosts_entry.split(',') }}"

    - name: Create a list of host for later reuse. Will be scoped to localhost
      set_fact:
        users_list: "{{ users_entry.split(',') }}"

    - name: Store password for later reuse as vars_prompt are limited to play
      set_fact:
        user_password: "{{ user_password }}"

 - name: Do the actual work
   hosts: whatever_group

   tasks:
     - name: Make sure users are present
       user:
         name: "{{ item | trim }}"
         password: "{{ hostvars['localhost'].user_password }}"
         shell: /bin/bash
       loop: "{{ hostvars['localhost'].users_list }}"
我在localhost上创建了一个play,从
vars\u提示符
收集信息。在这个剧本中,我使用
add_host
动态创建一个
which_组
。注意使用
split
从输入中带有逗号分隔元素的字符串创建列表,使用
trim
删除前导/尾随空格(如果用户输入)。由于
vars\u prompt
仅限于当前播放,因此我还使用
set\u fact
获取用户列表和密码,以备将来使用

在下一个重头戏中,我以
任何组为目标,运行
用户
任务。请注意,由于先前使用的
set\u fact
将变量的作用域限定为
localhost
,因此我们必须使用
hostvars
magic变量来获取用户
循环
和密码的相关信息

下面是运行的示例

$ ansible-playbook -i demo_inventory.yml test.yml 
Enter comma separated list of hosts to target: test1, test2
Enter comma separated list of users to create: user1, user2, user3
Enter initial password applied to all users: 
confirm Enter initial password applied to all users: 

PLAY [Gather needed information] ***************************************************************

TASK [Gathering Facts] *************************************************************************
ok: [localhost]

TASK [Create a dynamic whatever_group with entered hosts] **************************************
changed: [localhost] => (item=test1)
changed: [localhost] => (item= test2)

TASK [Create a list of host for later reuse. Will be scoped to localhost] **********************
ok: [localhost]

TASK [Store password for later reuse as vars_prompt are limited to play] ***********************
ok: [localhost]

PLAY [Do the actual work] **********************************************************************

TASK [Gathering Facts] *************************************************************************
ok: [test1]
ok: [test2]

TASK [Make sure users are present] *************************************************************
changed: [test2] => (item=user1)
changed: [test1] => (item=user1)
changed: [test2] => (item= user2)
changed: [test1] => (item= user2)
changed: [test2] => (item= user3)
changed: [test1] => (item= user3)

PLAY RECAP *************************************************************************************
localhost                  : ok=4    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test1                      : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test2                      : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

下面是一个你可以尝试的例子。适应你的需要

注意:如果每个主机的用户列表不同,只需执行playbook几次。在ansible中将此作为一个可接受的游戏来实现将是一个彻底的痛苦,并且仅仅是无法使用的

在下面的示例中,
test1
test2
指向我在
demo\u inventory.yml
中添加的2个docker容器

all:
  hosts:
    test1:
      ansible_connection: docker
    test2:
      ansible_connection: docker
ansible需要正确知道您输入的主机,才能使其正常工作

这是演示手册
test.yml

---
- name: Gather needed information
  hosts: localhost

  vars_prompt:

    - name: hosts_entry
      prompt: Enter comma separated list of hosts to target
      private: false

    - name: users_entry
      prompt: Enter comma separated list of users to create
      private: false

    - name: user_password
      prompt: Enter initial password applied to all users
      encrypt: md5_crypt
      confirm: true
      salt_size: 7


  tasks:
    - name: Create a dynamic whatever_group with entered hosts
      add_host:
        name: "{{ item | trim }}"
        groups:
          - whatever_group
      loop: "{{ hosts_entry.split(',') }}"

    - name: Create a list of host for later reuse. Will be scoped to localhost
      set_fact:
        users_list: "{{ users_entry.split(',') }}"

    - name: Store password for later reuse as vars_prompt are limited to play
      set_fact:
        user_password: "{{ user_password }}"

 - name: Do the actual work
   hosts: whatever_group

   tasks:
     - name: Make sure users are present
       user:
         name: "{{ item | trim }}"
         password: "{{ hostvars['localhost'].user_password }}"
         shell: /bin/bash
       loop: "{{ hostvars['localhost'].users_list }}"
我在localhost上创建了一个play,从
vars\u提示符
收集信息。在这个剧本中,我使用
add_host
动态创建一个
which_组
。注意使用
split
从输入中带有逗号分隔元素的字符串创建列表,使用
trim
删除前导/尾随空格(如果用户输入)。由于
vars\u prompt
仅限于当前播放,因此我还使用
set\u fact
获取用户列表和密码,以备将来使用

在下一个重头戏中,我以
任何组为目标,运行
用户
任务。请注意,由于先前使用的
set\u fact
将变量的作用域限定为
localhost
,因此我们必须使用
hostvars
magic变量来获取用户
循环
和密码的相关信息

下面是运行的示例

$ ansible-playbook -i demo_inventory.yml test.yml 
Enter comma separated list of hosts to target: test1, test2
Enter comma separated list of users to create: user1, user2, user3
Enter initial password applied to all users: 
confirm Enter initial password applied to all users: 

PLAY [Gather needed information] ***************************************************************

TASK [Gathering Facts] *************************************************************************
ok: [localhost]

TASK [Create a dynamic whatever_group with entered hosts] **************************************
changed: [localhost] => (item=test1)
changed: [localhost] => (item= test2)

TASK [Create a list of host for later reuse. Will be scoped to localhost] **********************
ok: [localhost]

TASK [Store password for later reuse as vars_prompt are limited to play] ***********************
ok: [localhost]

PLAY [Do the actual work] **********************************************************************

TASK [Gathering Facts] *************************************************************************
ok: [test1]
ok: [test2]

TASK [Make sure users are present] *************************************************************
changed: [test2] => (item=user1)
changed: [test1] => (item=user1)
changed: [test2] => (item= user2)
changed: [test1] => (item= user2)
changed: [test2] => (item= user3)
changed: [test1] => (item= user3)

PLAY RECAP *************************************************************************************
localhost                  : ok=4    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test1                      : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test2                      : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0