ansible - 在角色中包含静态脚本文件

ansible - include the static script file in roles

下面是我的名为 webserver 的 ansibel 角色的目录结构

本地主机角色#树

.
├── readme.md
├── site.yml
└── webserver
    ├── files
    │   ├── createswap.sh
    │   └── nginxkeyadd.sh
    ├── handlers
    │   └── main.yml
    ├── tasks
    │   └── main.yml
    ├── templates
    │   ├── helloworld.conf.j2
    │   └── index.html.j2
    └── vars
        └── main.yml

我的tasks/main.yml 看起来像

- name: Create swap file 50MB
  script: /etc/ansible/roles/webserver/files/createswap.sh
- name: add GPG key for nginx
  script: /etc/ansible/roles/webserver/files/nginxkeyadd.sh 
- name: Install nginx on target
  apt: name={{ item }} state=latest
  with_items:
   - rsync
   - git
   - nginx

在 task/main.yml im 中指定本地脚本文件的绝对路径,如

script: /etc/ansible/roles/webserver/files/nginxkeyadd.shscript: /etc/ansible/roles/webserver/files/createswap.sh 。这些脚本没有任何可靠的变量。 这是 ansible 的好习惯吗?

is it good practice in ansible ?

没有。摘自docs

Any copy, script, template or include tasks (in the role) can reference files in roles/x/{files,templates,tasks}/ (dir depends on task) without having to path them relatively or absolutely

同样使用 shell-scripts 而不是原生 Ansible 模块也是一种反模式。