我正在尝试通过 Ansible 将用户添加到 Windows AD,为此我需要提示用户输入新用户的信息(用户名、密码和 AD 组)。
这是我当前的剧本,但这提示错误,因为显然我不能在任务中使用 vars_prompt。所以我想知道的是:我怎样才能在用户每次迭代输入新信息的地方循环。
谢谢
vars_prompt:
- name: num_loops
prompt: Enter number of users you want to add
private: no
tasks:
- name: create local Users
loop: "{{ range(1, num_loops|int + 1) | list }}"
vars_prompt:
- name: wu_group
prompt: "Enter the user's group"
private: no
- name: wu_username
prompt: "Enter the username for user {{ item }}"
private: no
- name: wu_password
prompt: "Enter the password for user {{ item }}"
private: yes
ansible.windows.win_user:
name: "{{ wu_username }}"
password: "{{ wu_password }}"
groups: "{{ wu_group }}"
update_password: on_create
password_expired: true
在循环中获取用户名和密码
- pause:
prompt: "Enter group"
register: out_group
- pause:
prompt: "{{ item }}. Enter user,password"
register: out_users
loop: "{{ range(1, num_loops|int + 1) | list }}"
声明变量
wu_group: "{{ out_group.user_input }}"
wu_users: "{{ out_users.results|map(attribute='user_input')|
map('split', ',') }}
给出,例如
wu_group: my_group
wu_users:
- [alice, '123']
- [bob, '456']
- [carol, '789']
任务示例(未测试)
- ansible.windows.win_user:
name: "{{ item.0 }}"
password: "{{ item.1 }}"
groups: "{{ wu_group }}"
update_password: on_create
password_expired: true
loop: "{{ wu_users }}"
完整的测试剧本示例
shell> cat pb.yml
- hosts: localhost
vars_prompt:
- name: num_loops
prompt: Enter number of users you want to add
private: no
vars:
wu_group: "{{ out_group.user_input }}"
wu_users: "{{ out_users.results|map(attribute='user_input')|
map('split', ',') }}"
tasks:
- pause:
prompt: "Enter group"
register: out_group
- pause:
prompt: "{{ item }}. Enter user,password"
register: out_users
loop: "{{ range(1, num_loops|int + 1) | list }}"
- debug:
var: wu_group
- debug:
var: wu_users|to_yaml
- debug:
msg: |
{% filter from_yaml|to_yaml %}
group: {{ wu_group }}
users:
{% for i in wu_users %}
- {name: {{ i.0 }}, pswd: {{ i.1 }}}
{% endfor %}
{% endfilter %}
给予
shell> ansible-playbook pb.yml
Enter number of users you want to add: 3
PLAY [localhost] ******************************************************************************
TASK [pause] **********************************************************************************
[pause]
Enter group:
my_group^Mok: [localhost]
TASK [pause] **********************************************************************************
[pause]
1. Enter user,password:
alice,123^Mok: [localhost] => (item=1)
[pause]
2. Enter user,password:
bob,456^Mok: [localhost] => (item=2)
[pause]
3. Enter user,password:
carol,789^Mok: [localhost] => (item=3)
TASK [debug] **********************************************************************************
ok: [localhost] =>
wu_group: my_group
TASK [debug] **********************************************************************************
ok: [localhost] =>
wu_users|to_yaml: |-
- [alice, '123']
- [bob, '456']
- [carol, '789']
TASK [debug] **********************************************************************************
ok: [localhost] =>
msg: |-
group: my_group
users:
- {name: alice, pswd: 123}
- {name: bob, pswd: 456}
- {name: carol, pswd: 789}
PLAY RECAP ************************************************************************************
localhost: ok=5 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0