【问题标题】:Ansible playbook not woking with multivalued listsAnsible playbook 不适用于多值列表
【发布时间】:2022-10-23 15:51:13
【问题描述】:

我正在尝试编写一个创建多个用户的剧本,但是当我将变量放在列表形式中时它不起作用。

不工作的剧本

[root@ansible-master playbooks]# cat users_list_new.yml
---
users:
  - username:
      - amba
      - ruchita

[root@ansible-master playbooks]# cat multi_users_new.yml
---
- name: Creating the multi users with a new approach
  hosts: california
  become: true
  vars_files:
    - users_list_new.yml
  tasks:
    - name: Create the user
      user:
        name: "{{ item.username[0] }}"
      loop: "{{ users }}"

它只创建第一个用户,因为我已将下标 0 放在用户模块中。我的问题是我们如何在 username 上创建一个循环。例如,我修改了我的剧本,但它没有用

---
- name: Creating the multi users with a new approach
  hosts: california
  become: true
  vars_files:
    - users_list_new.yml
  tasks:
    - name: Create the user
      user:
        name: "{{ item }}"
      loop: "{{ users.username }}"

当我运行剧本时它抛出了错误

PLAY [Creating the multi users with a new approach] ********************************************************************************************************************

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

TASK [Create the user] *************************************************************************************************************************************************
fatal: [10.128.0.5]: FAILED! => {"msg": "'list object' has no attribute 'username'"}

PLAY RECAP *************************************************************************************************************************************************************
10.128.0.5                 : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

[root@ansible-master playbooks]#

请帮我找出解决方案

【问题讨论】:

  • 思考(无法验证 atm,不在工作中)您只需要展平外部列表:去掉用户名前面的连字符。

标签: linux ansible


【解决方案1】:

你的问题是你的变量结构users 包含一个列表而不是一个字典,所以你不能访问username 作为它的键,因为它是一个列表而不是一个字典。您需要以users[0] 的身份访问才能访问用户名列表。

---

- name: Creating the multi users with a new approach
  hosts: localhost
  vars_files:
    - users_list_new.yml
  tasks:
    - name: Create the user
      user:
        name: "{{ item }}"
      loop: "{{ users[0].username }}"
TASK [Create the user] ********************************************************************************************************************************************************************
Sunday 23 October 2022  08:47:41 +0100 (0:00:07.567)       0:00:08.179 ******** 
changed: [localhost] => (item=amba)
changed: [localhost] => (item=ruchita)

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-04
    • 1970-01-01
    • 1970-01-01
    • 2021-10-20
    • 2018-08-31
    • 1970-01-01
    相关资源
    最近更新 更多