How to copy the template sites_enabled.j2 into a /etc/nginx/sites-enabled/default using adhoc commands. & How to start nginx using shell?

846 Views Asked by At

The template gets copied normally in /etc/nginx/sites-enabled

On running this command: ansible localhost -b -m copy -a "src=/abc/efg/ngs/templates/sites-enabled.j2 dest=/etc/nginx/sites-enabled"

The file gets copied.

:/etc/nginx/sites-enabled$ ls gives the output as default & sites-enabled.j2.

How to copy the template provided in /ngs to /etc/nginx/sites-enabled/default and how to start the nginx using adhoc the commands?

1

There are 1 best solutions below

0
Lovedeep Sharma On

What I understood from your question is that:

  • You want to copy multiple template files from src = "/abc/efg/ngs/" to dest = "/etc/nginx/sites-enabled/default".

  • You want to restart Nginx.

To achieve this using Adhoc command:

  • COPY FILES: ansible localhost -b -m copy -a "src=/abc/efg/ngs/templates dest=/etc/nginx/sites-enabled/default/

  • START NGINX USING COMMAND MODULE: ansible localhost -m command -a "systemctl start nginx"

  • START NGINX USING SHELL MODULE: ansible localhost -m shell -a "systemctl start nginx"

Ref to ad-hoc commands: https://docs.ansible.com/ansible/latest/user_guide/intro_adhoc.html

To achieve this using the playbook command:

- name: Copying files from source to destination
  copy:
    src: /abc/efg/ngs/templates
    dest: /etc/nginx/sites-enabled/default/
    owner: foo
    group: foo
    mode: 0644
- name: Starting nginx
  command: systemctl start nginx

Ref: https://docs.ansible.com/ansible/2.4/copy_module.html

Ref: https://docs.ansible.com/ansible/latest/collections/ansible/builtin/command_module.html

But rather I would suggest If you learn about handlers as they are very much helpful to do these kinds of tasks when you want to restart/reload any service only when a change happens.

Ref: https://docs.ansible.com/ansible/latest/user_guide/playbooks_handlers.html

If you asked something else, let me know.