【问题标题】:How to consolidate roles and templates to the same playbook如何将角色和模板整合到同一个剧本中
【发布时间】:2019-08-14 21:14:42
【问题描述】:

我需要使用一个名为 vcommander 的配置工具 看起来这个工具支持在配置的主机上本地运行 ansible 只有,你只需要给他提供运行的剧本。

我已经有工作的 ansible 角色,其中包括任务和模板,例如,ntp 角色看起来像下面这样:

ls -1 roles/ntp/

defaults
LICENSE
meta
README.md
tasks
templates

主要任务如下所示:

cat roles/ntp/tasks/main.yml

---

- name: "Configure ntp.conf"
  template:
    src: "ntp.conf.j2"
    dest: "/etc/ntp.conf"
    mode: "0644"
    become: True

- name: Updating time zone
  shell: tzdata-update

- name: Ensure NTP-related packages are installed.
  package:
  name: ntp
 state: present

- name: Ensure NTP is running and enabled as configured.
  service:
  name:  ntpd
 state: started
enabled: yes

模板看起来像这样:

cat roles/ntp/templates/ntp.conf.j2

{{ ansible_managed }}

restrict 127.0.0.1
restrict -6 ::1

server 127.127.1.0
fudge   127.127.1.0 stratum 10

driftfile /var/lib/ntp/drift
keys /etc/ntp/keys

有没有办法包含 main.yml 和 ntp.conf.j2(模板) 和角色中包含的其他文件合并到一个 yml 文件中? 请举个例子。

【问题讨论】:

    标签: ansible


    【解决方案1】:

    您是否要求将您的 ntp 角色应用于localhost 的剧本?

    看起来像这样:

    cat playbook.yml

    - hosts: localhost
      roles:
        - ntp
    
    

    请参阅文档:https://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse_roles.html#using-roles

    【讨论】:

    • 问题是如何将任务和模板包含到同一个 yml 文件中?
    • 所以你想处理这个角色,实际上只有一个单个文件
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-06
    • 1970-01-01
    • 2015-11-13
    • 2020-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多