1
votes

Below is my directory structure for ansibel role called webserver

localhost roles # tree

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

my tasks/main.yml looks like

- 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

in the task/main.yml im specifying absolute path to local scriptfile like

script: /etc/ansible/roles/webserver/files/nginxkeyadd.sh and script: /etc/ansible/roles/webserver/files/createswap.sh . The scripts don't have any ansible variables. is it good practice in ansible ?

1

1 Answers

1
votes

is it good practice in ansible ?

No. Excerpt from the 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

Also using shell-scripts instead of native Ansible modules is an antipattern.