Saltstack [SOLVED]: What is the expected behavior of using grain.present?

Saltstack [SOLVED]: What is the expected behavior of using grain.present?

Home Forums Automation Tools Saltstack Tutorials Saltstack [SOLVED]: What is the expected behavior of using grain.present?

Tagged: ,

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #166114

    Cloudy Point
    Keymaster

    QuestionQuestion

    I am trying to use salt to move over my php.ini file, but I only want it to run once on the first run. I am trying to use salt grains to do this, but this is working inconsistently. Any ideas?

    #phpinifile.sls
    {%- set php_ini_initialized = salt['grains.get']('PHP_INI_INITIALIZED') | default( False, true) %}
    
    {% if php_ini_initialized == False %}
    etc-php-70-cli-phpini:
    file.managed:
        - name: /etc/php/7.0/cli/php.ini
        - source: salt://billing/sources/etc/php/7.0/cli/php.ini
        - user: root
        - group: root
        - mode: 644
        - context:
          set_pear_path: True
        - template: jinja
    
    
    PHP_INI_INITIALIZED:
      grains.present:
        - value: True
    {% endif %}
    

    #166115

    Cloudy Point
    Keymaster

    Accepted AnswerAnswer

    What is the expected behavior of using grain.present?

    It will make sure that the grain will be present. In other words. Ik will make it when it’s not there.

    Any ideas?

    You want to make sure that the php.ini file is only placed on your minion once. During the first ‘state.apply’

    file.managed will make sure that your file is present, with the right permissions and the right content.
    When you do another state.apply, Salt will see that the file is already available with the correct content and moves on. In this case you could run this as many times as you want without a problem.

    If your php.ini file is changed manually after it is placed by Salt I understand that you want to make sure to only run it once. In this case you could use the unless requisite like this:

    phpini:
    file.managed:
        - name: /etc/php/7.0/cli/php.ini
        - source: salt://billing/sources/etc/php/7.0/cli/php.ini
        - unless: ls /etc/php/7.0/cli/php.ini
    

    The unless requisite specifies that a state should only run when any of the specified commands return False. In this case the ls returns false if the file does not exist.

    I hope this helps.

    Source: https://stackoverflow.com/questions/45468128/what-is-the-expected-behavior-of-using-grain-present
    Author: Gijs Brandsma
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.