无法将 item_var 与 ansible_hostname 进行比较

无法将 item_var 与 ansible_hostname 进行比较

我正在使用 ansible 版本2.9.13,并试图修改以下任务 - 顺便说一下,它运行良好 - 所以我可以将 yml 文件中存储的给定主机名列表与事实进行比较ansible_hostname在执行创建学生操作之前/期间。

以下是该create_students任务的完整运行情况:

create_a27_students.yml

---
- hosts: a27-alumnes
  become: yes
  vars_files:
    - users/students.yml

  tasks:
  - import_tasks: tasks/create_students.yml
  - import_tasks: tasks/restart_lightdm.yml

tasks/create_students.yml

  - name: Create student's regular users
    user:
      name: "{{ item.name }}"
      # mkpasswd --method=sha-512
      password: "{{ item.pass | password_hash('sha512') }}"
      state: present
      shell: /bin/bash       # Defaults to /bin/bash
      system: no             # Defaults to no
      createhome: yes        # Defaults to yes
      home: /home/{{ item.name }}       # Defaults to /home/<username>
    with_items: "{{  students  }}"

  - import_tasks: restart_lightdm.yml

tasks/restart_lightdm.yml

  - name: Check if lightdm service exists (stat module)
    stat: path=/etc/init.d/lightdm
    register: service_stat

  - name: Check if lightdm service exists (service module)
    service: name=lightdm
    register: service

  - name: Restart lightdm service if exists and is running
    service: name=lightdm state=restarted
    when: service_stat.stat.exists and service.status.SubState == 'running'
    register: service_started

以下是 var 文件 yml users/students.yml

---
    students:
        - name: smx
          pass: smx
          state: present

        - name: smx2
          pass: smx2
          state: present

        - name: arduino
          pass: arduino
          state: present

以下是主机名列表data/hostnames.yml

---
    hostnames:
        - name: PC-a27-01
          hostname: PC-a27-01

        - name: PC-a27-01
          hostname: PC-a27-01

        - name: arduino
          hostname: arduino

以下是create_students修改后用于运行比较的任务,但是它似乎不起作用:

create_a27_students.yml

---
- hosts: a27-alumnes
  become: yes
  vars_files:
    - users/students.yml
    - data/hostnames.yml

  tasks:
  - import_tasks: tasks/create_students.yml
  - import_tasks: tasks/restart_lightdm.yml

tasks/create_students.yml

  - name: Create student's regular users
    user:
      name: "{{ item.name }}"
      # mkpasswd --method=sha-512
      password: "{{ item.pass | password_hash('sha512') }}"
      state: present
      shell: /bin/bash       # Defaults to /bin/bash
      system: no             # Defaults to no
      createhome: yes        # Defaults to yes
      home: /home/{{ item.name }}       # Defaults to /home/<username>
    ## I would also be interested on using the "in" condition as long as the "=="
    when: item.hostname == ansible_hostname
    with_items: "{{  students  }}", "{{ hostnames }}"

  - import_tasks: restart_lightdm.yml

希望有人能帮助我解决这个问题。

谢谢,

PD: 这是我的 github repo 的副本[1],希望对您有帮助

[1]https://github.com/pauperis/ansible/tree/master/playbooks

答案1

希望这会有所帮助。

首先要说的是,从 Ansible 2.5 开始,循环是迭代的首选语法。
Ansible Doc - 从 with_X 迁移到 loop

所以据我理解,你喜欢迭代嵌套列表。
Ansible 文档 - 迭代嵌套列表

请注意,列表项将是item.0item.1
您还需要将项目索引添加到代码中item.0.name

按照上面的例子,我认为接下来的内容需要您详细说明。
我只是注释掉上一行并在下面添加新行。

  - name: Create student's regular users
    user:
      #name: "{{ item.name }}"
      name: "{{ item.0.name }}"
      # mkpasswd --method=sha-512
      #password: "{{ item.pass | password_hash('sha512') }}"
      password: "{{ item.0.pass | password_hash('sha512') }}"
      state: present
      shell: /bin/bash       # Defaults to /bin/bash
      system: no             # Defaults to no
      createhome: yes        # Defaults to yes
      #home: /home/{{ item.name }}       # Defaults to /home/<username>
      home: /home/{{ item.0.name }}       # Defaults to /home/<username>
    ## I would also be interested on using the "in" condition as long as the "=="
    #when: item.hostname == ansible_hostname
    when: item.1.hostname == ansible_hostname
    #with_items: "{{  students  }}", "{{ hostnames }}"
    loop: "{{ students|product(hostnames)|list }}"

  - import_tasks: restart_lightdm.yml

相关内容