Chef ruby, работа с json

Для того чтобы читать json нужно использовать цикл и работать как с коллекцией, пример:

a = {
    "deploy": {
        "magento": {
            "application": "magento",
            "application_type": "php",
            "scm": {
                "revision": "master"
            },
            "group": "www-data",
            "user": "www-data",
            "keep_releases": 3,
            "robots": {
                "server_func": "admin",
                "sitemap": "http://www.luluandgeorgia.com/sitemap/sitemap.xml"
            },
            "var_path": "/srv/var",
            "amfpclocal": {
                "host": "lulu-prod.7ddf4w.ng.0001.usw2.cache.amazonaws.com"
            },
            "magento_config": {
                "just_disable_logs": false,
                "install_date": "Tue, 18 Feb 2014 20:50:42 +0000",
                "crypt_key": "f554e225fe79cdd102d671b4e045bc93",
                "disable_local_modules": false,
                "table_prefix": "",
                "session_save": "db",
                "front_name": "backoffice",
                "connections": [
                    {
                        "name": "default_setup",
                        "host": "",
                        "username": "",
                        "password": "",
                        "dbname": "",
                        "initStatements": "SET NAMES utf8",
                        "model": "mysql4",
                        "type": "pdo_mysql",
                        "pdoType": "",
                        "active": true
                    }
                ],
                "redis_config": {
                    "used": true,
                    "params": {
                        "host": "",
                        "port": "6379",
                        "timeout": "2.5",
                        "db": 1,
                        "compression_threshold": 2048,
                        "compression_lib": "gzip",
                        "log_level": 4
                    }
                },
                "cache": {
                    "used": true,
                    "backend": "Cm_Cache_Backend_Redis",
                    "backend_options": {
                        "server": "lulu-cache-prod.7ddf4w.0001.usw2.cache.amazonaws.com",
                        "port": "6379",
                        "database": "0",
                        "force_standalone": "0",
                        "read_timeout": 10,
                        "automatic_cleaning_factor": "0",
                        "compress_data": "1",
                        "compress_tags": "1",
                        "compress_threshold": "20480",
                        "compression_lib": "gzip",
                        "connect_retries": 1,
                        "lifetimelimit": 43200,
                        "use_lua": 0
                    }
                }
            }
        }
    },
    "apache": {
        "version": "2.4",
        "mpm": "prefork",
        "listen": [
            "80",
            "443"
        ],
        "log_config_custom": {
            "enabled": true,
            "set_variables": [
                "SetEnvIf Request_URI \"^/ping\\.php$\" dontlog",
                "SetEnvIf Request_URI \"^/apache\\-stats\" dontlog",
                "SetEnvIf Remote_Addr \"127\\.0\\.0\\.1\" dontlog",
                "SetEnvIf User-Agent \"ELB-HealthChecker\" dontlog",
                "SetEnvIf User-Agent \"NewRelicPinger\" dontlog",
                "SetEnvIf User-Agent \"Panopta\\ v1\\.1\" dontlog",
                "SetEnvIf User-Agent \"Amazon\\ CloudFront\" dontlog",
                "SetEnvIf X-Forwarded-For \"104\\.254\\.65\\.197\" dontlog",
                "SetEnvIf REMOTE_ADDR \"(.+)\" CLIENTIP=$1",
                "SetEnvIf X-Forwarded-For \"^([0-9.]+)\" CLIENTIP=$1"
            ],
            "format_line": "%{CLIENTIP}e %D %u %{%d/%m/%Y:%H:%M:%S}t \\\"%r\\\" \\\"STATUS_CODE %>s\\\" \\\"%{Referer}i\\\" \\\"%{User-Agent}i\\\"",
            "format_name": "trueip_combined",
            "format_env": "env=!dontlog"
        }
    },
    "nfs_mounts": [
        {
            "nfs_server_path": "127.0.0.1:/exports/nfs",
            "nfs_mountpoint": "/srv/nfs",
            "nfs_media": "/srv/nfs/media",
            "nfs_type": "nfs4",
            "nfs_options": "defaults",
            "nfs_target_media": "/srv/www/magento/current/media",
            "git_media": "/tmp/opsworks*/archive.d/media"
        }
    ],
    "nfsexport": {
        "dir": "/exports/nfs",
        "subnet": "10.8.0.0/16"
    },
    "php": {
        "packages": [
            "php7.0-bcmath",
            "php-imagick",
            "php-redis",
            "php-igbinary",
            "php7.0-zip",
            "php7.0-soap",
            "php7.0-mbstring",
            "php7.0-ldap",
            "php7.0-imap",
            "php7.0-xsl",
            "php7.0-curl",
            "php7.0-xmlrpc",
            "php7.0-sqlite",
            "php7.0-dev",
            "php7.0-gd",
            "php7.0-cli",
            "php7.0-mcrypt",
            "php7.0-mysql"
        ]
    },
    "ssh_users": {},
    "domains": [
        "luluandgeorgia.com"
    ],
    "htaccess_admin": true
}
unless a[:nfs_mounts].nil?            # Проверяем что такая секция есть
  a[:nfs_mounts].each do |nfs_mount|  # Перебираем все элементы в списке
      p nfs_mount[:nfs_mountpoint]    # Выводим нужные переменные
      p nfs_mount[:nfs_options]
  end
end

Чтобы прочитать вложенную коллекцию, надо использовать 2 перпеменных после each

a[:deploy].each do |ap, deploy|
  p ("#{ap} output")                       # Получим вывод "magento output"
  unless deploy[:site_users].nil?          
    deploy[:site_users].each do |site_user|
      p site_user[:user_name]              # В данном примере нет такого, ничего не выведет
      end
end
end

 

Chef

Related Articles

0 Comment

Leave a Comment

Ваш адрес email не будет опубликован.