首页 > 解决方案 > ansible:循环目录并在另一个剧本中引用该文件

问题描述

我正在尝试制作一个循环遍历目录中文件数量的剧本,然后在另一个剧本中使用这些文件。我现在的剧本:

---

- name: Run playbooks for Raji's testing
  register: scripts
  roles:
    - prepare_edge.yml
    - prepare_iq.yml
    - scriptor.yml
  with_fileglob: ~/ansible/test_scripts/*
~

当我运行它时它不起作用,我尝试“注册:脚本”来创建一个变量以在 scriptor.yml 中引用,但剧本再次失败。您可以提供的任何建议或帮助将不胜感激。谢谢!

PS我对ansible超级陌生

这是scriptor.yml

---

- hosts: all
  tasks:

  - name: Create directory
    command: mkdir /some/path/

  - name: If file is a playbook
    copy:
      src: "{{ scripts }}"
      dest: /some/path/
    when: "{{ scripts }}" == "*.yml"

  - name: if file is a script
    shell: . ${{ scripts }}
    when: "{{ scripts }}" == "*.sh"

PSS prepare_edge.yml 和 prepare_iq.yml 不引用任何内容,只需要在 scriptor.yml 之前的循环中调用

这是错误:

ERROR! 'register' is not a valid attribute for a Play

The error appears to have been in '/Users/JGrow33/ansible/raji_magic_playbook.yml': line 3, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


- name: Run playbooks for Raji's testing
  ^ here

标签: ansible

解决方案


您收到的错误消息是告诉您无法register在 Playbook 中运行。

您可以通过在 scriptor.yml 文件中执行以下操作来完成您正在寻找的内容:

- hosts: all
  tasks:

  - name: Create directory
    command: mkdir /some/path/

  - name: If file is a playbook
    copy:
      src: "{{ item }}"
      dest: /some/path/
    with_fileglob: ~/ansible/test_scripts/*.yml

  - name: if file is a script
    shell: . ${{ item }}
    with_fileglob:         copy:
      src: "{{ item }}"
      dest: /some/path/
    with_fileglobe: ~/ansible/test_scripts/*.sh

参考

Ansible 如何在变量中“注册”包含剧本的结果?


推荐阅读