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



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

本地主机角色 # 树

.
├── 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

我的任务/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

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

script: /etc/ansible/roles/webserver/files/nginxkeyadd.shscript: /etc/ansible/roles/webserver/files/createswap.sh.脚本没有任何 ansible 变量。这是安斯布尔的好做法吗?

这是安斯布尔的好做法吗?

不。文档摘录:

任何副本、脚本、模板或包含任务(在角色中(都可以引用角色/x/{文件,模板,任务}/(目录取决于任务(中的文件,而不必相对或绝对地路径

同样使用 shell 脚本而不是本机 Ansible 模块是一种反模式。

最新更新