saltstack / salt

Software to automate the management and configuration of any infrastructure or application at scale. Install Salt from the Salt package repositories here:
https://docs.saltproject.io/salt/install-guide/en/latest/
Apache License 2.0
14.19k stars 5.48k forks source link

Unable to manage file: Jinja variable 'dict object' has no attribute #43308

Closed OPSTime closed 5 years ago

OPSTime commented 7 years ago

Env:

# salt --version
salt 2015.5.10 (Lithium)

# cat /etc/redhat-release 
CentOS Linux release 7.0.1406 (Core) 

Pillar:

zookeeper_cluster2:
  node_list:
      192.168.0.19:
        - 0
        - 2888:3888
        - node1
      192.168.0.20:
        - 1
        - 2888:3888
        - node2
      192.168.0.21:
        - 2
        - 2888:3888
        - node3
  tickTime: 2000
  dataDir: /data/zookeeper/data
  dataLogDir: /data/zookeeper/tlog
  zookeeper.log.dir: '/data/logs/zookeeper'
  zookeeper.tracelog.dir: '/data/logs/zookeeper'

Salt: zookeeper_configure.sls

{% set zkcluster = 'zookeeper_cluster2' %}

zookeeper_pkg_install:
  archive.extracted:
    - name: /usr/local
    - source: salt://zookeeper/files/zookeeper-3.4.1.tar.gz
    - archive_format: tar
    - tar_options: xz
    #- user: root
    #- group: root
    - if_missing: /usr/local/zookeeper-3.4.1

  file.symlink:
    - name: /usr/local/zookeeper
    - target: /usr/local/zookeeper-3.4.1
    - force: True
    - onchanges:
      - archive: zookeeper_pkg_install

/usr/local/zookeeper/conf/log4j.properties:
  file.managed:
    - source: salt://zookeeper/files/log4j.properties
    - user: root
    - group: root
    - template: jinja
    - defaults:
      zkcluster: {{ zkcluster }}

/usr/local/zookeeper/conf/zoo.cfg:
  file.managed:
    - source: salt://zookeeper/files/zoo.cfg
    - user: root
    - group: root
    - template: jinja
    - defaults:
      zkcluster: {{ zkcluster }}

log4j.properties

# Define some default values that can be overridden by system properties
zookeeper.root.logger=WARN, CONSOLE
zookeeper.console.threshold=INFO
zookeeper.log.dir={{ pillar[zkcluster]['zookeeper.log.dir'] }}
zookeeper.log.file=zookeeper.log
zookeeper.log.threshold=DEBUG
zookeeper.tracelog.dir={{ pillar[zkcluster]['zookeeper.tracelog.dir'] }}
zookeeper.tracelog.file=zookeeper_trace.log

#
# ZooKeeper Logging Configuration
#

# Format is "<default threshold> (, <appender>)+

# DEFAULT: console appender only
log4j.rootLogger=${zookeeper.root.logger}

# Example with rolling log file
#log4j.rootLogger=DEBUG, CONSOLE, ROLLINGFILE

# Example with rolling log file and tracing
#log4j.rootLogger=TRACE, CONSOLE, ROLLINGFILE, TRACEFILE

#
# Log INFO level and above messages to the console
#
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=${zookeeper.console.threshold}
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}@%L] - %m%n

#
# Add ROLLINGFILE to rootLogger to get log file output
#    Log DEBUG level and above messages to a log file
log4j.appender.ROLLINGFILE=org.apache.log4j.RollingFileAppender
log4j.appender.ROLLINGFILE.Threshold=${zookeeper.log.threshold}
log4j.appender.ROLLINGFILE.File=${zookeeper.log.dir}/${zookeeper.log.file}

# Max log file size of 10MB
log4j.appender.ROLLINGFILE.MaxFileSize=10MB
# uncomment the next line to limit number of backup files
#log4j.appender.ROLLINGFILE.MaxBackupIndex=10

log4j.appender.ROLLINGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.ROLLINGFILE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}@%L] - %m%n

#
# Add TRACEFILE to rootLogger to get log file output
#    Log DEBUG level and above messages to a log file
log4j.appender.TRACEFILE=org.apache.log4j.FileAppender
log4j.appender.TRACEFILE.Threshold=TRACE
log4j.appender.TRACEFILE.File=${zookeeper.tracelog.dir}/${zookeeper.tracelog.file}

log4j.appender.TRACEFILE.layout=org.apache.log4j.PatternLayout
### Notice we are including log4j's NDC here (%x)
log4j.appender.TRACEFILE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}@%L][%x] - %m%n

run

# salt-ssh -L '192.168.0.19' state.sls zookeeper.zookeeper_configure

Result:

