0

I'm trying to create some nginx files but only after having deleted all the files of the /etc/nginx/sites-enabled before.

I have this states:

install_nginx:
  pkg.installed:
    - name: nginx

restart_nginx:
  service:
    - name: nginx
    - running
    - watch:
      - file: /etc/nginx/sites-enabled/*.conf

clear_default_config:
  file.directory:
    - name: /etc/nginx/sites-enabled/
    - clean: True
    - require:
      - pkg: install_nginx

{% for config in salt['pillar.get']('nginx:conf') %}
set_{{ config }}_conf_uncm:
  file.uncomment:
    - name: /etc/nginx/nginx.conf
    - regex: "{{ config }} (.*);$"
    - char: '#'
    - require:
      - file: clear_default_config

set_{{ config }}_conf_repl:
  file.replace:
    - name: /etc/nginx/nginx.conf
    - pattern: "{{ config }} (.*);$"
    - repl: "{{ config }} {{ salt['pillar.get']('nginx:conf:'+config) }};"
    - require:
      - file: clear_default_config
{% endfor %}

The problem is that if creates the files and then removes them, exactly the opposit of the desired way.

----------
          ID: crear_vhost_testsalt
    Function: file.managed
        Name: /etc/nginx/sites-enabled/testsalt.conf
      Result: True
     Comment: File /etc/nginx/sites-enabled/testsalt.conf updated
     Started: 15:23:26.209494
    Duration: 5.923 ms
     Changes:
              ----------
              diff:
                  New file
              mode:
                  0644
----------
          ID: crear_vhost_testsalt2
    Function: file.managed
        Name: /etc/nginx/sites-enabled/testsalt2.conf
      Result: True
     Comment: File /etc/nginx/sites-enabled/testsalt2.conf updated
     Started: 15:23:26.215843
    Duration: 4.738 ms
     Changes:
              ----------
              diff:
                  New file
              mode:
                  0644
----------
          ID: restart_nginx
    Function: service.running
        Name: nginx
      Result: True
     Comment: Service restarted
     Started: 15:23:26.235859
    Duration: 1033.838 ms
     Changes:
              ----------
              nginx:
                  True
----------
          ID: clear_default_config
    Function: file.directory
        Name: /etc/nginx/sites-enabled/
      Result: True
     Comment: Files cleaned from directory /etc/nginx/sites-enabled
     Started: 15:23:27.270678
    Duration: 1.096 ms
     Changes:
              ----------
              removed:
                  - /etc/nginx/sites-enabled/testsalt2.conf
                  - /etc/nginx/sites-enabled/testsalt.conf

Any help?

Thanks!

Julio
  • 1
  • What I found (and I think it is not the best solution), is to add an order option: clear_default_config: file.directory: - name: /etc/nginx/sites-enabled/ - clean: True - require: - pkg: install_nginx - order: 10 – Julio May 04 '15 at 09:11

1 Answers1

0

I'd add an onlyif to the clear_default_config like:

...
- onlyif:
  - "test -f /etc/nginx/sites-enabled/default.conf"
Roald Nefs
  • 426
  • 5
  • 13
c4urself
  • 5,270
  • 3
  • 25
  • 39
  • Thanks for the answer! I'm doing that to prevent if I delete one virtualhost that I do not want to use anymore. – Julio May 04 '15 at 09:09