192.168.0.19:
----------
          ID: zookeeper_pkg_install
    Function: archive.extracted
        Name: /usr/local
      Result: True
     Comment: salt://zookeeper/files/zookeeper-3.4.9.tar.gz extracted in /usr/local/
     Started: 17:31:13.410766
    Duration: 2621.445 ms
     Changes:   
              ----------
              directories_created:
                  - /usr/local/
                  - /usr/local/zookeeper-3.4.9
              extracted_files:
                  no tar output so far
----------
          ID: zookeeper_pkg_install
    Function: file.symlink
        Name: /usr/local/zookeeper
      Result: True
     Comment: Created new symlink /usr/local/zookeeper -> /usr/local/zookeeper-3.4.9
     Started: 17:31:16.034408
    Duration: 1.284 ms
     Changes:   
              ----------
              new:
                  /usr/local/zookeeper
----------
          ID: /usr/local/zookeeper/conf/log4j.properties
    Function: file.managed
      Result: False
     Comment: Unable to manage file: Jinja variable 'dict object' has no attribute u'zookeeper_cluster2'
     Started: 17:31:16.035773
    Duration: 13.339 ms
     Changes:   
----------
          ID: /usr/local/zookeeper/conf/zoo.cfg
    Function: file.managed
      Result: False
     Comment: Unable to manage file: Jinja variable 'dict object' has no attribute u'zookeeper_cluster2'
     Started: 17:31:16.049237
    Duration: 18.581 ms
     Changes:   

Summary
------------
Succeeded: 2 (changed=2)
Failed:    2

Retry Result:

10.111.0.19:
----------
          ID: zookeeper_pkg_install
    Function: archive.extracted
        Name: /usr/local
      Result: True
     Comment: /usr/local/zookeeper-3.4.9 already exists
     Started: 17:41:19.573347
    Duration: 0.356 ms
     Changes:   
----------
          ID: zookeeper_pkg_install
    Function: file.symlink
        Name: /usr/local/zookeeper
      Result: True
     Comment: State was not run because onchanges req did not change
     Started: 
    Duration: 
     Changes:   
----------
          ID: /usr/local/zookeeper/conf/log4j.properties
    Function: file.managed
      Result: True
     Comment: File /usr/local/zookeeper/conf/log4j.properties updated
     Started: 17:41:19.575469
    Duration: 11.925 ms
     Changes:   
              ----------
              diff:
                  --- 
                  +++ 
                  @@ -1,10 +1,10 @@
                   # Define some default values that can be overridden by system properties
                  -zookeeper.root.logger=INFO, CONSOLE
                  +zookeeper.root.logger=WARN, CONSOLE
                   zookeeper.console.threshold=INFO
                  -zookeeper.log.dir=.
                  +zookeeper.log.dir=/data/logs/zookeeper
                   zookeeper.log.file=zookeeper.log
                   zookeeper.log.threshold=DEBUG
                  -zookeeper.tracelog.dir=.
                  +zookeeper.tracelog.dir=/data/logs/zookeeper
                   zookeeper.tracelog.file=zookeeper_trace.log

                   #
              group:
                  root
              user:
                  root
----------
          ID: /usr/local/zookeeper/conf/zoo.cfg
    Function: file.managed
      Result: True
     Comment: File /usr/local/zookeeper/conf/zoo.cfg updated
     Started: 17:41:19.587476
    Duration: 12.28 ms
     Changes:   
              ----------
              diff:
                  New file
              mode:
                  0644

Summary
------------
Succeeded: 4 (changed=2)
Failed:    0
------------
Total states run:     4
Ch3LL commented 7 years ago

@lyddragon can you share a sanitized version of either salt://zookeeper/files/log4j.properties or salt://zookeeper/files/zoo.cfg so we can see how your making the jinja call?

OPSTime commented 7 years ago

@Ch3LL Has been added

OPSTime commented 7 years ago

Another problem:

zookeeper_cluster2_deploy.sls

{% set zkcluster = 'zookeeper_cluster2' %}

include:
  #- sysconf.disable-transparent-hugepage
  - zookeeper.jdk-8u121-linux-x64_install
  - zookeeper.zookeeper_pkg-3-4-1_install
  - zookeeper.zookeeper_configure

run

[root@iZ251zn2kpwZ zookeeper]# salt-ssh -L '192.168.0.19' state.sls zookeeper.zookeeper_cluster2_deploy
[CRITICAL] Rendering SLS 'base:zookeeper.zookeeper_configure' failed: Jinja variable 'zkcluster' is undefined
192.168.0.19:
    - Rendering SLS 'base:zookeeper.zookeeper_configure' failed: Jinja variable 'zkcluster' is undefined
stale[bot] commented 5 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

If this issue is closed prematurely, please leave a comment and we will gladly reopen the issue.