├── .github └── workflows │ └── build.yml ├── .gitignore ├── .rspec ├── CHANGELOG.md ├── Gemfile ├── LICENSE.txt ├── README.md ├── Rakefile ├── ansible_spec.gemspec ├── bin └── ansiblespec-init ├── lib ├── ansible_spec.rb ├── ansible_spec │ ├── load_ansible.rb │ ├── vendor │ │ ├── MIT-LICENSE │ │ └── hash.rb │ └── version.rb └── src │ ├── .ansiblespec │ ├── .rspec │ ├── Rakefile │ └── spec │ └── spec_helper.rb └── spec ├── case ├── get_hash_behaviour │ ├── basic │ │ ├── .ansiblespec │ │ ├── hosts │ │ ├── roles │ │ │ └── test │ │ │ │ └── defaults │ │ │ │ └── main.yml │ │ └── site.yml │ └── with_ansiblespec │ │ ├── .ansiblespec │ │ ├── hosts │ │ ├── roles │ │ └── test │ │ │ └── defaults │ │ │ └── main.yml │ │ └── site.yml ├── get_variable │ ├── deep_merge │ │ ├── .ansiblespec │ │ ├── hosts │ │ ├── roles │ │ │ └── test │ │ │ │ └── defaults │ │ │ │ └── main.yml │ │ └── site.yml │ ├── empty_group │ │ ├── group_vars │ │ │ ├── group_a.yml │ │ │ └── group_b.yml │ │ ├── hosts │ │ └── site.yml │ ├── group_all │ │ ├── group_vars │ │ │ └── all.yml │ │ ├── hosts │ │ ├── roles │ │ │ └── test │ │ │ │ └── defaults │ │ │ │ └── main.yml │ │ └── site.yml │ ├── group_each_vars │ │ ├── group_vars │ │ │ ├── all.yml │ │ │ └── group1.yml │ │ ├── hosts │ │ └── site.yml │ ├── group_each_vars_parent_child │ │ ├── group_vars │ │ │ ├── all.yml │ │ │ ├── group1 │ │ │ │ └── vars │ │ │ ├── group2 │ │ │ │ └── vars │ │ │ └── parentgroup │ │ │ │ └── vars │ │ ├── hosts │ │ ├── site.yml │ │ ├── site1.yml │ │ ├── site2.yml │ │ └── site3.yml │ ├── group_host_playbook │ │ ├── group_vars │ │ │ ├── all.yml │ │ │ └── group1.yml │ │ ├── host_vars │ │ │ ├── 192.168.1.1.yml │ │ │ └── 192.168.1.2.yml │ │ ├── hosts │ │ └── site.yml │ ├── group_host_playbook_role │ │ ├── group_vars │ │ │ ├── all.yml │ │ │ └── group1.yml │ │ ├── host_vars │ │ │ ├── 192.168.1.1.yml │ │ │ └── 192.168.1.2.yml │ │ ├── hosts │ │ ├── roles │ │ │ └── test │ │ │ │ └── vars │ │ │ │ └── main.yml │ │ └── site.yml │ ├── group_vars_hosts_childrens_env_dev │ │ ├── .ansiblespec │ │ ├── dynamic_inventory.sh │ │ ├── group_vars │ │ │ ├── all │ │ │ ├── application-servers │ │ │ ├── database-servers │ │ │ ├── develop │ │ │ ├── develop-application-servers │ │ │ ├── develop-database-servers │ │ │ ├── develop-web-servers │ │ │ ├── production │ │ │ ├── production-application-servers │ │ │ ├── production-database-servers │ │ │ ├── production-web-servers │ │ │ ├── staging │ │ │ ├── staging-application-servers │ │ │ ├── staging-database-servers │ │ │ ├── staging-web-servers │ │ │ └── web-servers │ │ └── site.yml │ ├── group_vars_hosts_childrens_env_prd │ │ ├── .ansiblespec │ │ ├── dynamic_inventory.sh │ │ ├── group_vars │ │ │ ├── all │ │ │ ├── application-servers │ │ │ ├── database-servers │ │ │ ├── develop │ │ │ ├── develop-application-servers │ │ │ ├── develop-database-servers │ │ │ ├── develop-web-servers │ │ │ ├── production │ │ │ ├── production-application-servers │ │ │ ├── production-database-servers │ │ │ ├── production-web-servers │ │ │ ├── staging │ │ │ ├── staging-application-servers │ │ │ ├── staging-database-servers │ │ │ ├── staging-web-servers │ │ │ └── web-servers │ │ └── site.yml │ ├── group_vars_hosts_childrens_env_stg │ │ ├── .ansiblespec │ │ ├── dynamic_inventory.sh │ │ ├── group_vars │ │ │ ├── all │ │ │ ├── application-servers │ │ │ ├── database-servers │ │ │ ├── develop │ │ │ ├── develop-application-servers │ │ │ ├── develop-database-servers │ │ │ ├── develop-web-servers │ │ │ ├── production │ │ │ ├── production-application-servers │ │ │ ├── production-database-servers │ │ │ ├── production-web-servers │ │ │ ├── staging │ │ │ ├── staging-application-servers │ │ │ ├── staging-database-servers │ │ │ ├── staging-web-servers │ │ │ └── web-servers │ │ └── site.yml │ ├── host_vars │ │ ├── host_vars │ │ │ ├── 192.168.1.1.yml │ │ │ └── 192.168.1.2.yml │ │ ├── hosts │ │ └── site.yml │ ├── inventories_env_group_all │ │ ├── inventories │ │ │ ├── development │ │ │ │ ├── group_vars │ │ │ │ │ └── all.yml │ │ │ │ └── hosts │ │ │ ├── production │ │ │ │ ├── group_vars │ │ │ │ │ └── all.yml │ │ │ │ └── hosts │ │ │ └── staging │ │ │ │ ├── group_vars │ │ │ │ └── all.yml │ │ │ │ └── hosts │ │ ├── roles │ │ │ └── test │ │ │ │ └── defaults │ │ │ │ └── main.yml │ │ └── site.yml │ ├── inventories_env_group_each_vars │ │ ├── inventories │ │ │ ├── development │ │ │ │ ├── group_vars │ │ │ │ │ ├── all.yml │ │ │ │ │ └── group1.yml │ │ │ │ └── hosts │ │ │ ├── production │ │ │ │ ├── group_vars │ │ │ │ │ ├── all.yml │ │ │ │ │ └── group1.yml │ │ │ │ └── hosts │ │ │ └── staging │ │ │ │ ├── group_vars │ │ │ │ ├── all.yml │ │ │ │ └── group1.yml │ │ │ │ └── hosts │ │ └── site.yml │ ├── inventories_env_host_vars │ │ ├── inventories │ │ │ ├── development │ │ │ │ ├── host_vars │ │ │ │ │ ├── 192.168.1.1.yml │ │ │ │ │ └── 192.168.1.2.yml │ │ │ │ └── hosts │ │ │ ├── production │ │ │ │ ├── host_vars │ │ │ │ │ ├── 192.168.3.1.yml │ │ │ │ │ └── 192.168.3.2.yml │ │ │ │ └── hosts │ │ │ └── staging │ │ │ │ ├── host_vars │ │ │ │ ├── 192.168.2.1.yml │ │ │ │ └── 192.168.2.2.yml │ │ │ │ └── hosts │ │ └── site.yml │ ├── playbook_only │ │ ├── hosts │ │ └── site.yml │ ├── resolve_variables │ │ ├── group_vars │ │ │ └── all.yml │ │ ├── hosts │ │ └── site.yml │ └── roles_default │ │ ├── hosts │ │ ├── roles │ │ └── test │ │ │ └── defaults │ │ │ └── main.yml │ │ └── site.yml └── load_vars_file │ ├── group_all │ ├── .ansiblespec │ └── group_vars │ │ ├── all │ │ └── all.yml │ ├── group_all_deep_merge │ ├── .ansiblespec │ └── group_vars │ │ └── all.yml │ ├── vars_dir │ └── group_vars │ │ └── all │ │ ├── file1 │ │ └── file2 │ └── vault_dir │ ├── ansible.cfg │ ├── group_vars │ └── all │ │ └── vault_file │ └── vault-password-file ├── commands_spec.rb ├── dependency_spec.rb ├── dynamic_inventory_hosts_childrens_spec.rb ├── dynamic_inventory_spec.rb ├── ec2_dynamic_inventory_spec.rb ├── get_variable_hosts_childrens_spec.rb ├── get_variable_spec.rb ├── get_variable_vars_dirs_path_spec.rb ├── get_vars_dirs_path_spec.rb ├── inventory_parameters_spec.rb ├── load_ansible_cfg_spec.rb ├── load_ansible_cfg_ssh_spec.rb ├── load_ansible_spec.rb ├── spec_helper.rb └── ssh_spec.rb /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | # This workflow uses actions that are not certified by GitHub. 2 | # They are provided by a third-party and are governed by 3 | # separate terms of service, privacy policy, and support 4 | # documentation. 5 | # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake 6 | # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby 7 | 8 | name: build 9 | 10 | on: 11 | push: 12 | branches: [ master ] 13 | pull_request: 14 | branches: [ master ] 15 | 16 | jobs: 17 | test: 18 | 19 | runs-on: ubuntu-latest 20 | strategy: 21 | matrix: 22 | # https://github.com/ruby/setup-ruby#supported-versions 23 | # EOL >= 2.6 24 | ruby-version: ['2.6.10', '2.7.7', '3.0.5', '3.1.3'] 25 | 26 | steps: 27 | - uses: actions/checkout@v3 28 | - name: Set up Ruby 29 | # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby, 30 | # change this to (see https://github.com/ruby/setup-ruby#versioning): 31 | uses: ruby/setup-ruby@v1 32 | #uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e 33 | with: 34 | ruby-version: ${{ matrix.ruby-version }} 35 | # 'latest', the latest compatible Bundler version is installed (Bundler 2 on Ruby >= 2.3, Bundler 1 on Ruby < 2.3). 36 | bundler: latest 37 | bundler-cache: true # runs 'bundle install' and caches installed gems automatically 38 | - name: Install dependencies 39 | run: bundle install --jobs=3 --retry=3 40 | - name: check env 41 | run: | 42 | bundle list 43 | - name: Run tests 44 | run: bundle exec rspec spec 45 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | *.rbc 3 | .bundle 4 | .config 5 | .yardoc 6 | Gemfile.lock 7 | InstalledFiles 8 | _yardoc 9 | coverage 10 | doc/ 11 | lib/bundler/man 12 | pkg 13 | rdoc 14 | spec/reports 15 | test/tmp 16 | test/version_tmp 17 | tmp 18 | .DS_Store 19 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --color 2 | --require spec_helper 3 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # v0.3.2 2 | - Merge [#135 Fix wrong index by empty group](https://github.com/volanja/ansible_spec/pull/135) by [mamiya312](https://github.com/mamiya312) 3 | 4 | # v0.3.1 5 | - Fix [#126 no implicit conversion of nil into Array Error when process get_parent](https://github.com/volanja/ansible_spec/issues/126) by [shogos3](https://github.com/shogos3) 6 | 7 | # v0.3 8 | - Support Ansible Vault (Run Ruby 2.1.0 and higher.) 9 | - Merge [#116 Feature ansible vault](https://github.com/volanja/ansible_spec/pull/116) by [RebelCodeBase](https://github.com/RebelCodeBase) 10 | - Merge [#123 skip test at unsupported version ref ansible vault](https://github.com/volanja/ansible_spec/pull/123) by volanja 11 | 12 | And Some Bug Fixed. See [Milestone v0.3](https://github.com/volanja/ansible_spec/milestone/13?closed=1) 13 | 14 | # v0.2.25 15 | - Merge [#118 Incorporate group parent child relationships into variable assignment hierarchy](https://github.com/volanja/ansible_spec/pull/118) by [rbramwell](https://github.com/rbramwell) 16 | 17 | # v0.2.24 18 | - Merge [#117 Feature resolve variables](https://github.com/volanja/ansible_spec/pull/117) by [RebelCodeBase](https://github.com/RebelCodeBase) 19 | 20 | # v0.2.23 21 | - Merge [#115 Support import_playbook](https://github.com/volanja/ansible_spec/pull/115) by [seiji](https://github.com/seiji) 22 | - Merge [#113 Make ENV variables override SSH config file options](https://github.com/volanja/ansible_spec/pull/113) by [Jonnymcc](https://github.com/Jonnymcc) 23 | 24 | # v0.2.22 25 | - Merge [#111 fix sudo directive for after Ansible 1.9](https://github.com/volanja/ansible_spec/pull/111) by [katsuhisa91](https://github.com/katsuhisa91) 26 | 27 | # v0.2.21 28 | - Merge [#103 Parse the full included playbook](https://github.com/volanja/ansible_spec/pull/103) by [agx](https://github.com/agx) 29 | 30 | # v0.2.20 31 | - Merge [#101 Added feature set path to group_vars and host_vars](https://github.com/volanja/ansible_spec/pull/101) by [rbramwell](https://github.com/rbramwell) 32 | - Merge [#99 Unbreak 'serverspec:all' if groups with no hosts exist](https://github.com/volanja/ansible_spec/pull/99) by [agx](https://github.com/agx) 33 | 34 | 35 | # v0.2.19 36 | - Merge [#98 delete directory('roles','more_roles') after execute rpsec](https://github.com/volanja/ansible_spec/pull/98) by volanja 37 | - Merge [#97 Add local connection test path](https://github.com/volanja/ansible_spec/pull/97) by [mtoriumi](https://github.com/mtoriumi) 38 | 39 | # v0.2.18 40 | - Merge [#96 Handle roles_path for role dependencies as well](https://github.com/volanja/ansible_spec/pull/96) by [agx](https://github.com/agx) 41 | 42 | # v0.2.17 43 | - Merge [#90 Check if all array elements have a name](https://github.com/volanja/ansible_spec/pull/90) by [agx](https://github.com/agx) 44 | - Merge [#91 Parse roledirs from ansible.cfg](https://github.com/volanja/ansible_spec/pull/91) by [agx](https://github.com/agx) 45 | - Merge [#92 should use winrm < v2.1.1 at ruby 1.9.3](https://github.com/volanja/ansible_spec/pull/92) by volanja 46 | - Merge [#93 #89 use ENV['SSH_CONFIG_FILE'] or ssh_args at ansible_cfg](https://github.com/volanja/ansible_spec/pull/93) by volanja 47 | Original idea [#89 set a option, it can select file of SSH-configration](https://github.com/volanja/ansible_spec/pull/89) by [gigathlete](https://github.com/gigathlete) 48 | - Merge [add test at ruby 2.4.0 & 2.3.3 & 2.2.6 (drop test 2.3.1 & 2.2.5)](https://github.com/volanja/ansible_spec/pull/94) by volanja 49 | 50 | # v0.2.16 51 | - Merge [#88 fix nil dependencies](https://github.com/volanja/ansible_spec/pull/88) by [developerinlondon](https://github.com/developerinlondon) 52 | - Merge [#87 added .DS_Store to gitignore](https://github.com/volanja/ansible_spec/pull/87) by [developerinlondon](https://github.com/developerinlondon) 53 | - Merge [#85 #84 modify variable expansion](https://github.com/volanja/ansible_spec/pull/85) by volanja 54 | - Merge [#84 On errors print the playbook name](https://github.com/volanja/ansible_spec/pull/84) by [agx](https://github.com/agx) 55 | - Merge [#83 Handle "simple" role dependencies](https://github.com/volanja/ansible_spec/pull/83) by [agx](https://github.com/agx) 56 | - Merge [#82 Handle directories with vars as well](https://github.com/volanja/ansible_spec/pull/82) by [agx](https://github.com/agx) 57 | - Merge [#78 should use json v1.8.3 at ruby 1.9.3](https://github.com/volanja/ansible_spec/pull/78) by volanja 58 | - Merge [#77 support ansible_host, ansible_user, ansible_port](https://github.com/volanja/ansible_spec/pull/77) by volanja 59 | 60 | # v0.2.15 61 | - Merge [Add .rspec when run `ansiblespec-init`](https://github.com/volanja/ansible_spec/pull/76) by volanja 62 | 63 | # v0.2.14 64 | - Merge [Fix NoMethodError at get_variables](https://github.com/volanja/ansible_spec/pull/75) by [hico-horiuchi](https://github.com/hico-horiuchi) 65 | 66 | # v0.2.13 67 | - Merge [Dynamic Inventory use multiple hosts, lookup group children dependency group_vars.](https://github.com/volanja/ansible_spec/pull/73) by [guyon](https://github.com/guyon) 68 | 69 | # v0.2.12 70 | - Merge [Support deep merge for variable](https://github.com/volanja/ansible_spec/pull/72) by [okamototk](https://github.com/okamototk) 71 | 72 | # v0.2.11 73 | - Merge [Support Windows](https://github.com/volanja/ansible_spec/pull/68) by [takuyakawabuchi](https://github.com/takuyakawabuchi) 74 | 75 | # v0.2.10 76 | - Merge [support group and host variables without yml extention.](https://github.com/volanja/ansible_spec/pull/66) by [okamototk](https://github.com/okamototk) 77 | 78 | # v0.2.9 79 | - Merge [#65 return an empty array when the group has no hosts in it](https://github.com/volanja/ansible_spec/pull/65) by [franmrl](https://github.com/franmrl) 80 | 81 | # v0.2.8 82 | - Merge [#60 Support ansible variable in playbook partially](https://github.com/volanja/ansible_spec/pull/60) by [okamototk](https://github.com/okamototk) 83 | 84 | # v0.2.7 85 | - Fix [#55 Connection fails where dynamic inventory has basic host list](https://github.com/volanja/ansible_spec/issues/55) by [temyers](https://github.com/temyers) 86 | - Merge [#59 Issue55 fail dynamic inventory](https://github.com/volanja/ansible_spec/pull/59) 87 | 88 | # v0.2.6 89 | - Merge [#53 Execute tests for dependent roles](https://github.com/volanja/ansible_spec/pull/53) by [Gerrrr](https://github.com/Gerrrr) 90 | 91 | # v0.2.5 92 | - Merge [#51 Handle hosts which are not assigned to any group](https://github.com/volanja/ansible_spec/pull/51) by [Gerrrr](https://github.com/Gerrrr) 93 | 94 | # v0.2.4 95 | - Fix [#50 do not fail on no hosts matched](https://github.com/volanja/ansible_spec/pull/50) by [phiche](https://github.com/phiche) 96 | 97 | # v0.2.3 98 | - Fix [#47 NoMethodError: undefined method 'each' for nil:NilClass if inventory has "roles"=>[]](https://github.com/volanja/ansible_spec/issues/47) by [phiche](https://github.com/phiche) 99 | 100 | # v0.2.2 101 | - Fix [#43 Enable to set only either of `PLAYBOOK` or `INVENTORY`.](https://github.com/volanja/ansible_spec/issues/43) by [akagisho](https://github.com/akagisho) 102 | - Fix [#45 ansible_spec cannot use ec2.py dynamic inventory](https://github.com/volanja/ansible_spec/issues/45) by [phiche](https://github.com/phiche) 103 | 104 | # v0.2.1 105 | - fix [#27 check name on playbook](https://github.com/volanja/ansible_spec/issues/27) 106 | - Add Test 107 | - 2.1.6 108 | - 2.2.2 109 | - Delete Test 110 | - 2.1.0 111 | - 2.1.1 112 | - 2.2.0 113 | - 2.2.1 114 | 115 | # v0.2 116 | - fix [#24 Support ENV](https://github.com/volanja/ansible_spec/issues/24) 117 | 118 | ``` 119 | Example: 120 | $ PLAYBOOK=site.yml INVENTORY=hosts rake serverspec:Ansible-Sample-TDD 121 | ``` 122 | 123 | # v0.1.1 124 | 125 | fix [#22 NameError: uninitialized constant AnsibleSpec::Open3](https://github.com/volanja/ansible_spec/issues/22) 126 | 127 | 128 | # v0.1 129 | 130 | - Support Serverspec v2 131 | - Simplification Rakefile and Modularization. Because of Improvement of testability. 132 | - Support InventoryParameters 133 | - ansible_ssh_port 134 | - ansible_ssh_user 135 | - ansible_ssh_host 136 | - ansible_ssh_private_key_file 137 | - Support [hostlist expressions](http://docs.ansible.com/intro_inventory.html#hosts-and-groups) 138 | - Support DynamicInventory 139 | 140 | This gem created template file until v0.0.1.4, 141 | But it was modularized on v0.1. Because module is easy to unit-test and Rakefile is simple. 142 | 143 | If you want old release that can create template, use `gem install ansible_spec -v 0.0.1.4` 144 | But I can't support(Bug fix, Add feature) old release. 145 | 146 | ## VersionUp from v0.0.1.4 to v0.1 147 | 148 | ``` 149 | $ rm Rakefile 150 | $ rm spec/spec_helper.md 151 | $ ansiblespec-init 152 | ``` 153 | 154 | # v0.0.1.3 155 | - Support `.ansiblespec` 156 | 157 | 158 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # Specify your gem's dependencies in ansible_spec.gemspec 4 | gemspec 5 | 6 | if Gem::Version.new(RUBY_VERSION.dup) < Gem::Version.new('2.0.0') 7 | # net-ssh 3.x dropped Ruby 1.8 and 1.9 support. 8 | gem 'net-ssh', '~> 2.7' 9 | end 10 | 11 | if Gem::Version.new(RUBY_VERSION.dup) >= Gem::Version.new('2.1') 12 | # Ansible::Vault support Ruby 2.1.0 and higher. 13 | gem 'ansible-vault' 14 | end 15 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016 volanja 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ansible_spec 2 | 3 | [![Gem Version](https://badge.fury.io/rb/ansible_spec.svg)](https://badge.fury.io/rb/ansible_spec) 4 | [![build](https://github.com/volanja/ansible_spec/actions/workflows/build.yml/badge.svg)](https://github.com/volanja/ansible_spec/actions/workflows/build.yml) 5 | 6 | This is a Ruby gem that implements an Ansible Config Parser for Serverspec. 7 | It creates a Rake task that can run tests, using Ansible inventory files 8 | and playbooks. You can test multiple roles and multiple hosts. 9 | 10 | # Features 11 | 12 | - Supports [Serverspec](http://serverspec.org/) v2 13 | - Supports special host variables 14 | - `ansible_ssh_port` or `ansible_port` 15 | - `ansible_ssh_user` or `ansible_user` 16 | - `ansible_ssh_host` or `ansible_host` 17 | - `ansible_ssh_private_key_file` 18 | - Supports [host patterns/ranges](http://docs.ansible.com/intro_inventory.html#hosts-and-groups) -- e.g.: `www[01:50].example.com` 19 | - Supports Ansible [dynamic inventory sources](http://docs.ansible.com/ansible/intro_dynamic_inventory.html) 20 | 21 | # Installation 22 | 23 | ``` 24 | $ gem install ansible_spec 25 | ``` 26 | 27 | # Usage 28 | 29 | ## Create `Rakefile` & `spec/spec_helper.rb` 30 | 31 | ``` 32 | $ ansiblespec-init 33 | create spec 34 | create spec/spec_helper.rb 35 | create Rakefile 36 | create .ansiblespec 37 | create .rspec 38 | ``` 39 | 40 | ## [Optional] `.ansiblespec` 41 | 42 | By default, `site.yml` will be used as the playbook and `hosts` as the 43 | inventory file. You can either follow these conventions or you can 44 | customize the playbook and inventory using an `.ansiblespec` file. 45 | 46 | ```.ansiblespec 47 | --- 48 | - 49 | playbook: site.yml 50 | inventory: hosts 51 | vars_dirs_path: inventories/staging 52 | hash_behaviour: merge 53 | ``` 54 | 55 | ## [Optional] Environment variables 56 | 57 | You can use environment variables with the `rake` command. They are listed below. 58 | 59 | - `PLAYBOOK` -- playbook name (e.g. `site.yml`) 60 | - `INVENTORY` -- inventory file name (e.g. `hosts`) 61 | - `VARS_DIRS_PATH` -- directory path containing the group_vars and host_vars directories (e.g. `inventories/staging`) 62 | - `HASH_BEHAVIOUR` -- hash behaviour when duplicate hash variables (e.g. `merge`) 63 | - `SSH_CONFIG_FILE` -- ssh configuration file path (e.g. `ssh_config`) 64 | `SSH_CONFIG_FILE` take precedence over the path at ssh_args( -F "filename") in [ssh_connection] section of ansible.cfg 65 | 66 | 67 | Environment variables take precedence over the `.ansiblespec` file. 68 | 69 | Example: 70 | 71 | ``` 72 | $ PLAYBOOK=site.yml INVENTORY=hosts rake serverspec:Ansible-Sample-TDD 73 | or 74 | $ PLAYBOOK=site.yml rake serverspec:Ansible-Sample-TDD 75 | or 76 | $ INVENTORY=hosts rake serverspec:Ansible-Sample-TDD 77 | or 78 | $ VARS_DIRS_PATH=inventories/staging rake serverspec:Ansible-Sample-TDD 79 | or 80 | $ HASH_BEHAVIOUR=merge rake serverspec:Ansible-Sample-TDD 81 | ``` 82 | 83 | HASH_BEHAVIOUR is same as Ansible's hash behaviour parameter. By default, 'replace'. 84 | See http://docs.ansible.com/ansible/intro_configuration.html#hash-behaviour. 85 | 86 | ## Inventory 87 | 88 | Inventory files can: 89 | 90 | - use standard ansible parameters 91 | - `ansible_ssh_port` or `ansible_port` 92 | - `ansible_ssh_user` or `ansible_user` 93 | - `ansible_ssh_host` or `ansible_host` 94 | - `ansible_ssh_private_key_file` 95 | - define hosts as expressions. `host-[1:3]` would expand into `host-1`,`host-2`,`host-3` 96 | - Group Children 97 | - Use [dynamic inventory sources](http://docs.ansible.com/intro_dynamic_inventory.html) 98 | 99 | ### Sample 100 | 101 | ```hosts 102 | [server] 103 | # skip line(comment) 104 | # normal 105 | 192.168.0.1 106 | # use port 5309 107 | 192.168.0.3:5309 108 | # use port 22 109 | 192.168.0.2 ansible_ssh_port=22 110 | # use Private-key ~/.ssh/id_rsa 111 | 192.168.0.4 ansible_ssh_private_key_file=~/.ssh/id_rsa 112 | # use user `git` 113 | 192.168.0.5 ansible_ssh_user=git 114 | # use port 5555 & host 192.168.1.50 115 | jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50 116 | # Ansible 2.0 117 | 192.168.10.2 ansible_port=2222 118 | 192.168.10.5 ansible_user=git 119 | jumper2 ansible_port=5555 ansible_host=192.168.10.50 120 | 121 | [web] 122 | # www1.example.com to www99.example.com 123 | www[1:99].example.com 124 | # www01.example.com to www99.example.com 125 | www[01:99].example.com 126 | 127 | [databases] 128 | # db-a.example.com to db-z.example.com 129 | db-[a:z].example.com 130 | # db-A.example.com to db-Z.example.com 131 | db-[A:Z].example.com 132 | 133 | # Multi Group. use server & databases 134 | [parent:children] 135 | server 136 | databases 137 | ``` 138 | 139 | ## Dynamic Inventory Sources 140 | 141 | (Note: These files need to have execute permission) 142 | 143 | ``` 144 | #!/bin/bash 145 | echo '{"databases": {"hosts": ["host1.example.com", "host2.example.com"],"vars":{"a": true}}}' 146 | ``` 147 | 148 | ## Variables 149 | 150 | Ansible variables supported by following condition. 151 | 152 | * Playbook's variables are supported. If same variable is defined in different places, 153 | priority follows [Ansible order](http://docs.ansible.com/ansible/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable). 154 | * Variables defined main.yml in role's tasks are not supported. 155 | * Inventory variables are not supported. 156 | * Facts are not supported. 157 | 158 | ## SSH config priority (from v0.2.23) 159 | 160 | 1. ENV['SSH_CONFIG_FILE'] (high priority) 161 | 1. ssh_args( -F "filename") in [ssh_connection] section of ansible.cfg 162 | 1. user from site.yml 163 | 1. ~/.ssh/config (low priority) 164 | 165 | ### Sample 166 | 167 | Support variables are in site.yml, group_vars, host_vars, roles. 168 | 169 | ``` 170 | ├── site.yml 171 | ├── group_vars 172 | │  ├── all.yml 173 | │   ├── dbserver.yml 174 | │  └── webserver.yml 175 | ├── host_vars 176 | │  ├── 192.168.1.1.yml 177 | │  └── 192.168.1.2.yml 178 | └── roles 179 | ├── apaches 180 | │ └── vars 181 | │ └── main.yml 182 | └── mariadb 183 | └── vars 184 | └── main.yml 185 | 186 | ``` 187 | 188 | and (e.g. when `vars_dirs_path: inventories/staging`) 189 | 190 | ``` 191 | ├── site.yml 192 | ├── inventories 193 | │  ├── development 194 | | | ├── group_vars 195 | | | │  ├── all.yml 196 | | | │   ├── dbserver.yml 197 | | | │  └── webserver.yml 198 | | | └── host_vars 199 | | |   ├── 192.168.1.1.yml 200 | | |   └── 192.168.1.2.yml 201 | │   ├── production 202 | | | ├── group_vars 203 | | | │  ├── all.yml 204 | | | │   ├── dbserver.yml 205 | | | │  └── webserver.yml 206 | | | └── host_vars 207 | | |   ├── 192.168.10.1.yml 208 | | |   └── 192.168.10.2.yml 209 | │  └── staging 210 | | ├── group_vars 211 | | │  ├── all.yml 212 | | │   ├── dbserver.yml 213 | | │  └── webserver.yml 214 | | └── host_vars 215 | |   ├── 192.168.20.1.yml 216 | |   └── 192.168.20.2.yml 217 | └── roles 218 | ├── apaches 219 | │ └── vars 220 | │ └── main.yml 221 | └── mariadb 222 | └── vars 223 | └── main.yml 224 | 225 | ``` 226 | 227 | **Note:** Parse role dirs from ansible.cfg. This allows us to find specs that are not under ./roles. 228 | 229 | ``` 230 | # ansible.cfg 231 | [defaults] 232 | roles_path = moreroles 233 | ``` 234 | 235 | #### Define variable(site.yml) 236 | 237 | 238 | ``` 239 | - name: Ansible-Variable-Sample 240 | hosts: webserver.yml 241 | user: root 242 | vars: 243 | - www_port: 8080 244 | roles: 245 | - nginx 246 | ``` 247 | 248 | #### Spec file(roles/nginx/spec/nginx_spec.rb) 249 | 250 | ``` 251 | describe port(property['www_port']) do 252 | it { should be_listening } 253 | end 254 | ``` 255 | 256 | # Sample 257 | ## Directory 258 | sample is [here](https://github.com/volanja/ansible-sample-tdd) 259 | 260 | ``` 261 | . 262 | ├── .ansiblespec # Create file (use Serverspec). read above section. 263 | ├── .rspec # Create file (use Serverspec). read RSpec Doc. 264 | ├── README.md 265 | ├── hosts # use Ansible and Serverspec if .ansiblespec is not exist. 266 | ├── site.yml # use Ansible and Serverspec if .ansiblespec is not exist. 267 | ├── nginx.yml # (comment-out) incluted by site.yml 268 | ├── roles 269 | │   └── nginx 270 | │   ├── handlers 271 | │   │   └── main.yml 272 | │   ├── spec # use Serverspec 273 | │   │   └── nginx_spec.rb 274 | │   ├── tasks 275 | │   │   └── main.yml 276 | │   ├── templates 277 | │   │   └── nginx.repo 278 | │   └── vars 279 | │   └── main.yml 280 | ├── Rakefile # Create file (use Serverspec) 281 | └── spec # Create file (use Serverspec) 282 | └── spec_helper.rb 283 | ``` 284 | 285 | ## Playbook 286 | 287 | playbook can use `import_playbook` & `include[DEPRECATION]` 288 | 289 | ```site.yml 290 | - name: Ansible-Sample-TDD 291 | hosts: server 292 | user: root 293 | roles: 294 | - nginx 295 | - name: Ansible-Sample-TDD2 296 | hosts: parent 297 | user: root 298 | roles: 299 | - nginx 300 | ``` 301 | 302 | ## Run Test 303 | 304 | ``` 305 | $ rake -T 306 | rake serverspec:Ansible-Sample-TDD # Run serverspec for Ansible-Sample-TDD 307 | rake serverspec:Ansible-Sample-TDD2 # Run serverspec for Ansible-Sample-TDD2 308 | 309 | $ rake serverspec:Ansible-Sample-TDD 310 | Run serverspec for Ansible-Sample-TDD to 192.168.0.103 311 | /Users/Adr/.rvm/rubies/ruby-1.9.3-p194/bin/ruby -S rspec roles/mariadb/spec/mariadb_spec.rb roles/nginx/spec/nginx_spec.rb 312 | ........... 313 | 314 | Finished in 0.34306 seconds 315 | 11 examples, 0 failures 316 | ``` 317 | 318 | # Contributing 319 | 320 | To set up a development environment: 321 | 322 | ``` 323 | $ bundle install 324 | ``` 325 | 326 | To run the tests: 327 | 328 | ``` 329 | $ bundle exec rspec 330 | ``` 331 | 332 | To contribute your change, create a GitHub pull request as follows: 333 | 334 | 1. Fork it 335 | 2. Create your feature branch (`git checkout -b my-new-feature`) 336 | 3. Commit your changes (`git commit -am 'Add some feature'`) 337 | 4. Push to the branch (`git push origin my-new-feature`) 338 | 5. Create new Pull Request at https://github.com/volanja/ansible_spec 339 | 340 | # Old CI 341 | [Travis CI - volanja/ansible_spec](https://travis-ci.org/github/volanja/ansible_spec) 342 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | -------------------------------------------------------------------------------- /ansible_spec.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | lib = File.expand_path('../lib', __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require 'ansible_spec/version' 5 | 6 | Gem::Specification.new do |gem| 7 | gem.name = "ansible_spec" 8 | gem.version = AnsibleSpec::VERSION 9 | gem.authors = ["volanja"] 10 | gem.email = ["volaaanja@gmail.com"] 11 | gem.description = %q{Ansible Config Parser for Serverspec. Run test Multi Role and Multi Host by Ansible Configuration} 12 | gem.summary = %q{Ansible Config Parser for Serverspec. Run test Multi Role and Multi Host by Ansible Configuration} 13 | gem.homepage = "https://github.com/volanja/ansible_spec" 14 | gem.license = "MIT" 15 | 16 | gem.files = `git ls-files`.split($/) 17 | gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) } 18 | gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) 19 | gem.require_paths = ["lib"] 20 | 21 | gem.add_development_dependency "bundler" 22 | gem.add_development_dependency "rake" 23 | gem.add_development_dependency "diff-lcs" 24 | gem.add_development_dependency "simplecov" 25 | 26 | gem.add_runtime_dependency "serverspec", ">= 2.0.0" 27 | gem.add_runtime_dependency "hostlist_expression" 28 | gem.add_runtime_dependency "oj" 29 | gem.add_runtime_dependency "winrm" 30 | gem.add_runtime_dependency "inifile" 31 | gem.add_runtime_dependency "ansible-vault" 32 | 33 | end 34 | -------------------------------------------------------------------------------- /bin/ansiblespec-init: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby 2 | # coding: utf-8 3 | 4 | require 'ansible_spec' 5 | AnsibleSpec.main 6 | 7 | exit 0 8 | -------------------------------------------------------------------------------- /lib/ansible_spec.rb: -------------------------------------------------------------------------------- 1 | require "ansible_spec/version" 2 | require "ansible_spec/load_ansible" 3 | require "fileutils" 4 | 5 | # Reference 6 | # https://github.com/serverspec/serverspec/blob/master/lib/serverspec/setup.rb 7 | # Reference License (MIT) 8 | # https://github.com/serverspec/serverspec/blob/master/LICENSE.txt 9 | 10 | module AnsibleSpec 11 | 12 | def self.main() 13 | safe_create_spec_helper 14 | safe_create_rakefile 15 | safe_create_ansiblespec 16 | safe_create_rspec 17 | end 18 | 19 | 20 | def self.safe_create_spec_helper 21 | content = File.open(File.dirname(__FILE__) + "/../lib/src/spec/spec_helper.rb").read 22 | safe_mkdir("spec") 23 | safe_touch("spec/spec_helper.rb") 24 | File.open("spec/spec_helper.rb", 'w') do |f| 25 | f.puts content 26 | end 27 | 28 | end 29 | 30 | def self.safe_create_rakefile 31 | content = File.open(File.dirname(__FILE__) + "/../lib/src/Rakefile").read 32 | safe_touch("Rakefile") 33 | File.open("Rakefile", 'w') do |f| 34 | f.puts content 35 | end 36 | end 37 | 38 | def self.safe_create_ansiblespec 39 | content = File.open(File.dirname(__FILE__) + "/../lib/src/.ansiblespec").read 40 | safe_touch(".ansiblespec") 41 | File.open(".ansiblespec", 'w') do |f| 42 | f.puts content 43 | end 44 | end 45 | 46 | def self.safe_create_rspec 47 | content = File.open(File.dirname(__FILE__) + "/../lib/src/.rspec").read 48 | safe_touch(".rspec") 49 | File.open(".rspec", 'w') do |f| 50 | f.puts content 51 | end 52 | end 53 | 54 | def self.safe_mkdir(dir) 55 | unless FileTest.exist?("#{dir}") 56 | FileUtils.mkdir_p("#{dir}") 57 | TermColor.green 58 | puts "\t\tcreate\t#{dir}" 59 | TermColor.reset 60 | else 61 | TermColor.red 62 | puts "\t\texists\t#{dir}" 63 | TermColor.reset 64 | end 65 | end 66 | 67 | def self.safe_touch(file) 68 | unless File.exists? "#{file}" 69 | File.open("#{file}", 'w') do |f| 70 | #f.puts content 71 | end 72 | TermColor.green 73 | puts "\t\tcreate\t#{file}" 74 | TermColor.reset 75 | else 76 | TermColor.red 77 | puts "\t\texists\t#{file}" 78 | TermColor.reset 79 | end 80 | end 81 | 82 | class TermColor 83 | class << self 84 | # 色を解除 85 | def reset ; c 0 ; end 86 | 87 | # 各色 88 | def red ; c 31; end 89 | def green ; c 32; end 90 | 91 | # カラーシーケンスの出力 92 | def c(num) 93 | print "\e[#{num.to_s}m" 94 | end 95 | end 96 | end 97 | 98 | end 99 | -------------------------------------------------------------------------------- /lib/ansible_spec/load_ansible.rb: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | require 'hostlist_expression' 3 | require 'oj' 4 | require 'open3' 5 | require 'yaml' 6 | require 'inifile' 7 | require 'ansible_spec/vendor/hash' 8 | if Gem::Version.new(RUBY_VERSION.dup) >= Gem::Version.new('2.1') 9 | require 'ansible/vault' 10 | end 11 | 12 | module AnsibleSpec 13 | # param: inventory file of Ansible 14 | # param: return_type 'groups' or 'groups_parent_child_relationships' 15 | # return: Hash {"group" => ["192.168.0.1","192.168.0.2"]} 16 | # return: Hash {"group" => [{"name" => "192.168.0.1","uri" => "192.168.0.1", "port" => 22},...]} 17 | # return: Hash {"pg" => ["server", "databases"]} 18 | def self.load_targets(file, return_type = 'groups') 19 | if not ['groups', 'groups_parent_child_relationships'].include?(return_type) 20 | raise ArgumentError, "Variable return_type must be value 'groups' or 'groups_parent_child_relationships'" 21 | end 22 | 23 | if File.executable?(file) 24 | return get_dynamic_inventory(file) 25 | end 26 | f = File.open(file).read 27 | groups = Hash.new 28 | group = '' 29 | hosts = Hash.new 30 | hosts.default = Hash.new 31 | f.each_line{|line| 32 | line = line.strip 33 | # skip 34 | next if line.start_with?('#') #comment 35 | next if line.empty? == true #null 36 | 37 | # get group 38 | if line.start_with?('[') && line.end_with?(']') 39 | group = line.gsub('[','').gsub(']','') 40 | groups["#{group}"] = Array.new 41 | next 42 | end 43 | 44 | # get host 45 | host_name = line.split[0] 46 | if group.empty? == false 47 | if groups.has_key?(line) 48 | groups["#{group}"] << line 49 | next 50 | elsif host_name.include?("[") && host_name.include?("]") 51 | # www[01:50].example.com 52 | # db-[a:f].example.com 53 | hostlist_expression(line,":").each{|h| 54 | host = hosts[h.split[0]] 55 | groups["#{group}"] << get_inventory_param(h).merge(host) 56 | } 57 | next 58 | else 59 | # 1つのみ、かつ:を含まない場合 60 | # 192.168.0.1 61 | # 192.168.0.1 ansible_ssh_host=127.0.0.1 ... 62 | host = hosts[host_name] 63 | groups["#{group}"] << get_inventory_param(line).merge(host) 64 | next 65 | end 66 | else 67 | if host_name.include?("[") && host_name.include?("]") 68 | hostlist_expression(line, ":").each{|h| 69 | hosts[h.split[0]] = get_inventory_param(h) 70 | } 71 | else 72 | hosts[host_name] = get_inventory_param(line) 73 | end 74 | end 75 | } 76 | 77 | # parse children [group:children] 78 | search = Regexp.new(":children".to_s) 79 | groups_parent_child_relationships = Hash.new 80 | groups.keys.each{|k| 81 | unless (k =~ search).nil? 82 | # get parent child relationships 83 | k_parent = k.gsub(search,'') 84 | groups_parent_child_relationships["#{k_parent}"] = groups["#{k}"] 85 | # get group parent & merge parent 86 | groups.merge!(get_parent(groups,search,k)) 87 | # delete group children 88 | if groups.has_key?("#{k}") && groups.has_key?("#{k.gsub(search,'')}") 89 | groups.delete("#{k}") 90 | end 91 | end 92 | } 93 | 94 | return_value = groups # default 95 | if return_type == 'groups' 96 | return_value = groups 97 | elsif return_type == 'groups_parent_child_relationships' 98 | return_value = groups_parent_child_relationships 99 | end 100 | 101 | return return_value 102 | end 103 | 104 | # param hash {"server"=>["192.168.0.103"], "databases"=>["192.168.0.104"], "pg:children"=>["server", "databases"]} 105 | # param search ":children" 106 | # param k "pg:children" 107 | # return {"server"=>["192.168.0.103"], "databases"=>["192.168.0.104"], "pg"=>["192.168.0.103", "192.168.0.104"]} 108 | def self.get_parent(hash,search,k) 109 | k_parent = k.gsub(search,'') 110 | arry = Array.new 111 | hash["#{k}"].each{|group| 112 | arry = arry + hash["#{group}"] 113 | } 114 | h = Hash.new 115 | h["#{k_parent}"] = arry 116 | return h 117 | end 118 | 119 | # param filename 120 | # {"databases":{"hosts":["aaa.com","bbb.com"],"vars":{"a":true}}} 121 | # {"webservers":["aaa.com","bbb.com"]} 122 | # return: Hash {"databases"=>[{"uri" => "aaa.com", "port" => 22}, {"uri" => "bbb.com", "port" => 22}]} 123 | def self.get_dynamic_inventory(file) 124 | if file[0] == "/" 125 | file_path = file 126 | else 127 | file_path = "./#{file}" 128 | end 129 | res = Hash.new 130 | so, se, st = Open3.capture3(file_path) 131 | dyn_inv = Oj.load(so.to_s) 132 | 133 | res["hosts_childrens"] = dyn_inv.select do |property, value| 134 | value.instance_of?(Hash) && value.has_key?("children") 135 | end 136 | 137 | if dyn_inv.key?('_meta') 138 | # assume we have an ec2.py created dynamic inventory 139 | dyn_inv = dyn_inv.tap{ |h| h.delete("_meta") } 140 | end 141 | dyn_inv.each{|k,v| 142 | res["#{k.to_s}"] = Array.new unless res.has_key?("#{k.to_s}") 143 | if v.is_a?(Array) 144 | # {"webservers":["aaa.com","bbb.com"]} 145 | v.each {|host| 146 | res["#{k.to_s}"] << {"uri"=> host, "port"=> 22} 147 | } 148 | elsif v.has_key?("hosts") && v['hosts'].is_a?(Array) 149 | v['hosts'].each {|host| 150 | res["#{k.to_s}"] << {"uri"=> host, "port"=> 22} 151 | } 152 | end 153 | } 154 | return res 155 | end 156 | 157 | # param ansible_ssh_port=22 158 | # return: hash 159 | def self.get_inventory_param(line) 160 | host = Hash.new 161 | # 初期値 162 | host['name'] = line 163 | host['port'] = 22 164 | host['connection'] = "ssh" 165 | if line.include?(":") # 192.168.0.1:22 166 | host['uri'] = line.split(":")[0] 167 | host['port'] = line.split(":")[1].to_i 168 | return host 169 | end 170 | # 192.168.0.1 ansible_ssh_port=22 171 | line.split.each{|v| 172 | unless v.include?("=") 173 | host['uri'] = v 174 | else 175 | key,value = v.split("=") 176 | host['port'] = value.to_i if key == "ansible_ssh_port" or key == "ansible_port" 177 | host['private_key'] = value if key == "ansible_ssh_private_key_file" 178 | host['user'] = value if key == "ansible_ssh_user" or key == "ansible_user" 179 | host['uri'] = value if key == "ansible_ssh_host" or key == "ansible_host" 180 | host['pass'] = value if key == "ansible_ssh_pass" 181 | host['connection'] = value if key == "ansible_connection" 182 | end 183 | } 184 | return host 185 | end 186 | 187 | # param: none 188 | # return: playbook, inventoryfile 189 | def self.load_ansiblespec() 190 | f = '.ansiblespec' 191 | y = nil 192 | if File.exist?(f) 193 | y = YAML.load_file(f) 194 | end 195 | if ENV["PLAYBOOK"] 196 | playbook = ENV["PLAYBOOK"] 197 | elsif y.is_a?(Array) && y[0]['playbook'] 198 | playbook = y[0]['playbook'] 199 | else 200 | playbook = 'site.yml' 201 | end 202 | if ENV["INVENTORY"] 203 | inventoryfile = ENV["INVENTORY"] 204 | elsif y.is_a?(Array) && y[0]['inventory'] 205 | inventoryfile = y[0]['inventory'] 206 | else 207 | inventoryfile = 'hosts' 208 | end 209 | if File.exist?(playbook) == false 210 | puts 'Error: ' + playbook + ' is not Found. create site.yml or ./.ansiblespec See https://github.com/volanja/ansible_spec' 211 | exit 1 212 | elsif File.exist?(inventoryfile) == false 213 | puts 'Error: ' + inventoryfile + ' is not Found. create hosts or ./.ansiblespec See https://github.com/volanja/ansible_spec' 214 | exit 1 215 | end 216 | return playbook, inventoryfile 217 | end 218 | 219 | # param: role 220 | # return: ["role1", "role2"] 221 | def self.load_dependencies(role, rolepath='roles') 222 | role_queue = [role] 223 | deps = [] 224 | until role_queue.empty? 225 | role = role_queue.pop() 226 | path = File.join(rolepath, role, "meta", "main.yml") 227 | 228 | if File.exist?(path) 229 | dependencies = YAML.load_file(path).fetch("dependencies", []) 230 | unless dependencies.nil? 231 | new_deps = dependencies.map { |h| 232 | h["role"] || h 233 | } 234 | role_queue.concat(new_deps) 235 | deps.concat(new_deps) 236 | end 237 | end 238 | end 239 | return deps 240 | end 241 | 242 | # param: playbook 243 | # return: json 244 | # {"name"=>"Ansible-Sample-TDD", "hosts"=>"server", "user"=>"root", "roles"=>["nginx", "mariadb"]} 245 | def self.load_playbook(f) 246 | playbook = YAML.load_file(f) 247 | 248 | # e.g. comment-out 249 | if playbook === false 250 | puts "Error: No data in #{f}" 251 | exit 252 | end 253 | # for Ruby 3.1. 254 | if playbook.nil? 255 | puts "Error: No data in #{f}" 256 | exit 257 | end 258 | 259 | properties = Array.new 260 | playbook.each do |site| 261 | if site.has_key?("include") 262 | YAML.load_file(site["include"]).each { |site| 263 | properties.push site 264 | } 265 | elsif site.has_key?("import_playbook") 266 | YAML.load_file(site["import_playbook"]).each { |site| 267 | properties.push site 268 | } 269 | else 270 | properties.push site 271 | end 272 | end 273 | properties.each do |property| 274 | property["roles"] = flatten_role(property["roles"]) 275 | end 276 | if name_exist?(properties) 277 | return properties 278 | else 279 | fail "Please insert name on playbook '#{f}'" 280 | end 281 | end 282 | 283 | # flatten roles (Issue 29) 284 | # param: Array 285 | # e.g. ["nginx"] 286 | # e.g. [{"role"=>"nginx"}] 287 | # e.g. [{"role"=>"nginx", "dir"=>"/opt/b", "port"=>5001}] 288 | # return: Array 289 | # e.g.["nginx"] 290 | def self.flatten_role(roles) 291 | ret = Array.new 292 | if roles 293 | roles.each do |role| 294 | if role.is_a?(String) 295 | ret << role 296 | elsif role.is_a?(Hash) 297 | ret << role["role"] if role.has_key?("role") 298 | end 299 | end 300 | end 301 | return ret 302 | end 303 | 304 | 305 | # Issue 27 306 | # param: array 307 | # return: boolean 308 | # true: name is exist on playbook 309 | # false: name is not exist on playbook 310 | def self.name_exist?(array) 311 | array.all? do |site| 312 | site.has_key?("name") 313 | end 314 | end 315 | 316 | # param: none 317 | # return: hash_behaviour 318 | def self.get_hash_behaviour() 319 | f = '.ansiblespec' 320 | y = nil 321 | if File.exist?(f) 322 | y = YAML.load_file(f) 323 | end 324 | hash_behaviour = 'replace' 325 | if ENV["HASH_BEHAVIOUR"] 326 | hash_behaviour = ENV["HASH_BEHAVIOUR"] 327 | elsif y.is_a?(Array) && y[0]['hash_behaviour'] 328 | hash_behaviour = y[0]['hash_behaviour'] 329 | end 330 | if !['replace','merge'].include?(hash_behaviour) 331 | puts "Error: hash_behaviour '" + hash_behaviour + "' should be 'replace' or 'merge' See https://github.com/volanja/ansible_spec" 332 | exit 1 333 | end 334 | return hash_behaviour 335 | end 336 | 337 | # param: none 338 | # return: file path 339 | def self.get_ssh_config_file() 340 | ssh_config_file = nil 341 | 342 | cfg = AnsibleSpec::AnsibleCfg.new 343 | ssh_args = cfg.get('ssh_connection', 'ssh_args') 344 | if ssh_args 345 | array = ssh_args.split(" ") 346 | if array.index("-F") && array[array.index("-F") + 1] 347 | ssh_config_file = array[array.index("-F") + 1] 348 | end 349 | end 350 | 351 | if ENV["SSH_CONFIG_FILE"] 352 | ssh_config_file = ENV["SSH_CONFIG_FILE"] 353 | end 354 | 355 | return nil if ssh_config_file.nil? 356 | 357 | if File.exist?(ssh_config_file) 358 | return ssh_config_file 359 | else 360 | return nil 361 | end 362 | end 363 | 364 | # param: hash 365 | # param: variable file 366 | # param: flag to extention 367 | # true: .yml extension is optional 368 | # false: must have .yml extention 369 | def self.load_vars_file(vars, path, check_no_ext = false) 370 | vars_file = path 371 | if check_no_ext && !File.exist?(vars_file) 372 | vars_file = path+".yml" 373 | end 374 | if File.exist?(vars_file) 375 | if File.directory?(vars_file) 376 | Dir.glob(File.join(vars_file, '*')).each { |f| 377 | vars = load_vars_file(vars, f) 378 | } 379 | else 380 | # you can use Ansible::Vault when use ruby 2.1.0 and higher. 381 | # Ansible::Vault support Ruby 2.1.0 and higher. 382 | if Gem::Version.new(RUBY_VERSION.dup) >= Gem::Version.new('2.1') 383 | if Ansible::Vault.encrypted?(vars_file) 384 | yaml = load_encrypted_file(vars_file) 385 | else 386 | yaml = YAML.load_file(vars_file) 387 | end 388 | vars = merge_variables(vars, yaml) 389 | else 390 | # Ruby 1.9 and 2.0 391 | yaml = YAML.load_file(vars_file) 392 | vars = merge_variables(vars, yaml) 393 | end 394 | end 395 | end 396 | return vars 397 | end 398 | 399 | # param: variable file 400 | # return: be merged hash 401 | def self.load_encrypted_file(vars_file) 402 | cfg = AnsibleSpec::AnsibleCfg.new 403 | vault_password_file = cfg.get('defaults', 'vault_password_file') 404 | if vault_password_file 405 | vault_password = File.open(vault_password_file).read.chomp 406 | yaml = YAML.load(Ansible::Vault.read(path: vars_file, password: vault_password)) 407 | end 408 | return yaml 409 | end 410 | 411 | # param: target hash 412 | # param: be merged hash 413 | def self.merge_variables(vars, hash) 414 | hash_behaviour = get_hash_behaviour() 415 | if hash.kind_of?(Hash) 416 | if hash_behaviour=="merge" 417 | vars.deep_merge!(hash) 418 | else 419 | vars.merge!(hash) 420 | end 421 | end 422 | return vars 423 | end 424 | 425 | # return: json 426 | # {"name"=>"Ansible-Sample-TDD", "hosts"=>["192.168.0.103"], "user"=>"root", "roles"=>["nginx", "mariadb"]} 427 | def self.get_properties() 428 | playbook, inventoryfile = load_ansiblespec 429 | 430 | # load inventory file and playbook hosts mapping 431 | hosts = load_targets(inventoryfile) 432 | properties = load_playbook(playbook) 433 | properties.each do |var| 434 | var["hosts_childrens"] = hosts["hosts_childrens"] 435 | var["group"] = var["hosts"] 436 | if var["hosts"].to_s == "all" 437 | var["hosts"] = hosts.values.flatten 438 | elsif hosts.has_key?("#{var["hosts"]}") 439 | var["hosts"] = hosts["#{var["hosts"]}"] 440 | elsif var["hosts"].instance_of?(Array) 441 | tmp_host = var["hosts"] 442 | var["hosts"] = [] 443 | tmp_host.each do |v| 444 | if hosts.has_key?("#{v}") 445 | hosts["#{v}"].map {|target_server| target_server["hosts"] = v} 446 | var["hosts"].concat hosts["#{v}"] 447 | end 448 | end 449 | if var["hosts"].size == 0 450 | properties = properties.compact.reject{|e| e["hosts"].length == 0} 451 | #puts "#{var["name"]} roles no hosts matched for #{var["hosts"]}" 452 | end 453 | else 454 | puts "no hosts matched for #{var["hosts"]}" 455 | var["hosts"] = [] 456 | end 457 | end 458 | return properties 459 | end 460 | 461 | # param: none 462 | # return: vars_dirs_path 463 | def self.get_vars_dirs_path() 464 | f = '.ansiblespec' 465 | y = nil 466 | if File.exist?(f) 467 | y = YAML.load_file(f) 468 | end 469 | if ENV["VARS_DIRS_PATH"] 470 | vars_dirs_path = ENV["VARS_DIRS_PATH"] 471 | elsif y.is_a?(Array) && y[0]['vars_dirs_path'] 472 | vars_dirs_path = y[0]['vars_dirs_path'] 473 | else 474 | vars_dirs_path = '' 475 | end 476 | return vars_dirs_path 477 | end 478 | 479 | def self.find_group_vars_file(hosts_childrens, hosts) 480 | target_host = hosts_childrens.select { |key, value| 481 | value["children"].include?(hosts) 482 | } 483 | target_host.keys[0] 484 | end 485 | 486 | # query replace jinja2 templates with target values 487 | # param: hash (cf. result self.get_variables) 488 | # param: number of iterations if found_template 489 | # return: hash 490 | def self.resolve_variables(vars, max_level=100) 491 | vars_yaml = vars.to_yaml 492 | level = 0 493 | begin 494 | found_template = false 495 | level += 1 496 | 497 | # query replace jinja2 templates in yaml 498 | # replace in-place (gsub!) 499 | # use non-greedy regex (.*?) 500 | vars_yaml.gsub!(/{{.*?}}/) do |template| 501 | 502 | # grab target variable 503 | # ignore whitespaces (\s*) 504 | # use non-greedy regex (.*?) 505 | target = template.gsub(/{{\s*(.*?)\s*}}/, '\1') 506 | 507 | # lookup value of target variable 508 | value = vars[target] 509 | 510 | # return lookup value if it exists 511 | # or leave template alone 512 | if value.nil? 513 | template 514 | else 515 | found_template = true 516 | value 517 | end 518 | end 519 | end while found_template and level <= max_level 520 | return YAML.load(vars_yaml) 521 | end 522 | 523 | def self.get_variables(host, group_idx, hosts=nil) 524 | vars = {} 525 | p = self.get_properties.compact.reject{|e| e["hosts"].length == 0} 526 | 527 | # roles default 528 | p[group_idx]['roles'].each do |role| 529 | vars = load_vars_file(vars ,"roles/#{role}/defaults/main.yml") 530 | end 531 | 532 | # get parent directory of group_vars and host_vars directories 533 | vars_dirs_path = get_vars_dirs_path 534 | if vars_dirs_path != '' 535 | vars_dirs_path = "#{vars_dirs_path}/" 536 | end 537 | 538 | # all group 539 | vars = load_vars_file(vars ,"#{vars_dirs_path}group_vars/all", true) 540 | 541 | # each group vars 542 | if p[group_idx].has_key?('group') 543 | # get groups parent child relationships 544 | playbook, inventoryfile = load_ansiblespec 545 | groups_rels = load_targets(inventoryfile, return_type='groups_parent_child_relationships') 546 | # get parental lineage 547 | g = p[group_idx]['group'] 548 | groups_stack = Array.new 549 | groups_stack << g 550 | groups_rels.keys.each{|k| 551 | groups_stack << k if (groups_rels[k].include?(g)) 552 | } 553 | # get vars from parents groups then child group 554 | groups_parents_then_child = groups_stack.reverse.flatten 555 | groups_parents_then_child.each{|group| 556 | vars = load_vars_file(vars ,"#{vars_dirs_path}group_vars/#{group}", true) 557 | } 558 | end 559 | 560 | # each host vars 561 | vars = load_vars_file(vars ,"#{vars_dirs_path}host_vars/#{host}", true) 562 | 563 | # site vars 564 | if p[group_idx].has_key?('vars') 565 | vars = merge_variables(vars, p[group_idx]['vars']) 566 | end 567 | 568 | # roles vars 569 | p[group_idx]['roles'].each do |role| 570 | vars = load_vars_file(vars ,"roles/#{role}/vars/main.yml") 571 | end 572 | 573 | # multiple host and children dependencies group vars 574 | unless hosts.nil? || p[group_idx]["hosts_childrens"].nil? 575 | hosts_childrens = p[group_idx]["hosts_childrens"] 576 | next_find_target = hosts 577 | while(!next_find_target.nil? && hosts_childrens.size > 0) 578 | vars = load_vars_file(vars ,"#{vars_dirs_path}group_vars/#{next_find_target}", true) 579 | group_vars_file = find_group_vars_file(hosts_childrens,next_find_target) 580 | next_find_target = group_vars_file 581 | hosts_childrens.delete(group_vars_file) 582 | end 583 | end 584 | 585 | return resolve_variables(vars) 586 | 587 | end 588 | 589 | class AnsibleCfg 590 | def initialize 591 | @cfg = self.class.load_ansible_cfg 592 | end 593 | 594 | def roles_path 595 | rp = (self.get('defaults', 'roles_path') or '').split(':') 596 | rp << 'roles' # Roles is always searched 597 | end 598 | 599 | class << self 600 | def find_ansible_cfgs() 601 | files = [] 602 | ["/etc/ansible/ansible.cfg", 603 | File.expand_path("~/.ansible.cfg"), 604 | "./ansible.cfg", 605 | ENV["ANSIBLE_CFG"], 606 | ].each do |f| 607 | files << f if f and File.exists? f 608 | end 609 | end 610 | 611 | def load_ansible_cfg() 612 | cfg = IniFile.new 613 | self.find_ansible_cfgs.each do |file| 614 | cfg = cfg.merge(IniFile.new :filename => file) 615 | end 616 | cfg.to_h 617 | end 618 | end 619 | 620 | def get(section, key) 621 | s = @cfg[section] 622 | if s 623 | return s[key] 624 | else 625 | return nil 626 | end 627 | end 628 | end 629 | end 630 | -------------------------------------------------------------------------------- /lib/ansible_spec/vendor/MIT-LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2005-2014 David Heinemeier Hansson 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /lib/ansible_spec/vendor/hash.rb: -------------------------------------------------------------------------------- 1 | class Hash 2 | # Returns a new hash with +self+ and +other_hash+ merged recursively. 3 | # 4 | # h1 = { a: true, b: { c: [1, 2, 3] } } 5 | # h2 = { a: false, b: { x: [3, 4, 5] } } 6 | # 7 | # h1.deep_merge(h2) #=> { a: false, b: { c: [1, 2, 3], x: [3, 4, 5] } } 8 | # 9 | # Like with Hash#merge in the standard library, a block can be provided 10 | # to merge values: 11 | # 12 | # h1 = { a: 100, b: 200, c: { c1: 100 } } 13 | # h2 = { b: 250, c: { c1: 200 } } 14 | # h1.deep_merge(h2) { |key, this_val, other_val| this_val + other_val } 15 | # # => { a: 100, b: 450, c: { c1: 300 } } 16 | def deep_merge(other_hash, &block) 17 | dup.deep_merge!(other_hash, &block) 18 | end 19 | 20 | # Same as +deep_merge+, but modifies +self+. 21 | def deep_merge!(other_hash, &block) 22 | other_hash.each_pair do |current_key, other_value| 23 | this_value = self[current_key] 24 | 25 | self[current_key] = if this_value.is_a?(Hash) && other_value.is_a?(Hash) 26 | this_value.deep_merge(other_value, &block) 27 | else 28 | if block_given? && key?(current_key) 29 | block.call(current_key, this_value, other_value) 30 | else 31 | other_value 32 | end 33 | end 34 | end 35 | 36 | self 37 | end 38 | end 39 | -------------------------------------------------------------------------------- /lib/ansible_spec/version.rb: -------------------------------------------------------------------------------- 1 | module AnsibleSpec 2 | VERSION = "0.3.2" 3 | end 4 | -------------------------------------------------------------------------------- /lib/src/.ansiblespec: -------------------------------------------------------------------------------- 1 | --- 2 | - 3 | playbook: site.yml 4 | inventory: hosts 5 | -------------------------------------------------------------------------------- /lib/src/.rspec: -------------------------------------------------------------------------------- 1 | --color 2 | --format documentation 3 | -------------------------------------------------------------------------------- /lib/src/Rakefile: -------------------------------------------------------------------------------- 1 | require 'rake' 2 | require 'rspec/core/rake_task' 3 | require 'yaml' 4 | require 'ansible_spec' 5 | 6 | properties = AnsibleSpec.get_properties 7 | # {"name"=>"Ansible-Sample-TDD", "hosts"=>["192.168.0.103","192.168.0.103"], "user"=>"root", "roles"=>["nginx", "mariadb"]} 8 | # {"name"=>"Ansible-Sample-TDD", "hosts"=>[{"name" => "192.168.0.103:22","uri"=>"192.168.0.103","port"=>22, "private_key"=> "~/.ssh/id_rsa"}], "user"=>"root", "roles"=>["nginx", "mariadb"]} 9 | cfg = AnsibleSpec::AnsibleCfg.new 10 | 11 | desc "Run serverspec to all test" 12 | task :all => "serverspec:all" 13 | 14 | namespace :serverspec do 15 | properties = properties.compact.reject{|e| e["hosts"].length == 0} 16 | task :all => properties.map {|v| 'serverspec:' + v["name"] } 17 | properties.each_with_index.map do |property, index| 18 | property["hosts"].each do |host| 19 | desc "Run serverspec for #{property["name"]}" 20 | RSpec::Core::RakeTask.new(property["name"].to_sym) do |t| 21 | puts "Run serverspec for #{property["name"]} to #{host}" 22 | ENV['TARGET_HOSTS'] = host["hosts"] 23 | ENV['TARGET_HOST'] = host["uri"] 24 | ENV['TARGET_PORT'] = host["port"].to_s 25 | ENV['TARGET_GROUP_INDEX'] = index.to_s 26 | ENV['TARGET_PRIVATE_KEY'] = host["private_key"] 27 | unless host["user"].nil? 28 | ENV['TARGET_USER'] = host["user"] 29 | else 30 | ENV['TARGET_USER'] = property["user"] 31 | end 32 | ENV['TARGET_PASSWORD'] = host["pass"] 33 | ENV['TARGET_CONNECTION'] = host["connection"] 34 | 35 | roles = property["roles"] 36 | for role in property["roles"] 37 | for rolepath in cfg.roles_path 38 | deps = AnsibleSpec.load_dependencies(role, rolepath) 39 | if deps != [] 40 | roles += deps 41 | break 42 | end 43 | end 44 | end 45 | t.pattern = '{' + cfg.roles_path.join(',') + '}/{' + roles.join(',') + '}/spec/*_spec.rb' 46 | end 47 | end 48 | end 49 | end 50 | -------------------------------------------------------------------------------- /lib/src/spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require 'serverspec' 2 | require 'net/ssh' 3 | require 'ansible_spec' 4 | require 'winrm' 5 | 6 | # 7 | # Set ansible variables to serverspec property 8 | # 9 | host = ENV['TARGET_HOST'] 10 | hosts = ENV["TARGET_HOSTS"] 11 | 12 | group_idx = ENV['TARGET_GROUP_INDEX'].to_i 13 | vars = AnsibleSpec.get_variables(host, group_idx,hosts) 14 | ssh_config_file = AnsibleSpec.get_ssh_config_file 15 | set_property vars 16 | 17 | connection = ENV['TARGET_CONNECTION'] 18 | 19 | case connection 20 | when 'ssh' 21 | # 22 | # OS type: UN*X 23 | # 24 | set :backend, :ssh 25 | 26 | # Ansible use `BECOME`, But Serverspec use `SUDO`. 27 | if ENV['ASK_BECOME_PASSWORD'] 28 | begin 29 | require 'highline/import' 30 | rescue LoadError 31 | fail "highline is not available. Try installing it." 32 | end 33 | set :sudo_password, ask("Enter become password: ") { |q| q.echo = false } 34 | else 35 | set :sudo_password, ENV['BECOME_PASSWORD'] 36 | end 37 | 38 | options = Net::SSH::Config.for(host) 39 | 40 | options[:user] = ENV['TARGET_USER'] || options[:user] 41 | options[:port] = ENV['TARGET_PORT'] || options[:port] 42 | options[:keys] = ENV['TARGET_PRIVATE_KEY'] || options[:keys] 43 | 44 | if ssh_config_file 45 | from_config_file = Net::SSH::Config.for(host,files=[ssh_config_file]) 46 | options.merge!(from_config_file) 47 | end 48 | 49 | set :host, options[:host_name] || host 50 | set :ssh_options, options 51 | 52 | # Disable become (Serverspec use sudo) 53 | # set :disable_sudo, true 54 | 55 | 56 | # Set environment variables 57 | # set :env, :LANG => 'C', :LC_MESSAGES => 'C' 58 | 59 | # Set PATH 60 | # set :path, '/sbin:/usr/local/sbin:$PATH' 61 | when 'winrm' 62 | # 63 | # OS type: Windows 64 | # 65 | set :backend, :winrm 66 | set :os, :family => 'windows' 67 | 68 | user = ENV['TARGET_USER'] 69 | port = ENV['TARGET_PORT'] 70 | pass = ENV['TARGET_PASSWORD'] 71 | 72 | if user.nil? 73 | begin 74 | require 'highline/import' 75 | rescue LoadError 76 | fail "highline is not available. Try installing it." 77 | end 78 | user = ask("\nEnter #{host}'s login user: ") { |q| q.echo = true } 79 | end 80 | if pass.nil? 81 | begin 82 | require 'highline/import' 83 | rescue LoadError 84 | fail "highline is not available. Try installing it." 85 | end 86 | pass = ask("\nEnter #{user}@#{host}'s login password: ") { |q| q.echo = false } 87 | end 88 | 89 | endpoint = "http://#{host}:#{port}/wsman" 90 | 91 | winrm = ::WinRM::WinRMWebService.new(endpoint, :ssl, :user => user, :pass => pass, :basic_auth_only => true) 92 | winrm.set_timeout 300 # 5 minutes max timeout for any operation 93 | Specinfra.configuration.winrm = winrm 94 | 95 | when 'local' 96 | # 97 | # local connection 98 | # 99 | set :backend, :exec 100 | end 101 | -------------------------------------------------------------------------------- /spec/case/get_hash_behaviour/basic/.ansiblespec: -------------------------------------------------------------------------------- 1 | --- 2 | - 3 | playbook: site.yml 4 | inventory: hosts 5 | -------------------------------------------------------------------------------- /spec/case/get_hash_behaviour/basic/hosts: -------------------------------------------------------------------------------- 1 | [merge] 2 | 192.168.1.1 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /spec/case/get_hash_behaviour/basic/roles/test/defaults/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | merge_var: 3 | keep: "role" 4 | override: "role" 5 | -------------------------------------------------------------------------------- /spec/case/get_hash_behaviour/basic/site.yml: -------------------------------------------------------------------------------- 1 | - name: merge 2 | hosts: merge 3 | connection: local 4 | vars: 5 | merge_var: 6 | override: "site" 7 | add: "site" 8 | roles: 9 | - test -------------------------------------------------------------------------------- /spec/case/get_hash_behaviour/with_ansiblespec/.ansiblespec: -------------------------------------------------------------------------------- 1 | --- 2 | - 3 | playbook: site.yml 4 | inventory: hosts 5 | hash_behaviour: merge 6 | -------------------------------------------------------------------------------- /spec/case/get_hash_behaviour/with_ansiblespec/hosts: -------------------------------------------------------------------------------- 1 | [merge] 2 | 192.168.1.1 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /spec/case/get_hash_behaviour/with_ansiblespec/roles/test/defaults/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | merge_var: 3 | keep: "role" 4 | override: "role" 5 | -------------------------------------------------------------------------------- /spec/case/get_hash_behaviour/with_ansiblespec/site.yml: -------------------------------------------------------------------------------- 1 | - name: merge 2 | hosts: merge 3 | connection: local 4 | vars: 5 | merge_var: 6 | override: "site" 7 | add: "site" 8 | roles: 9 | - test -------------------------------------------------------------------------------- /spec/case/get_variable/deep_merge/.ansiblespec: -------------------------------------------------------------------------------- 1 | --- 2 | - 3 | playbook: site.yml 4 | inventory: hosts 5 | hash_behaviour: merge 6 | -------------------------------------------------------------------------------- /spec/case/get_variable/deep_merge/hosts: -------------------------------------------------------------------------------- 1 | [merge] 2 | 192.168.1.1 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /spec/case/get_variable/deep_merge/roles/test/defaults/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | merge_var: 3 | keep: "role" 4 | override: "role" 5 | -------------------------------------------------------------------------------- /spec/case/get_variable/deep_merge/site.yml: -------------------------------------------------------------------------------- 1 | - name: merge 2 | hosts: merge 3 | connection: local 4 | vars: 5 | merge_var: 6 | override: "site" 7 | add: "site" 8 | roles: 9 | - test -------------------------------------------------------------------------------- /spec/case/get_variable/empty_group/group_vars/group_a.yml: -------------------------------------------------------------------------------- 1 | --- 2 | group_a_var: hoge 3 | -------------------------------------------------------------------------------- /spec/case/get_variable/empty_group/group_vars/group_b.yml: -------------------------------------------------------------------------------- 1 | --- 2 | group_b_var: fuga 3 | -------------------------------------------------------------------------------- /spec/case/get_variable/empty_group/hosts: -------------------------------------------------------------------------------- 1 | [group_a] 2 | [group_b] 3 | localhost.localdomain ansible_ssh_private_key_file=~/.ssh/id_rsa 4 | -------------------------------------------------------------------------------- /spec/case/get_variable/empty_group/site.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: playbook_a 3 | hosts: group_a 4 | roles: 5 | - role_a 6 | - name: playbook_b 7 | hosts: group_b 8 | roles: 9 | - role_b 10 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_all/group_vars/all.yml: -------------------------------------------------------------------------------- 1 | --- 2 | role_var: "group all" 3 | site_var: "group all" 4 | host_var: "group all" 5 | group_var: "group all" 6 | group_all_var: "group all" 7 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_all/hosts: -------------------------------------------------------------------------------- 1 | [group1] 2 | 192.168.1.1 3 | 4 | [group2] 5 | 192.168.1.2 6 | 7 | [all] 8 | 192.168.1.1 9 | 192.168.1.2 10 | 11 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_all/roles/test/defaults/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | role_var: 'role_var in roles default' 3 | site_var: 'site_var in roles default' 4 | host_var: 'host_var in roles default' 5 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_all/site.yml: -------------------------------------------------------------------------------- 1 | - name: group1 2 | hosts: group1 3 | connection: local 4 | roles: 5 | - test 6 | - test/nested 7 | 8 | - name: group2 9 | hosts: group2 10 | connection: local 11 | roles: 12 | - test 13 | - test/nested 14 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_each_vars/group_vars/all.yml: -------------------------------------------------------------------------------- 1 | --- 2 | role_var: "group all" 3 | site_var: "group all" 4 | host_var: "group all" 5 | group_var: "group all" 6 | group_all_var: "group all" 7 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_each_vars/group_vars/group1.yml: -------------------------------------------------------------------------------- 1 | role_var: "group1" 2 | site_var: "group1" 3 | host_var: "group1" 4 | group_var: 'group1' 5 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_each_vars/hosts: -------------------------------------------------------------------------------- 1 | [group1] 2 | 192.168.1.1 3 | 4 | [group2] 5 | 192.168.1.2 6 | 7 | [all] 8 | 192.168.1.1 9 | 192.168.1.2 10 | 11 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_each_vars/site.yml: -------------------------------------------------------------------------------- 1 | - name: group1 2 | hosts: group1 3 | connection: local 4 | roles: 5 | - test 6 | - test/nested 7 | 8 | - name: group2 9 | hosts: group2 10 | connection: local 11 | roles: 12 | - test 13 | - test/nested 14 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_each_vars_parent_child/group_vars/all.yml: -------------------------------------------------------------------------------- 1 | --- 2 | role_var: "group all" 3 | site_var: "group all" 4 | host_var: "group all" 5 | group_var: "group all" 6 | group_all_var: "group all" 7 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_each_vars_parent_child/group_vars/group1/vars: -------------------------------------------------------------------------------- 1 | role_var: "group1" 2 | site_var: "group1" 3 | host_var: "group1" 4 | group_var: 'group1' 5 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_each_vars_parent_child/group_vars/group2/vars: -------------------------------------------------------------------------------- 1 | role_var: "group2" 2 | site_var: "group2" 3 | host_var: "group2" 4 | group_var: 'group2' 5 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_each_vars_parent_child/group_vars/parentgroup/vars: -------------------------------------------------------------------------------- 1 | role_var: "parentgroup" 2 | site_var: "parentgroup" 3 | host_var: "parentgroup" 4 | group_var: 'parentgroup' 5 | group_var_parent: 'parentgroup' 6 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_each_vars_parent_child/hosts: -------------------------------------------------------------------------------- 1 | [group1] 2 | 192.168.1.1 3 | 4 | [group2] 5 | 192.168.1.2 6 | 7 | [all] 8 | 192.168.1.1 9 | 192.168.1.2 10 | 11 | [parentgroup:children] 12 | group1 13 | group2 14 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_each_vars_parent_child/site.yml: -------------------------------------------------------------------------------- 1 | - name: group1 2 | hosts: group1 3 | connection: local 4 | roles: 5 | - test 6 | - test/nested 7 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_each_vars_parent_child/site1.yml: -------------------------------------------------------------------------------- 1 | - name: group1 2 | hosts: group1 3 | connection: local 4 | roles: 5 | - test 6 | - test/nested 7 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_each_vars_parent_child/site2.yml: -------------------------------------------------------------------------------- 1 | - name: group2 2 | hosts: group2 3 | connection: local 4 | roles: 5 | - test 6 | - test/nested 7 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_each_vars_parent_child/site3.yml: -------------------------------------------------------------------------------- 1 | - name: parentgroup 2 | hosts: parentgroup 3 | connection: local 4 | roles: 5 | - test 6 | - test/nested 7 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_host_playbook/group_vars/all.yml: -------------------------------------------------------------------------------- 1 | --- 2 | role_var: "group all" 3 | site_var: "group all" 4 | host_var: "group all" 5 | group_var: "group all" 6 | group_all_var: "group all" 7 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_host_playbook/group_vars/group1.yml: -------------------------------------------------------------------------------- 1 | role_var: "group1" 2 | site_var: "group1" 3 | host_var: "group1" 4 | group_var: 'group1' 5 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_host_playbook/host_vars/192.168.1.1.yml: -------------------------------------------------------------------------------- 1 | role_var: "host role_var" 2 | site_var: "host site_var" 3 | host_var: "host 192.168.1.1" 4 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_host_playbook/host_vars/192.168.1.2.yml: -------------------------------------------------------------------------------- 1 | role_var: "host role_var" 2 | site_var: "host site_var" 3 | host_var: "host 192.168.1.2" 4 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_host_playbook/hosts: -------------------------------------------------------------------------------- 1 | [group1] 2 | 192.168.1.1 3 | 4 | [group2] 5 | 192.168.1.2 6 | 7 | [all] 8 | 192.168.1.1 9 | 192.168.1.2 10 | 11 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_host_playbook/site.yml: -------------------------------------------------------------------------------- 1 | - name: group1 2 | hosts: group1 3 | connection: local 4 | vars: 5 | site_var: "site" 6 | role_var: "site" 7 | roles: 8 | - test 9 | - test/nested 10 | 11 | - name: group2 12 | hosts: group2 13 | connection: local 14 | vars: 15 | site_var: "site" 16 | role_var: "site" 17 | roles: 18 | - test 19 | - test/nested 20 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_host_playbook_role/group_vars/all.yml: -------------------------------------------------------------------------------- 1 | --- 2 | role_var: "group all" 3 | site_var: "group all" 4 | host_var: "group all" 5 | group_var: "group all" 6 | group_all_var: "group all" 7 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_host_playbook_role/group_vars/group1.yml: -------------------------------------------------------------------------------- 1 | role_var: "group1" 2 | site_var: "group1" 3 | host_var: "group1" 4 | group_var: 'group1' 5 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_host_playbook_role/host_vars/192.168.1.1.yml: -------------------------------------------------------------------------------- 1 | role_var: "host role_var" 2 | site_var: "host site_var" 3 | host_var: "host 192.168.1.1" 4 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_host_playbook_role/host_vars/192.168.1.2.yml: -------------------------------------------------------------------------------- 1 | role_var: "host role_var" 2 | site_var: "host site_var" 3 | host_var: "host 192.168.1.2" 4 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_host_playbook_role/hosts: -------------------------------------------------------------------------------- 1 | [group1] 2 | 192.168.1.1 3 | 4 | [group2] 5 | 192.168.1.2 6 | 7 | [all] 8 | 192.168.1.1 9 | 192.168.1.2 10 | 11 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_host_playbook_role/roles/test/vars/main.yml: -------------------------------------------------------------------------------- 1 | role_var: "role" 2 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_host_playbook_role/site.yml: -------------------------------------------------------------------------------- 1 | - name: group1 2 | hosts: group1 3 | connection: local 4 | vars: 5 | site_var: "site" 6 | role_var: "site" 7 | roles: 8 | - test 9 | - test/nested 10 | 11 | - name: group2 12 | hosts: group2 13 | connection: local 14 | vars: 15 | site_var: "site" 16 | role_var: "site" 17 | roles: 18 | - test 19 | - test/nested 20 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_dev/.ansiblespec: -------------------------------------------------------------------------------- 1 | --- 2 | - 3 | playbook: site.yml 4 | inventory: dynamic_inventory.sh 5 | # hash_behaviour: replace 6 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_dev/dynamic_inventory.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | cat << EOS 3 | { 4 | "database-servers": { 5 | "hosts": [ 6 | "10.0.0.4" 7 | ] 8 | }, 9 | "application-servers": { 10 | "hosts": [ 11 | "10.0.0.2", 12 | "10.0.0.3" 13 | ] 14 | }, 15 | "web-servers": { 16 | "hosts": [ 17 | "10.0.0.1" 18 | ] 19 | }, 20 | "develop-database-servers": { 21 | "children": [ 22 | "database-servers" 23 | ] 24 | }, 25 | "develop-application-servers": { 26 | "children": [ 27 | "application-servers" 28 | ] 29 | }, 30 | "develop-web-servers": { 31 | "children": [ 32 | "web-servers" 33 | ] 34 | }, 35 | "develop": { 36 | "children": [ 37 | "develop-web-servers", 38 | "develop-application-servers", 39 | "develop-database-servers" 40 | ] 41 | } 42 | } 43 | EOS 44 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_dev/group_vars/all: -------------------------------------------------------------------------------- 1 | all_variable: "all!!" 2 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_dev/group_vars/application-servers: -------------------------------------------------------------------------------- 1 | logs_conf_file: "application_logs.conf" 2 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_dev/group_vars/database-servers: -------------------------------------------------------------------------------- 1 | logs_conf_file: "database_logs.conf" 2 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_dev/group_vars/develop: -------------------------------------------------------------------------------- 1 | foo: "foofoo-develop" 2 | bar: ["bar1-develop", "bar2-develop", "bar3-develop"] 3 | env_name: "develop" 4 | 5 | my_common_libs_version: "1.2" 6 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_dev/group_vars/develop-application-servers: -------------------------------------------------------------------------------- 1 | java_version: "1.8.0_92" 2 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_dev/group_vars/develop-database-servers: -------------------------------------------------------------------------------- 1 | mysql_version: "5.7.12" 2 | mysql_dependencies_libs: ["foo-v2","bar-v2"] 3 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_dev/group_vars/develop-web-servers: -------------------------------------------------------------------------------- 1 | nginx_conf_file: "develop_nginx.conf" 2 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_dev/group_vars/production: -------------------------------------------------------------------------------- 1 | foo: "foofoo-production" 2 | bar: ["bar1-production", "bar2-production", "bar3-production"] 3 | env_name: "production" 4 | 5 | my_common_libs_version: "1.0" 6 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_dev/group_vars/production-application-servers: -------------------------------------------------------------------------------- 1 | java_version: "1.8.0_45" 2 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_dev/group_vars/production-database-servers: -------------------------------------------------------------------------------- 1 | mysql_version: "5.6.30" 2 | mysql_dependencies_libs: ["foo-v1","bar-v1"] 3 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_dev/group_vars/production-web-servers: -------------------------------------------------------------------------------- 1 | nginx_conf_file: "production_nginx.conf" 2 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_dev/group_vars/staging: -------------------------------------------------------------------------------- 1 | foo: "foofoo-staging" 2 | bar: ["bar1-staging", "bar2-staging", "bar3-staging"] 3 | env_name: "staging" 4 | 5 | my_common_libs_version: "1.1" 6 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_dev/group_vars/staging-application-servers: -------------------------------------------------------------------------------- 1 | java_version: "1.8.0_66" 2 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_dev/group_vars/staging-database-servers: -------------------------------------------------------------------------------- 1 | mysql_version: "5.6.30" 2 | mysql_dependencies_libs: ["foo-v1","bar-v1"] 3 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_dev/group_vars/staging-web-servers: -------------------------------------------------------------------------------- 1 | nginx_conf_file: "staging_nginx.conf" 2 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_dev/group_vars/web-servers: -------------------------------------------------------------------------------- 1 | logs_conf_file: "web_logs.conf" 2 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_dev/site.yml: -------------------------------------------------------------------------------- 1 | #- name: install-logcollect-libs 2 | # hosts: 3 | # - web-servers 4 | # - database-servers 5 | # - application-servers 6 | # connection: local 7 | # roles: 8 | # - test 9 | 10 | - name: install-nginx 11 | hosts: 12 | - web-servers 13 | connection: local 14 | roles: 15 | - test 16 | 17 | #- name: install-java 18 | # hosts: 19 | # - application-servers 20 | # connection: local 21 | # roles: 22 | # - test 23 | # 24 | #- name: install-mysql 25 | # hosts: 26 | # - database-servers 27 | # connection: local 28 | # roles: 29 | # - test 30 | 31 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_prd/.ansiblespec: -------------------------------------------------------------------------------- 1 | --- 2 | - 3 | playbook: site.yml 4 | inventory: dynamic_inventory.sh 5 | # hash_behaviour: replace 6 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_prd/dynamic_inventory.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | cat << EOS 3 | { 4 | "database-servers": { 5 | "hosts": [ 6 | "10.0.0.4" 7 | ] 8 | }, 9 | "application-servers": { 10 | "hosts": [ 11 | "10.0.0.2", 12 | "10.0.0.3" 13 | ] 14 | }, 15 | "web-servers": { 16 | "hosts": [ 17 | "10.0.0.1" 18 | ] 19 | }, 20 | "production-database-servers": { 21 | "children": [ 22 | "database-servers" 23 | ] 24 | }, 25 | "production-application-servers": { 26 | "children": [ 27 | "application-servers" 28 | ] 29 | }, 30 | "production-web-servers": { 31 | "children": [ 32 | "web-servers" 33 | ] 34 | }, 35 | "production": { 36 | "children": [ 37 | "production-web-servers", 38 | "production-application-servers", 39 | "production-database-servers" 40 | ] 41 | } 42 | } 43 | EOS 44 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_prd/group_vars/all: -------------------------------------------------------------------------------- 1 | all_variable: "all!!" 2 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_prd/group_vars/application-servers: -------------------------------------------------------------------------------- 1 | logs_conf_file: "application_logs.conf" 2 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_prd/group_vars/database-servers: -------------------------------------------------------------------------------- 1 | logs_conf_file: "database_logs.conf" 2 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_prd/group_vars/develop: -------------------------------------------------------------------------------- 1 | foo: "foofoo-develop" 2 | bar: ["bar1-develop", "bar2-develop", "bar3-develop"] 3 | env_name: "develop" 4 | 5 | my_common_libs_version: "1.2" 6 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_prd/group_vars/develop-application-servers: -------------------------------------------------------------------------------- 1 | java_version: "1.8.0_92" 2 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_prd/group_vars/develop-database-servers: -------------------------------------------------------------------------------- 1 | mysql_version: "5.7.12" 2 | mysql_dependencies_libs: ["foo-v2","bar-v2"] 3 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_prd/group_vars/develop-web-servers: -------------------------------------------------------------------------------- 1 | nginx_conf_file: "develop_nginx.conf" 2 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_prd/group_vars/production: -------------------------------------------------------------------------------- 1 | foo: "foofoo-production" 2 | bar: ["bar1-production", "bar2-production", "bar3-production"] 3 | env_name: "production" 4 | 5 | my_common_libs_version: "1.0" 6 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_prd/group_vars/production-application-servers: -------------------------------------------------------------------------------- 1 | java_version: "1.8.0_45" 2 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_prd/group_vars/production-database-servers: -------------------------------------------------------------------------------- 1 | mysql_version: "5.6.30" 2 | mysql_dependencies_libs: ["foo-v1","bar-v1"] 3 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_prd/group_vars/production-web-servers: -------------------------------------------------------------------------------- 1 | nginx_conf_file: "production_nginx.conf" 2 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_prd/group_vars/staging: -------------------------------------------------------------------------------- 1 | foo: "foofoo-staging" 2 | bar: ["bar1-staging", "bar2-staging", "bar3-staging"] 3 | env_name: "staging" 4 | 5 | my_common_libs_version: "1.1" 6 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_prd/group_vars/staging-application-servers: -------------------------------------------------------------------------------- 1 | java_version: "1.8.0_66" 2 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_prd/group_vars/staging-database-servers: -------------------------------------------------------------------------------- 1 | mysql_version: "5.6.30" 2 | mysql_dependencies_libs: ["foo-v1","bar-v1"] 3 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_prd/group_vars/staging-web-servers: -------------------------------------------------------------------------------- 1 | nginx_conf_file: "staging_nginx.conf" 2 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_prd/group_vars/web-servers: -------------------------------------------------------------------------------- 1 | logs_conf_file: "web_logs.conf" 2 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_prd/site.yml: -------------------------------------------------------------------------------- 1 | #- name: install-logcollect-libs 2 | # hosts: 3 | # - web-servers 4 | # - database-servers 5 | # - application-servers 6 | # connection: local 7 | # roles: 8 | # - test 9 | 10 | - name: install-nginx 11 | hosts: 12 | - web-servers 13 | connection: local 14 | roles: 15 | - test 16 | 17 | #- name: install-java 18 | # hosts: 19 | # - application-servers 20 | # connection: local 21 | # roles: 22 | # - test 23 | # 24 | #- name: install-mysql 25 | # hosts: 26 | # - database-servers 27 | # connection: local 28 | # roles: 29 | # - test 30 | 31 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_stg/.ansiblespec: -------------------------------------------------------------------------------- 1 | --- 2 | - 3 | playbook: site.yml 4 | inventory: dynamic_inventory.sh 5 | # hash_behaviour: replace 6 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_stg/dynamic_inventory.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | cat << EOS 3 | { 4 | "database-servers": { 5 | "hosts": [ 6 | "10.0.0.4" 7 | ] 8 | }, 9 | "application-servers": { 10 | "hosts": [ 11 | "10.0.0.2", 12 | "10.0.0.3" 13 | ] 14 | }, 15 | "web-servers": { 16 | "hosts": [ 17 | "10.0.0.1" 18 | ] 19 | }, 20 | "staging-database-servers": { 21 | "children": [ 22 | "database-servers" 23 | ] 24 | }, 25 | "staging-application-servers": { 26 | "children": [ 27 | "application-servers" 28 | ] 29 | }, 30 | "staging-web-servers": { 31 | "children": [ 32 | "web-servers" 33 | ] 34 | }, 35 | "staging": { 36 | "children": [ 37 | "staging-web-servers", 38 | "staging-application-servers", 39 | "staging-database-servers" 40 | ] 41 | } 42 | } 43 | EOS 44 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_stg/group_vars/all: -------------------------------------------------------------------------------- 1 | all_variable: "all!!" 2 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_stg/group_vars/application-servers: -------------------------------------------------------------------------------- 1 | logs_conf_file: "application_logs.conf" 2 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_stg/group_vars/database-servers: -------------------------------------------------------------------------------- 1 | logs_conf_file: "database_logs.conf" 2 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_stg/group_vars/develop: -------------------------------------------------------------------------------- 1 | foo: "foofoo-develop" 2 | bar: ["bar1-develop", "bar2-develop", "bar3-develop"] 3 | env_name: "develop" 4 | 5 | my_common_libs_version: "1.2" 6 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_stg/group_vars/develop-application-servers: -------------------------------------------------------------------------------- 1 | java_version: "1.8.0_92" 2 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_stg/group_vars/develop-database-servers: -------------------------------------------------------------------------------- 1 | mysql_version: "5.7.12" 2 | mysql_dependencies_libs: ["foo-v2","bar-v2"] 3 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_stg/group_vars/develop-web-servers: -------------------------------------------------------------------------------- 1 | nginx_conf_file: "develop_nginx.conf" 2 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_stg/group_vars/production: -------------------------------------------------------------------------------- 1 | foo: "foofoo-production" 2 | bar: ["bar1-production", "bar2-production", "bar3-production"] 3 | env_name: "production" 4 | 5 | my_common_libs_version: "1.0" 6 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_stg/group_vars/production-application-servers: -------------------------------------------------------------------------------- 1 | java_version: "1.8.0_45" 2 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_stg/group_vars/production-database-servers: -------------------------------------------------------------------------------- 1 | mysql_version: "5.6.30" 2 | mysql_dependencies_libs: ["foo-v1","bar-v1"] 3 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_stg/group_vars/production-web-servers: -------------------------------------------------------------------------------- 1 | nginx_conf_file: "production_nginx.conf" 2 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_stg/group_vars/staging: -------------------------------------------------------------------------------- 1 | foo: "foofoo-staging" 2 | bar: ["bar1-staging", "bar2-staging", "bar3-staging"] 3 | env_name: "staging" 4 | 5 | my_common_libs_version: "1.1" 6 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_stg/group_vars/staging-application-servers: -------------------------------------------------------------------------------- 1 | java_version: "1.8.0_66" 2 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_stg/group_vars/staging-database-servers: -------------------------------------------------------------------------------- 1 | mysql_version: "5.6.30" 2 | mysql_dependencies_libs: ["foo-v1","bar-v1"] 3 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_stg/group_vars/staging-web-servers: -------------------------------------------------------------------------------- 1 | nginx_conf_file: "staging_nginx.conf" 2 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_stg/group_vars/web-servers: -------------------------------------------------------------------------------- 1 | logs_conf_file: "web_logs.conf" 2 | -------------------------------------------------------------------------------- /spec/case/get_variable/group_vars_hosts_childrens_env_stg/site.yml: -------------------------------------------------------------------------------- 1 | #- name: install-logcollect-libs 2 | # hosts: 3 | # - web-servers 4 | # - database-servers 5 | # - application-servers 6 | # connection: local 7 | # roles: 8 | # - test 9 | 10 | - name: install-nginx 11 | hosts: 12 | - web-servers 13 | connection: local 14 | roles: 15 | - test 16 | 17 | #- name: install-java 18 | # hosts: 19 | # - application-servers 20 | # connection: local 21 | # roles: 22 | # - test 23 | # 24 | #- name: install-mysql 25 | # hosts: 26 | # - database-servers 27 | # connection: local 28 | # roles: 29 | # - test 30 | 31 | -------------------------------------------------------------------------------- /spec/case/get_variable/host_vars/host_vars/192.168.1.1.yml: -------------------------------------------------------------------------------- 1 | role_var: "host role_var" 2 | site_var: "host site_var" 3 | host_var: "host 192.168.1.1" 4 | -------------------------------------------------------------------------------- /spec/case/get_variable/host_vars/host_vars/192.168.1.2.yml: -------------------------------------------------------------------------------- 1 | role_var: "host role_var" 2 | site_var: "host site_var" 3 | host_var: "host 192.168.1.2" 4 | -------------------------------------------------------------------------------- /spec/case/get_variable/host_vars/hosts: -------------------------------------------------------------------------------- 1 | [group1] 2 | 192.168.1.1 3 | 4 | [group2] 5 | 192.168.1.2 6 | 7 | [all] 8 | 192.168.1.1 9 | 192.168.1.2 10 | 11 | -------------------------------------------------------------------------------- /spec/case/get_variable/host_vars/site.yml: -------------------------------------------------------------------------------- 1 | - name: group1 2 | hosts: group1 3 | connection: local 4 | roles: 5 | - test 6 | - test/nested 7 | 8 | - name: group2 9 | hosts: group2 10 | connection: local 11 | roles: 12 | - test 13 | - test/nested 14 | -------------------------------------------------------------------------------- /spec/case/get_variable/inventories_env_group_all/inventories/development/group_vars/all.yml: -------------------------------------------------------------------------------- 1 | --- 2 | role_var: "group all" 3 | site_var: "group all" 4 | host_var: "group all" 5 | group_var: "group all" 6 | group_all_var: "group all" 7 | -------------------------------------------------------------------------------- /spec/case/get_variable/inventories_env_group_all/inventories/development/hosts: -------------------------------------------------------------------------------- 1 | [group1] 2 | 192.168.1.1 3 | 4 | [group2] 5 | 192.168.1.2 6 | 7 | [all] 8 | 192.168.1.1 9 | 192.168.1.2 10 | 11 | -------------------------------------------------------------------------------- /spec/case/get_variable/inventories_env_group_all/inventories/production/group_vars/all.yml: -------------------------------------------------------------------------------- 1 | --- 2 | role_var: "group all" 3 | site_var: "group all" 4 | host_var: "group all" 5 | group_var: "group all" 6 | group_all_var: "group all" 7 | -------------------------------------------------------------------------------- /spec/case/get_variable/inventories_env_group_all/inventories/production/hosts: -------------------------------------------------------------------------------- 1 | [group1] 2 | 192.168.3.1 3 | 4 | [group2] 5 | 192.168.3.2 6 | 7 | [all] 8 | 192.168.3.1 9 | 192.168.3.2 10 | -------------------------------------------------------------------------------- /spec/case/get_variable/inventories_env_group_all/inventories/staging/group_vars/all.yml: -------------------------------------------------------------------------------- 1 | --- 2 | role_var: "group all" 3 | site_var: "group all" 4 | host_var: "group all" 5 | group_var: "group all" 6 | group_all_var: "group all" 7 | -------------------------------------------------------------------------------- /spec/case/get_variable/inventories_env_group_all/inventories/staging/hosts: -------------------------------------------------------------------------------- 1 | [group1] 2 | 192.168.2.1 3 | 4 | [group2] 5 | 192.168.2.2 6 | 7 | [all] 8 | 192.168.2.1 9 | 192.168.2.2 10 | -------------------------------------------------------------------------------- /spec/case/get_variable/inventories_env_group_all/roles/test/defaults/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | role_var: 'role_var in roles default' 3 | site_var: 'site_var in roles default' 4 | host_var: 'host_var in roles default' 5 | -------------------------------------------------------------------------------- /spec/case/get_variable/inventories_env_group_all/site.yml: -------------------------------------------------------------------------------- 1 | - name: group1 2 | hosts: group1 3 | connection: local 4 | roles: 5 | - test 6 | - test/nested 7 | 8 | - name: group2 9 | hosts: group2 10 | connection: local 11 | roles: 12 | - test 13 | - test/nested 14 | -------------------------------------------------------------------------------- /spec/case/get_variable/inventories_env_group_each_vars/inventories/development/group_vars/all.yml: -------------------------------------------------------------------------------- 1 | --- 2 | role_var: "group all" 3 | site_var: "group all" 4 | host_var: "group all" 5 | group_var: "group all" 6 | group_all_var: "group all" 7 | -------------------------------------------------------------------------------- /spec/case/get_variable/inventories_env_group_each_vars/inventories/development/group_vars/group1.yml: -------------------------------------------------------------------------------- 1 | role_var: 'group1' 2 | site_var: 'group1' 3 | host_var: 'group1' 4 | group_var: 'group1' 5 | -------------------------------------------------------------------------------- /spec/case/get_variable/inventories_env_group_each_vars/inventories/development/hosts: -------------------------------------------------------------------------------- 1 | [group1] 2 | 192.168.1.1 3 | 4 | [group2] 5 | 192.168.1.2 6 | 7 | [all] 8 | 192.168.1.1 9 | 192.168.1.2 10 | 11 | -------------------------------------------------------------------------------- /spec/case/get_variable/inventories_env_group_each_vars/inventories/production/group_vars/all.yml: -------------------------------------------------------------------------------- 1 | --- 2 | role_var: "group all" 3 | site_var: "group all" 4 | host_var: "group all" 5 | group_var: "group all" 6 | group_all_var: "group all" 7 | -------------------------------------------------------------------------------- /spec/case/get_variable/inventories_env_group_each_vars/inventories/production/group_vars/group1.yml: -------------------------------------------------------------------------------- 1 | role_var: 'group1' 2 | site_var: 'group1' 3 | host_var: 'group1' 4 | group_var: 'group1' 5 | -------------------------------------------------------------------------------- /spec/case/get_variable/inventories_env_group_each_vars/inventories/production/hosts: -------------------------------------------------------------------------------- 1 | [group1] 2 | 192.168.3.1 3 | 4 | [group2] 5 | 192.168.3.2 6 | 7 | [all] 8 | 192.168.3.1 9 | 192.168.3.2 10 | -------------------------------------------------------------------------------- /spec/case/get_variable/inventories_env_group_each_vars/inventories/staging/group_vars/all.yml: -------------------------------------------------------------------------------- 1 | --- 2 | role_var: "group all" 3 | site_var: "group all" 4 | host_var: "group all" 5 | group_var: "group all" 6 | group_all_var: "group all" 7 | -------------------------------------------------------------------------------- /spec/case/get_variable/inventories_env_group_each_vars/inventories/staging/group_vars/group1.yml: -------------------------------------------------------------------------------- 1 | role_var: 'group1' 2 | site_var: 'group1' 3 | host_var: 'group1' 4 | group_var: 'group1' 5 | -------------------------------------------------------------------------------- /spec/case/get_variable/inventories_env_group_each_vars/inventories/staging/hosts: -------------------------------------------------------------------------------- 1 | [group1] 2 | 192.168.2.1 3 | 4 | [group2] 5 | 192.168.2.2 6 | 7 | [all] 8 | 192.168.2.1 9 | 192.168.2.2 10 | -------------------------------------------------------------------------------- /spec/case/get_variable/inventories_env_group_each_vars/site.yml: -------------------------------------------------------------------------------- 1 | - name: group1 2 | hosts: group1 3 | connection: local 4 | roles: 5 | - test 6 | - test/nested 7 | 8 | - name: group2 9 | hosts: group2 10 | connection: local 11 | roles: 12 | - test 13 | - test/nested 14 | -------------------------------------------------------------------------------- /spec/case/get_variable/inventories_env_host_vars/inventories/development/host_vars/192.168.1.1.yml: -------------------------------------------------------------------------------- 1 | role_var: "host role_var" 2 | site_var: "host site_var" 3 | host_var: "host 192.168.1.1" 4 | -------------------------------------------------------------------------------- /spec/case/get_variable/inventories_env_host_vars/inventories/development/host_vars/192.168.1.2.yml: -------------------------------------------------------------------------------- 1 | role_var: "host role_var" 2 | site_var: "host site_var" 3 | host_var: "host 192.168.1.2" 4 | -------------------------------------------------------------------------------- /spec/case/get_variable/inventories_env_host_vars/inventories/development/hosts: -------------------------------------------------------------------------------- 1 | [group1] 2 | 192.168.1.1 3 | 4 | [group2] 5 | 192.168.1.2 6 | 7 | [all] 8 | 192.168.1.1 9 | 192.168.1.2 10 | 11 | -------------------------------------------------------------------------------- /spec/case/get_variable/inventories_env_host_vars/inventories/production/host_vars/192.168.3.1.yml: -------------------------------------------------------------------------------- 1 | role_var: "host role_var" 2 | site_var: "host site_var" 3 | host_var: "host 192.168.3.1" 4 | -------------------------------------------------------------------------------- /spec/case/get_variable/inventories_env_host_vars/inventories/production/host_vars/192.168.3.2.yml: -------------------------------------------------------------------------------- 1 | role_var: "host role_var" 2 | site_var: "host site_var" 3 | host_var: "host 192.168.3.2" 4 | -------------------------------------------------------------------------------- /spec/case/get_variable/inventories_env_host_vars/inventories/production/hosts: -------------------------------------------------------------------------------- 1 | [group1] 2 | 192.168.3.1 3 | 4 | [group2] 5 | 192.168.3.2 6 | 7 | [all] 8 | 192.168.3.1 9 | 192.168.3.2 10 | -------------------------------------------------------------------------------- /spec/case/get_variable/inventories_env_host_vars/inventories/staging/host_vars/192.168.2.1.yml: -------------------------------------------------------------------------------- 1 | role_var: "host role_var" 2 | site_var: "host site_var" 3 | host_var: "host 192.168.2.1" 4 | -------------------------------------------------------------------------------- /spec/case/get_variable/inventories_env_host_vars/inventories/staging/host_vars/192.168.2.2.yml: -------------------------------------------------------------------------------- 1 | role_var: "host role_var" 2 | site_var: "host site_var" 3 | host_var: "host 192.168.2.2" 4 | -------------------------------------------------------------------------------- /spec/case/get_variable/inventories_env_host_vars/inventories/staging/hosts: -------------------------------------------------------------------------------- 1 | [group1] 2 | 192.168.2.1 3 | 4 | [group2] 5 | 192.168.2.2 6 | 7 | [all] 8 | 192.168.2.1 9 | 192.168.2.2 10 | -------------------------------------------------------------------------------- /spec/case/get_variable/inventories_env_host_vars/site.yml: -------------------------------------------------------------------------------- 1 | - name: group1 2 | hosts: group1 3 | connection: local 4 | roles: 5 | - test 6 | - test/nested 7 | 8 | - name: group2 9 | hosts: group2 10 | connection: local 11 | roles: 12 | - test 13 | - test/nested 14 | -------------------------------------------------------------------------------- /spec/case/get_variable/playbook_only/hosts: -------------------------------------------------------------------------------- 1 | [group1] 2 | 192.168.1.1 3 | 4 | [group2] 5 | 192.168.1.2 6 | 7 | [all] 8 | 192.168.1.1 9 | 192.168.1.2 10 | 11 | -------------------------------------------------------------------------------- /spec/case/get_variable/playbook_only/site.yml: -------------------------------------------------------------------------------- 1 | - name: group1 2 | hosts: group1 3 | connection: local 4 | vars: 5 | site_var: "site" 6 | role_var: "site" 7 | roles: 8 | - test 9 | - test/nested 10 | 11 | - name: group2 12 | hosts: group2 13 | connection: local 14 | vars: 15 | site_var: "site" 16 | role_var: "site" 17 | roles: 18 | - test 19 | - test/nested 20 | -------------------------------------------------------------------------------- /spec/case/get_variable/resolve_variables/group_vars/all.yml: -------------------------------------------------------------------------------- 1 | --- 2 | var_nested_one_1: 'val_nested_one' 3 | var_nested_one_2: "{{ var_nested_one_1 }}" 4 | 5 | var_nested_two_1: 'val_nested_two' 6 | var_nested_two_2: "{{ var_nested_two_1 }}" 7 | var_nested_two_3: "{{ var_nested_two_2 }}" 8 | 9 | var_nested_hash_1: 'val_hash' 10 | var_nested_hash_2: 11 | key: "{{ var_nested_hash_1 }}" 12 | 13 | var_nested_array_1: 'val_array' 14 | var_nested_array_2: 15 | - "{{ var_nested_array_1 }}" 16 | 17 | var_nested_array_hash_1: 'val_array_hash' 18 | var_nested_array_hash_2: 19 | - key: "{{ var_nested_array_hash_1 }}" 20 | 21 | var_nested_whitespace_1: val_nested_whitespace 22 | var_nested_whitespace_2: "{{var_nested_whitespace_1 }}" 23 | 24 | var_missingtarget_2: "{{ var_missingtarget_1 }}" 25 | -------------------------------------------------------------------------------- /spec/case/get_variable/resolve_variables/hosts: -------------------------------------------------------------------------------- 1 | [resolve] 2 | 192.168.1.1 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /spec/case/get_variable/resolve_variables/site.yml: -------------------------------------------------------------------------------- 1 | - name: resolve 2 | hosts: resolve 3 | connection: local 4 | -------------------------------------------------------------------------------- /spec/case/get_variable/roles_default/hosts: -------------------------------------------------------------------------------- 1 | [group1] 2 | 192.168.1.1 3 | 4 | [group2] 5 | 192.168.1.2 6 | 7 | [all] 8 | 192.168.1.1 9 | 192.168.1.2 10 | 11 | -------------------------------------------------------------------------------- /spec/case/get_variable/roles_default/roles/test/defaults/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | role_var: 'role_var in roles default' 3 | site_var: 'site_var in roles default' 4 | host_var: 'host_var in roles default' 5 | -------------------------------------------------------------------------------- /spec/case/get_variable/roles_default/site.yml: -------------------------------------------------------------------------------- 1 | - name: group1 2 | hosts: group1 3 | connection: local 4 | roles: 5 | - test 6 | - test/nested 7 | 8 | - name: group2 9 | hosts: group2 10 | connection: local 11 | roles: 12 | - test 13 | - test/nested 14 | -------------------------------------------------------------------------------- /spec/case/load_vars_file/group_all/.ansiblespec: -------------------------------------------------------------------------------- 1 | --- 2 | - 3 | playbook: site.yml 4 | inventory: hosts 5 | hash_behaviour: merge 6 | -------------------------------------------------------------------------------- /spec/case/load_vars_file/group_all/group_vars/all: -------------------------------------------------------------------------------- 1 | --- 2 | role_var: "without .yml extension" 3 | -------------------------------------------------------------------------------- /spec/case/load_vars_file/group_all/group_vars/all.yml: -------------------------------------------------------------------------------- 1 | --- 2 | role_var: "group all" 3 | site_var: "group all" 4 | host_var: "group all" 5 | group_var: "group all" 6 | group_all_var: "group all" 7 | -------------------------------------------------------------------------------- /spec/case/load_vars_file/group_all_deep_merge/.ansiblespec: -------------------------------------------------------------------------------- 1 | --- 2 | - 3 | playbook: site.yml 4 | inventory: hosts 5 | hash_behaviour: merge 6 | -------------------------------------------------------------------------------- /spec/case/load_vars_file/group_all_deep_merge/group_vars/all.yml: -------------------------------------------------------------------------------- 1 | --- 2 | role_var: "without .yml extension" 3 | -------------------------------------------------------------------------------- /spec/case/load_vars_file/vars_dir/group_vars/all/file1: -------------------------------------------------------------------------------- 1 | var1: "val1" 2 | -------------------------------------------------------------------------------- /spec/case/load_vars_file/vars_dir/group_vars/all/file2: -------------------------------------------------------------------------------- 1 | var2: "val2" 2 | -------------------------------------------------------------------------------- /spec/case/load_vars_file/vault_dir/ansible.cfg: -------------------------------------------------------------------------------- 1 | [defaults] 2 | vault_password_file = vault-password-file 3 | -------------------------------------------------------------------------------- /spec/case/load_vars_file/vault_dir/group_vars/all/vault_file: -------------------------------------------------------------------------------- 1 | $ANSIBLE_VAULT;1.1;AES256 2 | 33623930383865383331633337653461646233633934353935376363306265636331363565306532 3 | 3935313131623364363162616335333530376134383137350a376231363430353963646535313335 4 | 36383533346433306666623162663335353035333931366162353131383539643239646637333561 5 | 3963386635663534310a323134643666343962303733616263613535623965366337393666623832 6 | 3032 7 | -------------------------------------------------------------------------------- /spec/case/load_vars_file/vault_dir/vault-password-file: -------------------------------------------------------------------------------- 1 | supersecretpassword -------------------------------------------------------------------------------- /spec/commands_spec.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | require 'ansible_spec' 3 | require 'diff/lcs' 4 | 5 | created_file = [ 6 | "spec/spec_helper.rb", 7 | "Rakefile", 8 | ".ansiblespec" 9 | ] 10 | created_dir = [ 11 | "spec", 12 | ] 13 | test_dir = "tmp" 14 | 15 | describe "モジュールの実行" do 16 | # テスト実行前 17 | before do 18 | $stdout = File.open("/dev/null", "w") #テスト実行中は標準出力は/dev/nullにする。 19 | FileUtils.mkdir_p(test_dir) unless FileTest.exist?(test_dir) 20 | Dir.chdir(test_dir) #tmp/に移動 21 | AnsibleSpec.main 22 | end 23 | 24 | # テスト実行後 25 | after do 26 | created_file.each{|f| File.delete(f) } 27 | created_dir.each{|d| Dir.delete(d) } 28 | Dir.chdir("../") 29 | FileUtils.remove_entry_secure(test_dir) 30 | #Dir.delete(test_dir) 31 | $stdout =STDOUT # テスト実行後は元に戻す 32 | end 33 | 34 | it "/tmpにディレクトリが作成されること" do 35 | created_dir.each{|d| 36 | expect(File.directory?(d)).to be_truthy 37 | } 38 | end 39 | 40 | it "/tmpにファイルが作成されること" do 41 | created_file.each{|f| 42 | expect(FileTest.exist?(f)).to be_truthy 43 | } 44 | end 45 | 46 | it "ファイルがオリジナルと一致すること" do 47 | created_file.each{|f| 48 | expect(no_diff("../lib/src/"+f,f)).to be_truthy 49 | } 50 | end 51 | 52 | end 53 | 54 | # check diff 55 | # if exists diff, return false 56 | # if not exist diff, return true 57 | def no_diff(src_file,dst_file) 58 | src = File.open(src_file).read 59 | dst = File.open(dst_file).read 60 | if Diff::LCS.diff(src,dst).count == 0 61 | return true 62 | end 63 | return false 64 | end 65 | -------------------------------------------------------------------------------- /spec/dependency_spec.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | require 'ansible_spec' 3 | 4 | 5 | describe "load_dependencies" do 6 | tmp_hosts = 'hosts' 7 | tmp_ansiblespec = '.ansiblespec' 8 | tmp_playbook = 'site.yml' 9 | tmp_webapp_meta = 'roles/webapp/meta/main.yml' 10 | tmp_dep1_meta = 'roles/dep1/meta/main.yml' 11 | 12 | spec_content = <<'EOF' 13 | --- 14 | - 15 | playbook: site.yml 16 | inventory: hosts 17 | EOF 18 | 19 | hosts_content = <<'EOF' 20 | [normal] 21 | 192.168.0.1 22 | 192.168.0.2 23 | 192.168.0.3 24 | EOF 25 | 26 | playbook_content = <<'EOF' 27 | - name: Ansible-Sample-TDD 28 | hosts: normal 29 | user: root 30 | roles: 31 | - webapp 32 | EOF 33 | 34 | webapp_meta_content = <<'EOF' 35 | --- 36 | dependencies: 37 | - { role: dep1 } 38 | - dep2 39 | EOF 40 | 41 | dep1_meta_content = <<'EOF' 42 | --- 43 | dependencies: 44 | - { role: dep3 } 45 | EOF 46 | 47 | before do 48 | create_file(tmp_hosts, hosts_content) 49 | create_file(tmp_ansiblespec, spec_content) 50 | create_file(tmp_playbook, playbook_content) 51 | create_file(tmp_webapp_meta, webapp_meta_content) 52 | create_file(tmp_dep1_meta, dep1_meta_content) 53 | @deps = AnsibleSpec.load_dependencies("webapp") 54 | end 55 | 56 | it 'should correctly resolve nested dependencies' do 57 | expect(@deps).to eq ["dep1", "dep2", "dep3"] 58 | end 59 | 60 | after do 61 | File.delete(tmp_hosts) 62 | File.delete(tmp_ansiblespec) 63 | File.delete(tmp_playbook) 64 | File.delete(tmp_webapp_meta) 65 | File.delete(tmp_dep1_meta) 66 | FileUtils.rm_r(tmp_webapp_meta.split("/")[0]) 67 | end 68 | end 69 | 70 | describe "load_dependencies (nil dependencies)" do 71 | tmp_webapp_meta = 'roles/webapp/meta/main.yml' 72 | 73 | webapp_meta_content = <<'EOF' 74 | --- 75 | dependencies: 76 | EOF 77 | 78 | before do 79 | create_file(tmp_webapp_meta, webapp_meta_content) 80 | @deps = AnsibleSpec.load_dependencies("webapp") 81 | end 82 | 83 | it 'should correctly resolve nested dependencies' do 84 | expect(@deps).to eq [] 85 | end 86 | 87 | after do 88 | File.delete(tmp_webapp_meta) 89 | FileUtils.rm_r(tmp_webapp_meta.split("/")[0]) 90 | end 91 | end 92 | 93 | describe "load_dependencies from alternative dir" do 94 | tmp_webapp_meta = 'more_roles/webapp/meta/main.yml' 95 | 96 | webapp_meta_content = <<'EOF' 97 | --- 98 | dependencies: 99 | - foo 100 | EOF 101 | 102 | before do 103 | create_file(tmp_webapp_meta, webapp_meta_content) 104 | @deps = AnsibleSpec.load_dependencies("webapp", "more_roles") 105 | end 106 | 107 | it 'should correctly resolve deps in additional role dirs' do 108 | expect(@deps).to eq ["foo"] 109 | end 110 | 111 | after do 112 | File.delete(tmp_webapp_meta) 113 | FileUtils.rm_r(tmp_webapp_meta.split("/")[0]) 114 | end 115 | end 116 | -------------------------------------------------------------------------------- /spec/dynamic_inventory_hosts_childrens_spec.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | require 'ansible_spec' 3 | 4 | describe "Dynamic InventoryでChildren関係でhostsを指定しload_targetsの実行" do 5 | context '正常系:develop-env DynamicInventory:8 Group, 2 hosts' do 6 | tmp_hosts = 'hosts' 7 | before do 8 | content_h = <<'EOF' 9 | 10 | #!/bin/bash 11 | #echo '{"database-servers": {"hosts": ["host1.example.com"],"vars":{"a": true}}}' 12 | cat << EOS 13 | { 14 | "database-servers": { 15 | "hosts": [ 16 | "10.0.0.4" 17 | ] 18 | }, 19 | "application-servers": { 20 | "hosts": [] 21 | }, 22 | "web-servers": { 23 | "hosts": [ 24 | "10.0.0.1" 25 | ] 26 | }, 27 | "develop-database-servers": { 28 | "children": [ 29 | "database-servers" 30 | ] 31 | }, 32 | "develop-application-servers": { 33 | "children": [ 34 | "application-servers" 35 | ] 36 | }, 37 | "develop-web-servers": { 38 | "children": [ 39 | "web-servers" 40 | ] 41 | }, 42 | "develop": { 43 | "children": [ 44 | "develop-web-servers", 45 | "develop-application-servers", 46 | "develop-database-servers" 47 | ] 48 | } 49 | } 50 | EOS 51 | EOF 52 | create_file(tmp_hosts,content_h) 53 | File.chmod(0755,tmp_hosts) 54 | @res = AnsibleSpec.load_targets(tmp_hosts) 55 | end 56 | 57 | it 'res is hash' do 58 | expect(@res.instance_of?(Hash)).to be_truthy 59 | end 60 | 61 | it 'exist 8 group' do 62 | expect(@res.length).to eq 8 63 | end 64 | 65 | it 'exist group' do 66 | expect(@res.key?('hosts_childrens')).to be_truthy 67 | expect(@res.key?('database-servers')).to be_truthy 68 | expect(@res.key?('web-servers')).to be_truthy 69 | expect(@res.key?('application-servers')).to be_truthy 70 | expect(@res.key?('develop-database-servers')).to be_truthy 71 | expect(@res.key?('develop-application-servers')).to be_truthy 72 | expect(@res.key?('develop-web-servers')).to be_truthy 73 | expect(@res.key?('develop')).to be_truthy 74 | end 75 | 76 | it 'database-servers 10.0.0.4' do 77 | obj = @res['database-servers'][0] 78 | expect(obj.instance_of?(Hash)).to be_truthy 79 | expect(obj).to include({'uri' => '10.0.0.4', 80 | 'port' => 22}) 81 | end 82 | 83 | it 'application-servers empty' do 84 | obj = @res['application-servers'][0] 85 | expect(obj).to be_nil 86 | end 87 | 88 | it 'web-servers 10.0.0.1' do 89 | obj = @res['web-servers'][0] 90 | expect(obj.instance_of?(Hash)).to be_truthy 91 | expect(obj).to include({'uri' => '10.0.0.1', 92 | 'port' => 22}) 93 | end 94 | 95 | after do 96 | File.delete(tmp_hosts) 97 | end 98 | end 99 | 100 | context '正常系:staging-env DynamicInventory:8 Group, 3 hosts' do 101 | tmp_hosts = 'hosts' 102 | before do 103 | content_h = <<'EOF' 104 | #!/bin/bash 105 | #echo '{"database-servers": {"hosts": ["host1.example.com", "host2.example.com"],"vars":{"a": true}}}' 106 | cat << EOS 107 | { 108 | "database-servers": { 109 | "hosts": [ 110 | "10.1.0.4" 111 | ] 112 | }, 113 | "application-servers": { 114 | "hosts": [ 115 | "10.1.0.2" 116 | ] 117 | }, 118 | "web-servers": { 119 | "hosts": [ 120 | "10.1.0.1" 121 | ] 122 | }, 123 | "staging-database-servers": { 124 | "children": [ 125 | "database-servers" 126 | ] 127 | }, 128 | "staging-application-servers": { 129 | "children": [ 130 | "application-servers" 131 | ] 132 | }, 133 | "staging-web-servers": { 134 | "children": [ 135 | "web-servers" 136 | ] 137 | }, 138 | "staging": { 139 | "children": [ 140 | "staging-web-servers", 141 | "staging-application-servers", 142 | "staging-database-servers" 143 | ] 144 | } 145 | } 146 | EOS 147 | EOF 148 | create_file(tmp_hosts,content_h) 149 | File.chmod(0755,tmp_hosts) 150 | @res = AnsibleSpec.load_targets(tmp_hosts) 151 | end 152 | 153 | it 'res is hash' do 154 | expect(@res.instance_of?(Hash)).to be_truthy 155 | end 156 | 157 | it 'exist 8 group' do 158 | expect(@res.length).to eq 8 159 | end 160 | 161 | it 'exist group' do 162 | expect(@res.key?('hosts_childrens')).to be_truthy 163 | expect(@res.key?('database-servers')).to be_truthy 164 | expect(@res.key?('web-servers')).to be_truthy 165 | expect(@res.key?('application-servers')).to be_truthy 166 | expect(@res.key?('staging-database-servers')).to be_truthy 167 | expect(@res.key?('staging-application-servers')).to be_truthy 168 | expect(@res.key?('staging-web-servers')).to be_truthy 169 | expect(@res.key?('staging')).to be_truthy 170 | end 171 | 172 | it 'database-servers 10.1.0.4' do 173 | obj = @res['database-servers'][0] 174 | expect(obj.instance_of?(Hash)).to be_truthy 175 | expect(obj).to include({'uri' => '10.1.0.4', 176 | 'port' => 22}) 177 | end 178 | 179 | it 'application-servers 10.1.0.1' do 180 | obj = @res['application-servers'][0] 181 | expect(obj.instance_of?(Hash)).to be_truthy 182 | expect(obj).to include({'uri' => '10.1.0.2', 183 | 'port' => 22}) 184 | end 185 | 186 | 187 | it 'web-servers 10.1.0.1' do 188 | obj = @res['web-servers'][0] 189 | expect(obj.instance_of?(Hash)).to be_truthy 190 | expect(obj).to include({'uri' => '10.1.0.1', 191 | 'port' => 22}) 192 | end 193 | 194 | after do 195 | File.delete(tmp_hosts) 196 | end 197 | end 198 | context '正常系:production-env DynamicInventory:8 Group, 4 hosts. but no vars' do 199 | tmp_hosts = 'hosts' 200 | before do 201 | content_h = <<'EOF' 202 | #!/bin/bash 203 | #echo '{"web-servers": [ "host2.example.com", "host3.example.com" ]}' 204 | cat << EOS 205 | { 206 | "database-servers": { 207 | "hosts": [ 208 | "10.2.0.4" 209 | ] 210 | }, 211 | "application-servers": { 212 | "hosts": [ 213 | "10.2.0.2", 214 | "10.2.0.3" 215 | ] 216 | }, 217 | "web-servers": { 218 | "hosts": [ 219 | "10.2.0.1" 220 | ] 221 | }, 222 | "production-database-servers": { 223 | "children": [ 224 | "database-servers" 225 | ] 226 | }, 227 | "production-application-servers": { 228 | "children": [ 229 | "application-servers" 230 | ] 231 | }, 232 | "production-web-servers": { 233 | "children": [ 234 | "web-servers" 235 | ] 236 | }, 237 | "production": { 238 | "children": [ 239 | "production-web-servers", 240 | "production-application-servers", 241 | "production-database-servers" 242 | ] 243 | } 244 | } 245 | EOS 246 | EOF 247 | create_file(tmp_hosts,content_h) 248 | File.chmod(0755,tmp_hosts) 249 | @res = AnsibleSpec.load_targets(tmp_hosts) 250 | end 251 | 252 | it 'res is hash' do 253 | expect(@res.instance_of?(Hash)).to be_truthy 254 | end 255 | 256 | it 'exist 8 group' do 257 | expect(@res.length).to eq 8 258 | end 259 | 260 | it 'exist group' do 261 | expect(@res.key?('hosts_childrens')).to be_truthy 262 | expect(@res.key?('database-servers')).to be_truthy 263 | expect(@res.key?('web-servers')).to be_truthy 264 | expect(@res.key?('application-servers')).to be_truthy 265 | expect(@res.key?('production-database-servers')).to be_truthy 266 | expect(@res.key?('production-application-servers')).to be_truthy 267 | expect(@res.key?('production-web-servers')).to be_truthy 268 | expect(@res.key?('production')).to be_truthy 269 | end 270 | 271 | it 'database-servers 10.2.0.4' do 272 | obj = @res['database-servers'][0] 273 | expect(obj.instance_of?(Hash)).to be_truthy 274 | expect(obj).to include({'uri' => '10.2.0.4', 275 | 'port' => 22}) 276 | end 277 | 278 | it 'application-servers 10.2.0.2' do 279 | obj = @res['application-servers'][0] 280 | expect(obj.instance_of?(Hash)).to be_truthy 281 | expect(obj).to include({'uri' => '10.2.0.2', 282 | 'port' => 22}) 283 | end 284 | 285 | it 'aplication-servers 10.2.0.3' do 286 | obj = @res['application-servers'][1] 287 | expect(obj.instance_of?(Hash)).to be_truthy 288 | expect(obj).to include({'uri' => '10.2.0.3', 289 | 'port' => 22}) 290 | end 291 | 292 | it 'web-servers 10.2.0.1' do 293 | obj = @res['web-servers'][0] 294 | expect(obj.instance_of?(Hash)).to be_truthy 295 | expect(obj).to include({'uri' => '10.2.0.1', 296 | 'port' => 22}) 297 | end 298 | 299 | after do 300 | File.delete(tmp_hosts) 301 | end 302 | end 303 | end 304 | 305 | -------------------------------------------------------------------------------- /spec/dynamic_inventory_spec.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | require 'ansible_spec' 3 | 4 | describe "load_targetsの実行" do 5 | context '正常系:DynamicInventory:1 Group, 1 hosts' do 6 | tmp_hosts = 'hosts' 7 | before do 8 | content_h = <<'EOF' 9 | #!/bin/bash 10 | echo '{"databases": {"hosts": ["host1.example.com"],"vars":{"a": true}}}' 11 | EOF 12 | create_file(tmp_hosts,content_h) 13 | File.chmod(0755,tmp_hosts) 14 | @res = AnsibleSpec.load_targets(tmp_hosts) 15 | end 16 | 17 | it 'res is hash' do 18 | expect(@res.instance_of?(Hash)).to be_truthy 19 | end 20 | 21 | it 'exist 2 group' do 22 | expect(@res.length).to eq 2 23 | end 24 | 25 | it 'exist group' do 26 | expect(@res.key?('databases')).to be_truthy 27 | expect(@res.key?('hosts_childrens')).to be_truthy 28 | end 29 | 30 | it 'databases host1.example.com' do 31 | obj = @res['databases'][0] 32 | expect(obj.instance_of?(Hash)).to be_truthy 33 | expect(obj).to include({'uri' => 'host1.example.com', 34 | 'port' => 22}) 35 | end 36 | 37 | after do 38 | File.delete(tmp_hosts) 39 | end 40 | end 41 | 42 | context '正常系:DynamicInventory:1 Group, 2 hosts' do 43 | tmp_hosts = 'hosts' 44 | before do 45 | content_h = <<'EOF' 46 | #!/bin/bash 47 | echo '{"databases": {"hosts": ["host1.example.com", "host2.example.com"],"vars":{"a": true}}}' 48 | EOF 49 | create_file(tmp_hosts,content_h) 50 | File.chmod(0755,tmp_hosts) 51 | @res = AnsibleSpec.load_targets(tmp_hosts) 52 | end 53 | 54 | it 'res is hash' do 55 | expect(@res.instance_of?(Hash)).to be_truthy 56 | end 57 | 58 | it 'exist 2 group' do 59 | expect(@res.length).to eq 2 60 | end 61 | 62 | it 'exist group' do 63 | expect(@res.key?('databases')).to be_truthy 64 | expect(@res.key?('hosts_childrens')).to be_truthy 65 | end 66 | 67 | it 'databases host1.example.com' do 68 | obj = @res['databases'][0] 69 | expect(obj.instance_of?(Hash)).to be_truthy 70 | expect(obj).to include({'uri' => 'host1.example.com', 71 | 'port' => 22}) 72 | end 73 | 74 | it 'databases host2.example.com' do 75 | obj = @res['databases'][1] 76 | expect(obj.instance_of?(Hash)).to be_truthy 77 | expect(obj).to include({'uri' => 'host2.example.com', 78 | 'port' => 22}) 79 | end 80 | 81 | after do 82 | File.delete(tmp_hosts) 83 | end 84 | end 85 | context '正常系:DynamicInventory:1 Group, 2 hosts. but no vars' do 86 | tmp_hosts = 'hosts' 87 | before do 88 | content_h = <<'EOF' 89 | #!/bin/bash 90 | echo '{"webservers": [ "host2.example.com", "host3.example.com" ]}' 91 | EOF 92 | create_file(tmp_hosts,content_h) 93 | File.chmod(0755,tmp_hosts) 94 | @res = AnsibleSpec.load_targets(tmp_hosts) 95 | end 96 | 97 | it 'res is hash' do 98 | expect(@res.instance_of?(Hash)).to be_truthy 99 | end 100 | 101 | it 'exist 2 group' do 102 | expect(@res.length).to eq 2 103 | end 104 | 105 | it 'exist group' do 106 | expect(@res.key?('webservers')).to be_truthy 107 | expect(@res.key?('hosts_childrens')).to be_truthy 108 | end 109 | 110 | it 'databases host1.example.com' do 111 | obj = @res['webservers'][0] 112 | expect(obj.instance_of?(Hash)).to be_truthy 113 | expect(obj).to include({'uri' => 'host2.example.com', 114 | 'port' => 22}) 115 | end 116 | 117 | it 'databases host2.example.com' do 118 | obj = @res['webservers'][1] 119 | expect(obj.instance_of?(Hash)).to be_truthy 120 | expect(obj).to include({'uri' => 'host3.example.com', 121 | 'port' => 22}) 122 | end 123 | 124 | after do 125 | File.delete(tmp_hosts) 126 | end 127 | end 128 | end 129 | 130 | -------------------------------------------------------------------------------- /spec/ec2_dynamic_inventory_spec.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | require 'ansible_spec' 3 | 4 | describe "load_targets_ec2" do 5 | context 'EC2_DynamicInventory' do 6 | tmp_hosts = 'hosts' 7 | before do 8 | content_h = <<'EOF' 9 | #!/bin/bash 10 | echo '{ "_meta": { "hostvars": { "some_key1": { "ec2_something_something": false }, "some_key2": { "ec2_something_something": true }}}, "tag_some_other_key1": [ "host-1", "host-2" ], "some_other_key2": [ "host-1" ]}' 11 | EOF 12 | create_file(tmp_hosts,content_h) 13 | File.chmod(0755,tmp_hosts) 14 | @res = AnsibleSpec.load_targets(tmp_hosts) 15 | end 16 | 17 | it 'res is hash' do 18 | expect(@res.instance_of?(Hash)).to be_truthy 19 | end 20 | 21 | it 'contains 3 groups' do 22 | expect(@res.length).to eq 3 23 | end 24 | 25 | it 'contains key tag_some_other_key1' do 26 | expect(@res.key?('tag_some_other_key1')).to be_truthy 27 | end 28 | 29 | it 'contains key some_other_key2' do 30 | expect(@res.key?('some_other_key2')).to be_truthy 31 | end 32 | 33 | it 'tag_some_other_key1 contains host-1 and host-2' do 34 | obj = @res['tag_some_other_key1'][0] 35 | expect(obj.instance_of?(Hash)).to be_truthy 36 | expect(obj).to include({'uri' => 'host-1', 37 | 'port' => 22}) 38 | obj = @res['tag_some_other_key1'][1] 39 | expect(obj.instance_of?(Hash)).to be_truthy 40 | expect(obj).to include({'uri' => 'host-2', 41 | 'port' => 22}) 42 | end 43 | 44 | it 'some_other_key2 contains host-1' do 45 | obj = @res['some_other_key2'][0] 46 | expect(obj.instance_of?(Hash)).to be_truthy 47 | expect(obj).to include({'uri' => 'host-1', 48 | 'port' => 22}) 49 | end 50 | 51 | after do 52 | File.delete(tmp_hosts) 53 | end 54 | end 55 | context 'EC2_DynamicInventory' do 56 | tmp_hosts = 'hosts' 57 | before do 58 | content_h = <<'EOF' 59 | #!/bin/bash 60 | echo '{ "_meta" : {"hostvars": {"54.1.2.3": {"ec2_ip_address": "54.1.2.3","ec2_key_name": "my-secret-key", "ec2_launch_time": "2016-01-06T03:59:56.000Z", "ec2_tag_Name": "sample-app", "ec2_tag_Stack": "sample-app"}}},"tag_Name_sample_app": ["54.1.2.3"]}' 61 | EOF 62 | create_file(tmp_hosts,content_h) 63 | File.chmod(0755,tmp_hosts) 64 | @res = AnsibleSpec.load_targets(tmp_hosts) 65 | end 66 | 67 | it 'res is hash' do 68 | expect(@res.instance_of?(Hash)).to be_truthy 69 | end 70 | 71 | it 'contains 2 groups' do 72 | expect(@res.length).to eq 2 73 | end 74 | 75 | it 'contains key tag_Name_sample_app' do 76 | expect(@res.key?('tag_Name_sample_app')).to be_truthy 77 | end 78 | 79 | it 'tag_Name_sample_app contains hash' do 80 | obj = @res['tag_Name_sample_app'][0] 81 | expect(obj.instance_of?(Hash)).to be_truthy 82 | expect(obj).to include({'uri' => '54.1.2.3', 83 | 'port' => 22}) 84 | end 85 | 86 | after do 87 | File.delete(tmp_hosts) 88 | end 89 | end 90 | end 91 | 92 | -------------------------------------------------------------------------------- /spec/get_variable_hosts_childrens_spec.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | require 'fileutils' 3 | require 'ansible_spec' 4 | require 'pp' 5 | 6 | describe "Dynamic Inventoryによるchildrenの依存関係を参照し、get_variablesの実行" do 7 | context 'Correct operation : develop groups by web-serers hosts childrens' do 8 | before do 9 | @current_dir = Dir.pwd() 10 | Dir.chdir('spec/case/get_variable/group_vars_hosts_childrens_env_dev/') 11 | @res = AnsibleSpec.get_variables("10.0.0.1", 0, "web-servers") 12 | end 13 | 14 | it 'res is hash' do 15 | expect(@res.instance_of?(Hash)).to be_truthy 16 | end 17 | 18 | it 'exist 7 pair in Hash' do 19 | expect(@res.length).to eq 7 20 | end 21 | 22 | it 'exist each pair' do 23 | expect(@res).to include( {"all_variable"=>"all!!"}, 24 | {"logs_conf_file"=>"web_logs.conf"}, 25 | {"nginx_conf_file"=>"develop_nginx.conf"}, 26 | {"foo"=>"foofoo-develop"}, 27 | {"bar"=>["bar1-develop", "bar2-develop", "bar3-develop"]}, 28 | {"env_name"=>"develop"}, 29 | {"my_common_libs_version"=>"1.2"} 30 | ) 31 | end 32 | 33 | after do 34 | Dir.chdir(@current_dir) 35 | end 36 | end 37 | 38 | context 'Correct operation : develop groups by application-servers hosts childrens ' do 39 | before do 40 | @current_dir = Dir.pwd() 41 | Dir.chdir('spec/case/get_variable/group_vars_hosts_childrens_env_dev/') 42 | @res_1st = AnsibleSpec.get_variables("10.0.0.2", 0, "application-servers") 43 | @res_2nd = AnsibleSpec.get_variables("10.0.0.3", 0, "application-servers") 44 | end 45 | 46 | it 'res is hash' do 47 | expect(@res_1st.instance_of?(Hash)).to be_truthy 48 | expect(@res_2nd.instance_of?(Hash)).to be_truthy 49 | end 50 | 51 | it 'exist 7 pair in Hash' do 52 | expect(@res_1st.length).to eq 7 53 | expect(@res_2nd.length).to eq 7 54 | end 55 | 56 | it 'exist each pair' do 57 | expect(@res_1st).to include( {"all_variable"=>"all!!"}, 58 | {"logs_conf_file"=>"application_logs.conf"}, 59 | {"java_version"=>"1.8.0_92"}, 60 | {"foo"=>"foofoo-develop"}, 61 | {"bar"=>["bar1-develop", "bar2-develop", "bar3-develop"]}, 62 | {"env_name"=>"develop"}, 63 | {"my_common_libs_version"=>"1.2"} 64 | ) 65 | expect(@res_2nd).to include( {"all_variable"=>"all!!"}, 66 | {"logs_conf_file"=>"application_logs.conf"}, 67 | {"java_version"=>"1.8.0_92"}, 68 | {"foo"=>"foofoo-develop"}, 69 | {"bar"=>["bar1-develop", "bar2-develop", "bar3-develop"]}, 70 | {"env_name"=>"develop"}, 71 | {"my_common_libs_version"=>"1.2"} 72 | ) 73 | end 74 | 75 | after do 76 | Dir.chdir(@current_dir) 77 | end 78 | end 79 | 80 | context 'Correct operation : develop groups by database-servers hosts childrens ' do 81 | before do 82 | @current_dir = Dir.pwd() 83 | Dir.chdir('spec/case/get_variable/group_vars_hosts_childrens_env_dev/') 84 | @res = AnsibleSpec.get_variables("10.0.0.4", 0, "database-servers") 85 | end 86 | 87 | it 'res is hash' do 88 | expect(@res.instance_of?(Hash)).to be_truthy 89 | end 90 | 91 | it 'exist 8 pair in Hash' do 92 | expect(@res.length).to eq 8 93 | end 94 | 95 | it 'exist each pair' do 96 | expect(@res).to include( {"all_variable"=>"all!!"}, 97 | {"bar"=>["bar1-develop", "bar2-develop", "bar3-develop"]}, 98 | {"env_name"=>"develop"}, 99 | {"foo"=>"foofoo-develop"}, 100 | {"logs_conf_file"=>"database_logs.conf"}, 101 | {"my_common_libs_version"=>"1.2"}, 102 | {"mysql_version"=>"5.7.12"}, 103 | {"mysql_dependencies_libs"=>["foo-v2","bar-v2"]} 104 | ) 105 | end 106 | 107 | after do 108 | Dir.chdir(@current_dir) 109 | end 110 | end 111 | 112 | context 'Correct operation : staging groups by web-serers hosts childrens' do 113 | before do 114 | @current_dir = Dir.pwd() 115 | Dir.chdir('spec/case/get_variable/group_vars_hosts_childrens_env_stg/') 116 | @res = AnsibleSpec.get_variables("10.0.0.1", 0, "web-servers") 117 | end 118 | 119 | it 'res is hash' do 120 | expect(@res.instance_of?(Hash)).to be_truthy 121 | end 122 | 123 | it 'exist 7 pair in Hash' do 124 | expect(@res.length).to eq 7 125 | end 126 | 127 | it 'exist each pair' do 128 | expect(@res).to include( {"all_variable"=>"all!!"}, 129 | {"logs_conf_file"=>"web_logs.conf"}, 130 | {"nginx_conf_file"=>"staging_nginx.conf"}, 131 | {"foo"=>"foofoo-staging"}, 132 | {"bar"=>["bar1-staging", "bar2-staging", "bar3-staging"]}, 133 | {"env_name"=>"staging"}, 134 | {"my_common_libs_version"=>"1.1"} 135 | ) 136 | end 137 | 138 | after do 139 | Dir.chdir(@current_dir) 140 | end 141 | end 142 | 143 | context 'Correct operation : staging groups by application-servers hosts childrens ' do 144 | before do 145 | @current_dir = Dir.pwd() 146 | Dir.chdir('spec/case/get_variable/group_vars_hosts_childrens_env_stg/') 147 | @res_1st = AnsibleSpec.get_variables("10.0.0.2", 0, "application-servers") 148 | @res_2nd = AnsibleSpec.get_variables("10.0.0.3", 0, "application-servers") 149 | end 150 | 151 | it 'res is hash' do 152 | expect(@res_1st.instance_of?(Hash)).to be_truthy 153 | expect(@res_2nd.instance_of?(Hash)).to be_truthy 154 | end 155 | 156 | it 'exist 7 pair in Hash' do 157 | expect(@res_1st.length).to eq 7 158 | expect(@res_2nd.length).to eq 7 159 | end 160 | 161 | it 'exist each pair' do 162 | expect(@res_1st).to include( {"all_variable"=>"all!!"}, 163 | {"logs_conf_file"=>"application_logs.conf"}, 164 | {"java_version"=>"1.8.0_66"}, 165 | {"foo"=>"foofoo-staging"}, 166 | {"bar"=>["bar1-staging", "bar2-staging", "bar3-staging"]}, 167 | {"env_name"=>"staging"}, 168 | {"my_common_libs_version"=>"1.1"} 169 | ) 170 | expect(@res_2nd).to include( {"all_variable"=>"all!!"}, 171 | {"logs_conf_file"=>"application_logs.conf"}, 172 | {"java_version"=>"1.8.0_66"}, 173 | {"foo"=>"foofoo-staging"}, 174 | {"bar"=>["bar1-staging", "bar2-staging", "bar3-staging"]}, 175 | {"env_name"=>"staging"}, 176 | {"my_common_libs_version"=>"1.1"} 177 | ) 178 | end 179 | 180 | after do 181 | Dir.chdir(@current_dir) 182 | end 183 | end 184 | 185 | context 'Correct operation : staging groups by database-servers hosts childrens ' do 186 | before do 187 | @current_dir = Dir.pwd() 188 | Dir.chdir('spec/case/get_variable/group_vars_hosts_childrens_env_stg/') 189 | @res = AnsibleSpec.get_variables("10.0.0.4", 0, "database-servers") 190 | end 191 | 192 | it 'res is hash' do 193 | expect(@res.instance_of?(Hash)).to be_truthy 194 | end 195 | 196 | it 'exist 8 pair in Hash' do 197 | expect(@res.length).to eq 8 198 | end 199 | 200 | it 'exist each pair' do 201 | expect(@res).to include( {"all_variable"=>"all!!"}, 202 | {"bar"=>["bar1-staging", "bar2-staging", "bar3-staging"]}, 203 | {"env_name"=>"staging"}, 204 | {"foo"=>"foofoo-staging"}, 205 | {"logs_conf_file"=>"database_logs.conf"}, 206 | {"my_common_libs_version"=>"1.1"}, 207 | {"mysql_version"=>"5.6.30"}, 208 | {"mysql_dependencies_libs"=>["foo-v1","bar-v1"]} 209 | ) 210 | end 211 | 212 | after do 213 | Dir.chdir(@current_dir) 214 | end 215 | end 216 | 217 | context 'Correct operation : production groups by web-serers hosts childrens' do 218 | before do 219 | @current_dir = Dir.pwd() 220 | Dir.chdir('spec/case/get_variable/group_vars_hosts_childrens_env_prd/') 221 | @res = AnsibleSpec.get_variables("10.0.0.1", 0, "web-servers") 222 | end 223 | 224 | it 'res is hash' do 225 | expect(@res.instance_of?(Hash)).to be_truthy 226 | end 227 | 228 | it 'exist 7 pair in Hash' do 229 | expect(@res.length).to eq 7 230 | end 231 | 232 | it 'exist each pair' do 233 | expect(@res).to include( {"all_variable"=>"all!!"}, 234 | {"logs_conf_file"=>"web_logs.conf"}, 235 | {"nginx_conf_file"=>"production_nginx.conf"}, 236 | {"foo"=>"foofoo-production"}, 237 | {"bar"=>["bar1-production", "bar2-production", "bar3-production"]}, 238 | {"env_name"=>"production"}, 239 | {"my_common_libs_version"=>"1.0"} 240 | ) 241 | end 242 | 243 | after do 244 | Dir.chdir(@current_dir) 245 | end 246 | end 247 | 248 | context 'Correct operation : production groups by application-servers hosts childrens ' do 249 | before do 250 | @current_dir = Dir.pwd() 251 | Dir.chdir('spec/case/get_variable/group_vars_hosts_childrens_env_prd/') 252 | @res_1st = AnsibleSpec.get_variables("10.0.0.2", 0, "application-servers") 253 | @res_2nd = AnsibleSpec.get_variables("10.0.0.3", 0, "application-servers") 254 | end 255 | 256 | it 'res is hash' do 257 | expect(@res_1st.instance_of?(Hash)).to be_truthy 258 | expect(@res_2nd.instance_of?(Hash)).to be_truthy 259 | end 260 | 261 | it 'exist 7 pair in Hash' do 262 | expect(@res_1st.length).to eq 7 263 | expect(@res_2nd.length).to eq 7 264 | end 265 | 266 | it 'exist each pair' do 267 | expect(@res_1st).to include( {"all_variable"=>"all!!"}, 268 | {"logs_conf_file"=>"application_logs.conf"}, 269 | {"java_version"=>"1.8.0_45"}, 270 | {"foo"=>"foofoo-production"}, 271 | {"bar"=>["bar1-production", "bar2-production", "bar3-production"]}, 272 | {"env_name"=>"production"}, 273 | {"my_common_libs_version"=>"1.0"} 274 | ) 275 | expect(@res_2nd).to include( {"all_variable"=>"all!!"}, 276 | {"logs_conf_file"=>"application_logs.conf"}, 277 | {"java_version"=>"1.8.0_45"}, 278 | {"foo"=>"foofoo-production"}, 279 | {"bar"=>["bar1-production", "bar2-production", "bar3-production"]}, 280 | {"env_name"=>"production"}, 281 | {"my_common_libs_version"=>"1.0"} 282 | ) 283 | end 284 | 285 | after do 286 | Dir.chdir(@current_dir) 287 | end 288 | end 289 | 290 | context 'Correct operation : production groups by database-servers hosts childrens ' do 291 | before do 292 | @current_dir = Dir.pwd() 293 | Dir.chdir('spec/case/get_variable/group_vars_hosts_childrens_env_prd/') 294 | @res = AnsibleSpec.get_variables("10.0.0.4", 0, "database-servers") 295 | end 296 | 297 | it 'res is hash' do 298 | expect(@res.instance_of?(Hash)).to be_truthy 299 | end 300 | 301 | it 'exist 8 pair in Hash' do 302 | expect(@res.length).to eq 8 303 | end 304 | 305 | it 'exist each pair' do 306 | expect(@res).to include( {"all_variable"=>"all!!"}, 307 | {"bar"=>["bar1-production", "bar2-production", "bar3-production"]}, 308 | {"env_name"=>"production"}, 309 | {"foo"=>"foofoo-production"}, 310 | {"logs_conf_file"=>"database_logs.conf"}, 311 | {"my_common_libs_version"=>"1.0"}, 312 | {"mysql_version"=>"5.6.30"}, 313 | {"mysql_dependencies_libs"=>["foo-v1","bar-v1"]} 314 | ) 315 | end 316 | 317 | after do 318 | Dir.chdir(@current_dir) 319 | end 320 | end 321 | 322 | end 323 | -------------------------------------------------------------------------------- /spec/get_variable_spec.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | require 'fileutils' 3 | require 'ansible_spec' 4 | 5 | describe "get_variablesの実行" do 6 | context 'Correct operation : roles default' do 7 | before do 8 | @current_dir = Dir.pwd() 9 | Dir.chdir('spec/case/get_variable/roles_default/') 10 | @res = AnsibleSpec.get_variables('192.168.1.1', 0) 11 | end 12 | 13 | it 'res is hash' do 14 | expect(@res.instance_of?(Hash)).to be_truthy 15 | end 16 | 17 | it 'exist three pairs in Hash' do 18 | expect(@res.length).to eq 3 19 | end 20 | 21 | it 'exists each pair' do 22 | expect(@res).to include({'role_var' => 'role_var in roles default'}, 23 | {'site_var' => 'site_var in roles default'}, 24 | {'host_var'=>'host_var in roles default'}, 25 | ) 26 | end 27 | 28 | after do 29 | Dir.chdir(@current_dir) 30 | end 31 | end 32 | 33 | context 'Correct operation : all groups' do 34 | before do 35 | @current_dir = Dir.pwd() 36 | Dir.chdir('spec/case/get_variable/group_all/') 37 | @res = AnsibleSpec.get_variables('192.168.1.1', 0) 38 | end 39 | 40 | it 'res is hash' do 41 | expect(@res.instance_of?(Hash)).to be_truthy 42 | end 43 | 44 | it 'exist five pairs in Hash' do 45 | expect(@res.length).to eq 5 46 | end 47 | 48 | it 'exists each pair' do 49 | expect(@res).to include({'role_var' => 'group all'}, 50 | {'site_var' => 'group all'}, 51 | {'host_var'=>'group all'}, 52 | {'group_var'=>'group all'}, 53 | {'group_all_var'=>'group all'} 54 | ) 55 | end 56 | 57 | after do 58 | Dir.chdir(@current_dir) 59 | end 60 | end 61 | 62 | context 'Correct operation : each group vars' do 63 | before do 64 | @current_dir = Dir.pwd() 65 | Dir.chdir('spec/case/get_variable/group_each_vars/') 66 | @res = AnsibleSpec.get_variables('192.168.1.1', 0) 67 | end 68 | 69 | it 'res is hash' do 70 | expect(@res.instance_of?(Hash)).to be_truthy 71 | end 72 | 73 | it 'exist five pairs in Hash' do 74 | expect(@res.length).to eq 5 75 | end 76 | 77 | it 'exists each pair' do 78 | expect(@res).to include({'role_var' => 'group1'}, 79 | {'site_var' => 'group1'}, 80 | {'host_var'=>'group1'}, 81 | {'group_var'=>'group1'}, 82 | {'group_all_var'=>'group all'} 83 | ) 84 | end 85 | 86 | after do 87 | Dir.chdir(@current_dir) 88 | end 89 | end 90 | 91 | context 'Correct operation : host_vars' do 92 | before do 93 | @current_dir = Dir.pwd() 94 | Dir.chdir('spec/case/get_variable/host_vars/') 95 | @res = AnsibleSpec.get_variables('192.168.1.1', 0) 96 | end 97 | 98 | it 'res is hash' do 99 | expect(@res.instance_of?(Hash)).to be_truthy 100 | end 101 | 102 | it 'exist three pairs in Hash' do 103 | expect(@res.length).to eq 3 104 | end 105 | 106 | it 'exists each pair' do 107 | expect(@res).to include({'role_var' => 'host role_var'}, # in host_var/192.168.1.1.yml 108 | {'site_var' => 'host site_var'}, # in host_var/192.168.1.1.yml 109 | {'host_var'=>'host 192.168.1.1'}, # in host_var/192.168.1.1.yml 110 | ) 111 | end 112 | 113 | after do 114 | Dir.chdir(@current_dir) 115 | end 116 | end 117 | 118 | context 'Correct operation : host_vars' do 119 | before do 120 | @current_dir = Dir.pwd() 121 | Dir.chdir('spec/case/get_variable/host_vars/') 122 | @res = AnsibleSpec.get_variables('192.168.1.2', 1) 123 | end 124 | 125 | it 'res is hash' do 126 | expect(@res.instance_of?(Hash)).to be_truthy 127 | end 128 | 129 | it 'exist three pairs in Hash' do 130 | expect(@res.length).to eq 3 131 | end 132 | 133 | it 'exists each pair' do 134 | expect(@res).to include({'role_var' => 'host role_var'}, # in host_var/192.168.1.2.yml 135 | {'site_var' => 'host site_var'}, # in host_var/192.168.1.2.yml 136 | {'host_var'=>'host 192.168.1.2'}, # in host_var/192.168.1.2.yml 137 | ) 138 | end 139 | 140 | after do 141 | Dir.chdir(@current_dir) 142 | end 143 | end 144 | 145 | context 'Correct operation : playbook only' do 146 | before do 147 | @current_dir = Dir.pwd() 148 | Dir.chdir('spec/case/get_variable/playbook_only/') 149 | @res = AnsibleSpec.get_variables('192.168.1.1', 0) 150 | end 151 | 152 | it 'res is hash' do 153 | expect(@res.instance_of?(Hash)).to be_truthy 154 | end 155 | 156 | it 'exist two pairs in Hash' do 157 | expect(@res.length).to eq 2 158 | end 159 | 160 | it 'exists each pair' do 161 | expect(@res).to include({'role_var' => 'site'}, 162 | {'site_var' => 'site'}) 163 | end 164 | 165 | after do 166 | Dir.chdir(@current_dir) 167 | end 168 | end 169 | 170 | context 'Correct operation : playbook & group_var & host_var' do 171 | before do 172 | @current_dir = Dir.pwd() 173 | Dir.chdir('spec/case/get_variable/group_host_playbook/') 174 | @res = AnsibleSpec.get_variables('192.168.1.1', 0) 175 | end 176 | 177 | it 'res is hash' do 178 | expect(@res.instance_of?(Hash)).to be_truthy 179 | end 180 | 181 | it 'exist five pairs in Hash' do 182 | expect(@res.length).to eq 5 183 | end 184 | 185 | it 'exists each pair' do 186 | expect(@res).to include({'role_var' => 'site'}, # site.yml 187 | {'site_var' => 'site'}, # site.yml 188 | {'host_var'=>'host 192.168.1.1'}, # in host_var/192.168.1.1.yml 189 | {'group_var'=>'group1'}, # in group_var/all.yml 190 | {'group_all_var'=>'group all'} # in group_var/all.yml 191 | ) 192 | end 193 | 194 | after do 195 | Dir.chdir(@current_dir) 196 | end 197 | end 198 | 199 | context 'Correct operation : playbook & group_var & host_var & variable in role' do 200 | before do 201 | @current_dir = Dir.pwd() 202 | Dir.chdir('spec/case/get_variable/group_host_playbook_role/') 203 | @res = AnsibleSpec.get_variables('192.168.1.1', 0) 204 | end 205 | 206 | it 'res is hash' do 207 | expect(@res.instance_of?(Hash)).to be_truthy 208 | end 209 | 210 | it 'exist five pairs in Hash' do 211 | expect(@res.length).to eq 5 212 | end 213 | 214 | it 'exists each pair' do 215 | expect(@res).to include({'role_var' => 'role'}, # in roles/test/vars/main.yml 216 | {'site_var' => 'site'}, # site.yml 217 | {'host_var'=>'host 192.168.1.1'}, # in host_var/192.168.1.1.yml 218 | {'group_var'=>'group1'}, # in group_var/all.yml 219 | {'group_all_var'=>'group all'} # in group_var/all.yml 220 | ) 221 | end 222 | 223 | after do 224 | Dir.chdir(@current_dir) 225 | end 226 | end 227 | 228 | context 'Correct operation : deep_merge' do 229 | before do 230 | @current_dir = Dir.pwd() 231 | Dir.chdir('spec/case/get_variable/deep_merge') 232 | @res = AnsibleSpec.get_variables('192.168.1.1', 0) 233 | end 234 | 235 | it 'res is hash' do 236 | expect(@res.instance_of?(Hash)).to be_truthy 237 | end 238 | 239 | it 'exists one pair in Hash' do 240 | expect(@res.length).to eq 1 241 | end 242 | 243 | it 'exists the pair' do 244 | expect(@res).to include({'merge_var' => { 245 | 'keep'=>'role', 246 | 'override'=>'site', 247 | 'add'=>'site' 248 | }}, 249 | ) 250 | end 251 | 252 | after do 253 | Dir.chdir(@current_dir) 254 | end 255 | end 256 | 257 | context 'Correct operation : group vars for group1 hosts ' do 258 | before do 259 | @current_dir = Dir.pwd() 260 | Dir.chdir('spec/case/get_variable/group_each_vars_parent_child/') 261 | ENV["PLAYBOOK"] = 'site1.yml' 262 | ENV["INVENTORY"] = 'hosts' 263 | ENV["VARS_DIRS_PATH"] = '' 264 | @res = AnsibleSpec.get_variables("192.168.1.1", 0, "group1") 265 | end 266 | 267 | it 'res is hash' do 268 | expect(@res.instance_of?(Hash)).to be_truthy 269 | end 270 | 271 | it 'exist 6 pair in Hash' do 272 | expect(@res.length).to eq 6 273 | end 274 | 275 | it 'exist each pair' do 276 | expect(@res).to include( {"role_var"=>"group1"}, 277 | {"site_var"=>"group1"}, 278 | {"host_var"=>"group1"}, 279 | {"group_var"=>"group1"}, 280 | {"group_var_parent"=>"parentgroup"}, 281 | {"group_all_var"=>"group all"} 282 | ) 283 | end 284 | 285 | after do 286 | ENV.delete('PLAYBOOK') 287 | ENV.delete('INVENTORY') 288 | ENV.delete('VARS_DIRS_PATH') 289 | Dir.chdir(@current_dir) 290 | end 291 | end 292 | 293 | context 'Correct operation : group vars for group2 hosts ' do 294 | before do 295 | @current_dir = Dir.pwd() 296 | Dir.chdir('spec/case/get_variable/group_each_vars_parent_child/') 297 | ENV["PLAYBOOK"] = 'site2.yml' 298 | ENV["INVENTORY"] = 'hosts' 299 | ENV["VARS_DIRS_PATH"] = '' 300 | @res = AnsibleSpec.get_variables("192.168.1.2", 0, "group2") 301 | end 302 | 303 | it 'res is hash' do 304 | expect(@res.instance_of?(Hash)).to be_truthy 305 | end 306 | 307 | it 'exist 6 pair in Hash' do 308 | expect(@res.length).to eq 6 309 | end 310 | 311 | it 'exist each pair' do 312 | expect(@res).to include( {"role_var"=>"group2"}, 313 | {"site_var"=>"group2"}, 314 | {"host_var"=>"group2"}, 315 | {"group_var"=>"group2"}, 316 | {"group_var_parent"=>"parentgroup"}, 317 | {"group_all_var"=>"group all"} 318 | ) 319 | end 320 | 321 | after do 322 | ENV.delete('PLAYBOOK') 323 | ENV.delete('INVENTORY') 324 | ENV.delete('VARS_DIRS_PATH') 325 | Dir.chdir(@current_dir) 326 | end 327 | end 328 | 329 | context 'Correct operation : group vars for parentgroup hosts ' do 330 | before do 331 | @current_dir = Dir.pwd() 332 | Dir.chdir('spec/case/get_variable/group_each_vars_parent_child/') 333 | ENV["PLAYBOOK"] = 'site3.yml' 334 | ENV["INVENTORY"] = 'hosts' 335 | ENV["VARS_DIRS_PATH"] = '' 336 | @res = AnsibleSpec.get_variables("192.168.1.1", 0, "parentgroup") 337 | end 338 | 339 | it 'res is hash' do 340 | expect(@res.instance_of?(Hash)).to be_truthy 341 | end 342 | 343 | it 'exist 6 pair in Hash' do 344 | expect(@res.length).to eq 6 345 | end 346 | 347 | it 'exist each pair' do 348 | expect(@res).to include( {"role_var"=>"parentgroup"}, 349 | {"site_var"=>"parentgroup"}, 350 | {"host_var"=>"parentgroup"}, 351 | {"group_var"=>"parentgroup"}, 352 | {"group_var_parent"=>"parentgroup"}, 353 | {"group_all_var"=>"group all"} 354 | ) 355 | end 356 | 357 | after do 358 | ENV.delete('PLAYBOOK') 359 | ENV.delete('INVENTORY') 360 | ENV.delete('VARS_DIRS_PATH') 361 | Dir.chdir(@current_dir) 362 | end 363 | end 364 | 365 | # Fixed a problem that there is a gap between Index passed from Rakefile when empty group exists. by #135 366 | # cat hosts 367 | # [group_a] #no host 368 | # [group_b] 369 | # localhost.localdomain ansible_ssh_private_key_file=~/.ssh/id_rsa 370 | 371 | context 'Correct operation : empty group PR #135 ' do 372 | before do 373 | @current_dir = Dir.pwd() 374 | Dir.chdir('spec/case/get_variable/empty_group') 375 | ENV["PLAYBOOK"] = 'site.yml' 376 | ENV["INVENTORY"] = 'hosts' 377 | ENV["VARS_DIRS_PATH"] = '' 378 | @res = AnsibleSpec.get_variables("localhost.localdomain", 0) 379 | end 380 | 381 | it 'res is hash' do 382 | expect(@res.instance_of?(Hash)).to be_truthy 383 | end 384 | 385 | it 'exist 1 pair in Hash' do 386 | expect(@res.length).to eq 1 387 | end 388 | 389 | it 'get correct variable' do 390 | expect(@res).to eq("group_b_var" => "fuga") 391 | end 392 | 393 | it 'not get incorrect variables' do 394 | expect(@res).not_to eq("group_a_var" => "hoge") 395 | end 396 | 397 | after do 398 | ENV.delete('PLAYBOOK') 399 | ENV.delete('INVENTORY') 400 | ENV.delete('VARS_DIRS_PATH') 401 | Dir.chdir(@current_dir) 402 | end 403 | end 404 | end 405 | 406 | describe "get_hash_behaviourの実行" do 407 | context 'Correct operation : should get replace' do 408 | before do 409 | @current_dir = Dir.pwd() 410 | Dir.chdir('spec/case/get_hash_behaviour/basic') 411 | end 412 | 413 | it 'should get replace' do 414 | res = AnsibleSpec.get_hash_behaviour() 415 | expect(res).to eq 'replace' 416 | end 417 | 418 | after do 419 | Dir.chdir(@current_dir) 420 | end 421 | end 422 | 423 | context 'Correct operation : merge in ENV["HASH_BEHAVIOUR"]' do 424 | before do 425 | ENV["HASH_BEHAVIOUR"] = 'merge' 426 | @current_dir = Dir.pwd() 427 | Dir.chdir('spec/case/get_hash_behaviour/basic') 428 | end 429 | 430 | it 'should get merge' do 431 | res = AnsibleSpec.get_hash_behaviour() 432 | expect(res).to eq 'merge' 433 | end 434 | 435 | after do 436 | ENV.delete('HASH_BEHAVIOUR') 437 | Dir.chdir(@current_dir) 438 | end 439 | end 440 | 441 | context 'Correct operation : resolve variables' do 442 | before do 443 | @current_dir = Dir.pwd() 444 | Dir.chdir('spec/case/get_variable/resolve_variables/') 445 | @res = AnsibleSpec.get_variables('192.168.1.1', 0) 446 | end 447 | 448 | it 'res is hash' do 449 | expect(@res.instance_of?(Hash)).to be_truthy 450 | end 451 | 452 | it 'exist fourteen pairs in Hash' do 453 | expect(@res.length).to eq 14 454 | end 455 | 456 | it 'exist all pairs' do 457 | expect(@res).to include({'var_nested_one_1' => 'val_nested_one'}) 458 | expect(@res).to include({'var_nested_one_2' => 'val_nested_one'}) 459 | expect(@res).to include({'var_nested_two_1' => 'val_nested_two'}) 460 | expect(@res).to include({'var_nested_two_2' => 'val_nested_two'}) 461 | expect(@res).to include({'var_nested_two_3' => 'val_nested_two'}) 462 | expect(@res).to include({'var_nested_hash_1' => 'val_hash'}) 463 | expect(@res).to include({'var_nested_hash_2' => {'key' => 'val_hash'}}) 464 | expect(@res).to include({'var_nested_array_1' => 'val_array'}) 465 | expect(@res).to include({'var_nested_array_2' => ['val_array']}) 466 | expect(@res).to include({'var_nested_array_hash_1' => 'val_array_hash'}) 467 | expect(@res).to include({'var_nested_array_hash_2' => [{'key' => 'val_array_hash'}]}) 468 | expect(@res).to include({'var_nested_whitespace_1' => 'val_nested_whitespace'}) 469 | expect(@res).to include({'var_nested_whitespace_2' => 'val_nested_whitespace'}) 470 | expect(@res).to include({'var_missingtarget_2' => '{{ var_missingtarget_1 }}'}) 471 | end 472 | 473 | after do 474 | Dir.chdir(@current_dir) 475 | end 476 | end 477 | 478 | context 'Correct operation : mistake word in ENV["HASH_BEHAVIOUR"]' do 479 | before do 480 | ENV["HASH_BEHAVIOUR"] = 'mistake_word' 481 | @current_dir = Dir.pwd() 482 | Dir.chdir('spec/case/get_hash_behaviour/basic') 483 | end 484 | 485 | it 'exitする' do 486 | expect{ AnsibleSpec.get_hash_behaviour }.to raise_error(SystemExit) 487 | end 488 | 489 | after do 490 | ENV.delete('HASH_BEHAVIOUR') 491 | Dir.chdir(@current_dir) 492 | end 493 | end 494 | 495 | context 'Correct operation : with .ansiblespec' do 496 | before do 497 | @current_dir = Dir.pwd() 498 | Dir.chdir('spec/case/get_hash_behaviour/with_ansiblespec') 499 | end 500 | 501 | it 'should get merge' do 502 | res = AnsibleSpec.get_hash_behaviour() 503 | expect(res).to eq 'merge' 504 | end 505 | 506 | after do 507 | ENV.delete('HASH_BEHAVIOUR') 508 | Dir.chdir(@current_dir) 509 | end 510 | end 511 | 512 | context 'Correct operation : overwrite ENV["HASH_BEHAVIOUR"]' do 513 | before do 514 | ENV["HASH_BEHAVIOUR"] = 'replace' 515 | @current_dir = Dir.pwd() 516 | Dir.chdir('spec/case/get_hash_behaviour/with_ansiblespec') 517 | end 518 | 519 | it 'should get merge' do 520 | res = AnsibleSpec.get_hash_behaviour() 521 | expect(res).to eq 'replace' 522 | end 523 | 524 | after do 525 | ENV.delete('HASH_BEHAVIOUR') 526 | Dir.chdir(@current_dir) 527 | end 528 | end 529 | end 530 | 531 | describe 'merge_variablesの実行' do 532 | context 'Correct operation : deep_merge (merge)' do 533 | before do 534 | @current_dir = Dir.pwd() 535 | Dir.chdir('spec/case/get_variable/deep_merge') 536 | h1 = {'merge_var' => {'keep'=>'h1','override'=>'h1','add'=>'h1'}} 537 | h2 = {'merge_var' => {'override'=>'h2','add'=>'h2'}} 538 | @res = AnsibleSpec.merge_variables(h1, h2) 539 | end 540 | 541 | it 'res is hash' do 542 | expect(@res.instance_of?(Hash)).to be_truthy 543 | end 544 | 545 | it 'exist 1 pair in Hash' do 546 | expect(@res.length).to eq 1 547 | end 548 | 549 | it 'should h1 + h2' do 550 | expect(@res).to include({'merge_var' => { 551 | 'keep'=>'h1', 552 | 'override'=>'h2', 553 | 'add'=>'h2' 554 | }}, 555 | ) 556 | end 557 | 558 | after do 559 | Dir.chdir(@current_dir) 560 | end 561 | end 562 | 563 | context 'Correct operation : deep_merge (replace)' do 564 | before do 565 | ENV["HASH_BEHAVIOUR"] = 'replace' 566 | @current_dir = Dir.pwd() 567 | Dir.chdir('spec/case/get_variable/deep_merge') 568 | h1 = {'merge_var' => {'keep'=>'h1','override'=>'h1','add'=>'h1'}} 569 | h2 = {'merge_var' => {'override'=>'h2','add'=>'h2'}} 570 | @res = AnsibleSpec.merge_variables(h1, h2) 571 | end 572 | 573 | it 'res is hash' do 574 | expect(@res.instance_of?(Hash)).to be_truthy 575 | end 576 | 577 | it 'exists one pair in Hash' do 578 | expect(@res.length).to eq 1 579 | end 580 | 581 | it 'should replace from h1 to h2' do 582 | expect(@res).to include({'merge_var' => { 583 | 'override'=>'h2', 584 | 'add'=>'h2' 585 | }}, 586 | ) 587 | end 588 | 589 | after do 590 | ENV.delete('HASH_BEHAVIOUR') 591 | Dir.chdir(@current_dir) 592 | end 593 | end 594 | end 595 | 596 | describe "load_vars_fileの実行" do 597 | context 'Correct operation : without .yml extension' do 598 | # https://github.com/volanja/ansible_spec/pull/66 599 | # group_vars/xxx priority is higher than group_vars/xxx.yml. 600 | before do 601 | @current_dir = Dir.pwd() 602 | Dir.chdir('spec/case/load_vars_file/group_all/') 603 | vars = Hash.new 604 | file = 'group_vars/all' 605 | @res = AnsibleSpec.load_vars_file(vars, file) 606 | end 607 | 608 | it 'res is hash' do 609 | expect(@res.instance_of?(Hash)).to be_truthy 610 | end 611 | 612 | it 'exist one pair in Hash' do 613 | expect(@res.length).to eq 1 614 | end 615 | 616 | it 'exists the pair' do 617 | expect(@res).to include({'role_var' => 'without .yml extension'}) 618 | end 619 | 620 | after do 621 | Dir.chdir(@current_dir) 622 | end 623 | end 624 | 625 | context 'Correct operation : deep merge' do 626 | # https://github.com/volanja/ansible_spec/pull/66 627 | # group_vars/xxx priority is higher than group_vars/xxx.yml. 628 | before do 629 | @current_dir = Dir.pwd() 630 | Dir.chdir('spec/case/load_vars_file/group_all_deep_merge/') 631 | vars = Hash.new 632 | file = 'group_vars/all' 633 | @res = AnsibleSpec.load_vars_file(vars, file, true) 634 | end 635 | 636 | it 'res is hash' do 637 | expect(@res.instance_of?(Hash)).to be_truthy 638 | end 639 | 640 | it 'exists one pair in Hash' do 641 | expect(@res.length).to eq 1 642 | end 643 | 644 | it 'exists the pair' do 645 | expect(@res).to include({'role_var' => 'without .yml extension'}) 646 | end 647 | 648 | after do 649 | Dir.chdir(@current_dir) 650 | end 651 | end 652 | 653 | context 'Correct operation : vars in directory' do 654 | before do 655 | @current_dir = Dir.pwd() 656 | Dir.chdir('spec/case/load_vars_file/vars_dir/') 657 | vars = Hash.new 658 | file = 'group_vars/all' 659 | @res = AnsibleSpec.load_vars_file(vars, file) 660 | end 661 | 662 | it 'res is hash' do 663 | expect(@res.instance_of?(Hash)).to be_truthy 664 | end 665 | 666 | it 'exist two pairs in Hash' do 667 | expect(@res.length).to eq 2 668 | end 669 | 670 | it 'exists each pair' do 671 | expect(@res).to include({'var1' => 'val1'}) 672 | expect(@res).to include({'var2' => 'val2'}) 673 | end 674 | 675 | after do 676 | Dir.chdir(@current_dir) 677 | end 678 | end 679 | 680 | # Ansible::Vault support Ruby 2.1.0 and higher. 681 | # Skip test when Ruby 1.9.3 & 2.0.0 682 | if Gem::Version.new(RUBY_VERSION.dup) >= Gem::Version.new('2.1') 683 | context 'Correct operation : vaults in directory' do 684 | before do 685 | @current_dir = Dir.pwd() 686 | Dir.chdir('spec/case/load_vars_file/vault_dir/') 687 | vars = Hash.new 688 | file = 'group_vars/all' 689 | @res = AnsibleSpec.load_vars_file(vars, file) 690 | end 691 | 692 | it 'res is hash' do 693 | expect(@res.instance_of?(Hash)).to be_truthy 694 | end 695 | 696 | it 'exists one pair in Hash' do 697 | expect(@res.length).to eq 1 698 | end 699 | 700 | it 'exists the pair' do 701 | expect(@res).to include({'vault_var' => 'val'}) 702 | end 703 | 704 | after do 705 | Dir.chdir(@current_dir) 706 | end 707 | end 708 | end 709 | end 710 | -------------------------------------------------------------------------------- /spec/get_variable_vars_dirs_path_spec.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | require 'fileutils' 3 | require 'ansible_spec' 4 | 5 | describe "Run get_variables" do 6 | context 'Correct operation : env VARS_DIRS_PATH=inventories/staging - all groups' do 7 | require 'yaml' 8 | tmp_playbook = 'site.yml' 9 | tmp_inventory_file = 'inventories/staging/hosts' 10 | tmp_vars_dirs_path = 'inventories/staging' 11 | 12 | before do 13 | @current_dir = Dir.pwd() 14 | Dir.chdir('spec/case/get_variable/inventories_env_group_all/') 15 | ENV["PLAYBOOK"] = tmp_playbook 16 | ENV["INVENTORY"] = tmp_inventory_file 17 | ENV["VARS_DIRS_PATH"] = tmp_vars_dirs_path 18 | @res = AnsibleSpec.get_variables('192.168.2.1', 0) 19 | end 20 | 21 | it 'res is hash' do 22 | expect(@res.instance_of?(Hash)).to be_truthy 23 | end 24 | 25 | it 'exist 5 pair in Hash' do 26 | expect(@res.length).to eq 5 27 | end 28 | 29 | it 'exist each pair' do 30 | expect(@res).to include({'role_var' => 'group all'}, 31 | {'site_var' => 'group all'}, 32 | {'host_var' => 'group all'}, 33 | {'group_var' => 'group all'}, 34 | {'group_all_var' => 'group all'} 35 | ) 36 | end 37 | 38 | after do 39 | ENV.delete('PLAYBOOK') 40 | ENV.delete('INVENTORY') 41 | ENV.delete('VARS_DIRS_PATH') 42 | Dir.chdir(@current_dir) 43 | end 44 | end 45 | 46 | context 'Correct operation : env VARS_DIRS_PATH=inventories/staging - each group vars' do 47 | require 'yaml' 48 | tmp_playbook = 'site.yml' 49 | tmp_inventory_file = 'inventories/staging/hosts' 50 | tmp_vars_dirs_path = 'inventories/staging' 51 | 52 | before do 53 | @current_dir = Dir.pwd() 54 | Dir.chdir('spec/case/get_variable/inventories_env_group_each_vars/') 55 | ENV["PLAYBOOK"] = tmp_playbook 56 | ENV["INVENTORY"] = tmp_inventory_file 57 | ENV["VARS_DIRS_PATH"] = tmp_vars_dirs_path 58 | @res = AnsibleSpec.get_variables('192.168.2.1', 0) 59 | end 60 | 61 | it 'res is hash' do 62 | expect(@res.instance_of?(Hash)).to be_truthy 63 | end 64 | 65 | it 'exist 5 pair in Hash' do 66 | expect(@res.length).to eq 5 67 | end 68 | 69 | it 'exist each pair' do 70 | expect(@res).to include({'role_var' => 'group1'}, 71 | {'site_var' => 'group1'}, 72 | {'host_var' => 'group1'}, 73 | {'group_var' => 'group1'}, 74 | {'group_all_var' => 'group all'} 75 | ) 76 | end 77 | 78 | after do 79 | ENV.delete('PLAYBOOK') 80 | ENV.delete('INVENTORY') 81 | ENV.delete('VARS_DIRS_PATH') 82 | Dir.chdir(@current_dir) 83 | end 84 | end 85 | 86 | context 'Correct operation : env VARS_DIRS_PATH=inventories/staging - host_vars' do 87 | require 'yaml' 88 | tmp_playbook = 'site.yml' 89 | tmp_inventory_file = 'inventories/staging/hosts' 90 | tmp_vars_dirs_path = 'inventories/staging' 91 | 92 | before do 93 | @current_dir = Dir.pwd() 94 | Dir.chdir('spec/case/get_variable/inventories_env_host_vars/') 95 | ENV["PLAYBOOK"] = tmp_playbook 96 | ENV["INVENTORY"] = tmp_inventory_file 97 | ENV["VARS_DIRS_PATH"] = tmp_vars_dirs_path 98 | @res = AnsibleSpec.get_variables('192.168.2.1', 0) 99 | end 100 | 101 | it 'res is hash' do 102 | expect(@res.instance_of?(Hash)).to be_truthy 103 | end 104 | 105 | it 'exist 3 pair in Hash' do 106 | expect(@res.length).to eq 3 107 | end 108 | 109 | it 'exist each pair' do 110 | expect(@res).to include({'role_var' => 'host role_var'}, # in host_var/192.168.2.1.yml 111 | {'site_var' => 'host site_var'}, # in host_var/192.168.2.1.yml 112 | {'host_var' => 'host 192.168.2.1'} # in host_var/192.168.2.1.yml 113 | ) 114 | end 115 | 116 | after do 117 | ENV.delete('PLAYBOOK') 118 | ENV.delete('INVENTORY') 119 | ENV.delete('VARS_DIRS_PATH') 120 | Dir.chdir(@current_dir) 121 | end 122 | end 123 | 124 | context 'Correct operation : env VARS_DIRS_PATH=inventories/staging - host_vars' do 125 | require 'yaml' 126 | tmp_playbook = 'site.yml' 127 | tmp_inventory_file = 'inventories/staging/hosts' 128 | tmp_vars_dirs_path = 'inventories/staging' 129 | 130 | before do 131 | @current_dir = Dir.pwd() 132 | Dir.chdir('spec/case/get_variable/inventories_env_host_vars/') 133 | ENV["PLAYBOOK"] = tmp_playbook 134 | ENV["INVENTORY"] = tmp_inventory_file 135 | ENV["VARS_DIRS_PATH"] = tmp_vars_dirs_path 136 | @res = AnsibleSpec.get_variables('192.168.2.2', 1) 137 | end 138 | 139 | it 'res is hash' do 140 | expect(@res.instance_of?(Hash)).to be_truthy 141 | end 142 | 143 | it 'exist 3 pair in Hash' do 144 | expect(@res.length).to eq 3 145 | end 146 | 147 | it 'exist each pair' do 148 | expect(@res).to include({'role_var' => 'host role_var'}, # in host_var/192.168.2.2.yml 149 | {'site_var' => 'host site_var'}, # in host_var/192.168.2.2.yml 150 | {'host_var' => 'host 192.168.2.2'} # in host_var/192.168.2.2.yml 151 | ) 152 | end 153 | 154 | after do 155 | ENV.delete('PLAYBOOK') 156 | ENV.delete('INVENTORY') 157 | ENV.delete('VARS_DIRS_PATH') 158 | Dir.chdir(@current_dir) 159 | end 160 | end 161 | 162 | context 'Correct operation : .ansiblespec "vars_dirs_path: inventories/staging" - all groups' do 163 | require 'yaml' 164 | tmp_ansiblespec = '.ansiblespec' 165 | 166 | before do 167 | @current_dir = Dir.pwd() 168 | Dir.chdir('spec/case/get_variable/inventories_env_group_all/') 169 | content = <<'EOF' 170 | --- 171 | - 172 | playbook: 'site.yml' 173 | inventory: 'inventories/staging/hosts' 174 | vars_dirs_path: 'inventories/staging' 175 | EOF 176 | create_file(tmp_ansiblespec,content) 177 | @res = AnsibleSpec.get_variables('192.168.2.1', 0) 178 | end 179 | 180 | it 'res is hash' do 181 | expect(@res.instance_of?(Hash)).to be_truthy 182 | end 183 | 184 | it 'exist 5 pair in Hash' do 185 | expect(@res.length).to eq 5 186 | end 187 | 188 | it 'exist each pair' do 189 | expect(@res).to include({'role_var' => 'group all'}, 190 | {'site_var' => 'group all'}, 191 | {'host_var' => 'group all'}, 192 | {'group_var' => 'group all'}, 193 | {'group_all_var' => 'group all'} 194 | ) 195 | end 196 | 197 | after do 198 | File.delete(tmp_ansiblespec) 199 | Dir.chdir(@current_dir) 200 | end 201 | end 202 | 203 | context 'Correct operation : .ansiblespec "vars_dirs_path: inventories/staging" - each group vars' do 204 | require 'yaml' 205 | tmp_ansiblespec = '.ansiblespec' 206 | 207 | before do 208 | @current_dir = Dir.pwd() 209 | Dir.chdir('spec/case/get_variable/inventories_env_group_each_vars/') 210 | content = <<'EOF' 211 | --- 212 | - 213 | playbook: 'site.yml' 214 | inventory: 'inventories/staging/hosts' 215 | vars_dirs_path: 'inventories/staging' 216 | EOF 217 | create_file(tmp_ansiblespec,content) 218 | @res = AnsibleSpec.get_variables('192.168.2.1', 0) 219 | end 220 | 221 | it 'res is hash' do 222 | expect(@res.instance_of?(Hash)).to be_truthy 223 | end 224 | 225 | it 'exist 5 pair in Hash' do 226 | expect(@res.length).to eq 5 227 | end 228 | 229 | it 'exist each pair' do 230 | expect(@res).to include({'role_var' => 'group1'}, 231 | {'site_var' => 'group1'}, 232 | {'host_var' => 'group1'}, 233 | {'group_var' => 'group1'}, 234 | {'group_all_var' => 'group all'} 235 | ) 236 | end 237 | 238 | after do 239 | File.delete(tmp_ansiblespec) 240 | Dir.chdir(@current_dir) 241 | end 242 | end 243 | 244 | context 'Correct operation : .ansiblespec "vars_dirs_path: inventories/staging" - host_vars' do 245 | require 'yaml' 246 | tmp_ansiblespec = '.ansiblespec' 247 | 248 | before do 249 | @current_dir = Dir.pwd() 250 | Dir.chdir('spec/case/get_variable/inventories_env_host_vars/') 251 | content = <<'EOF' 252 | --- 253 | - 254 | playbook: 'site.yml' 255 | inventory: 'inventories/staging/hosts' 256 | vars_dirs_path: 'inventories/staging' 257 | EOF 258 | create_file(tmp_ansiblespec,content) 259 | @res = AnsibleSpec.get_variables('192.168.2.1', 0) 260 | end 261 | 262 | it 'res is hash' do 263 | expect(@res.instance_of?(Hash)).to be_truthy 264 | end 265 | 266 | it 'exist 3 pair in Hash' do 267 | expect(@res.length).to eq 3 268 | end 269 | 270 | it 'exist each pair' do 271 | expect(@res).to include({'role_var' => 'host role_var'}, # in host_var/192.168.2.1.yml 272 | {'site_var' => 'host site_var'}, # in host_var/192.168.2.1.yml 273 | {'host_var' => 'host 192.168.2.1'} # in host_var/192.168.2.1.yml 274 | ) 275 | end 276 | 277 | after do 278 | File.delete(tmp_ansiblespec) 279 | Dir.chdir(@current_dir) 280 | end 281 | end 282 | 283 | context 'Correct operation : .ansiblespec "vars_dirs_path: inventories/staging" - host_vars' do 284 | require 'yaml' 285 | tmp_ansiblespec = '.ansiblespec' 286 | 287 | before do 288 | @current_dir = Dir.pwd() 289 | Dir.chdir('spec/case/get_variable/inventories_env_host_vars/') 290 | content = <<'EOF' 291 | --- 292 | - 293 | playbook: 'site.yml' 294 | inventory: 'inventories/staging/hosts' 295 | vars_dirs_path: 'inventories/staging' 296 | EOF 297 | create_file(tmp_ansiblespec,content) 298 | @res = AnsibleSpec.get_variables('192.168.2.2', 1) 299 | end 300 | 301 | it 'res is hash' do 302 | expect(@res.instance_of?(Hash)).to be_truthy 303 | end 304 | 305 | it 'exist 3 pair in Hash' do 306 | expect(@res.length).to eq 3 307 | end 308 | 309 | it 'exist each pair' do 310 | expect(@res).to include({'role_var' => 'host role_var'}, # in host_var/192.168.2.2.yml 311 | {'site_var' => 'host site_var'}, # in host_var/192.168.2.2.yml 312 | {'host_var' => 'host 192.168.2.2'} # in host_var/192.168.2.2.yml 313 | ) 314 | end 315 | 316 | after do 317 | File.delete(tmp_ansiblespec) 318 | Dir.chdir(@current_dir) 319 | end 320 | end 321 | end 322 | -------------------------------------------------------------------------------- /spec/get_vars_dirs_path_spec.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | require 'fileutils' 3 | require 'ansible_spec' 4 | 5 | def create_file(name,content) 6 | dir = File.dirname(name) 7 | unless File.directory?(dir) 8 | FileUtils.mkdir_p(dir) 9 | end 10 | File.open(name, 'w') do |f| 11 | f.puts content 12 | end 13 | end 14 | 15 | describe "Run get_vars_dirs_path" do 16 | context 'Normal system (environment variable VARS_DIRS_PATH not specified and vars_dirs_path not specified in .ansiblespec)' do 17 | require 'yaml' 18 | expected_default_vars_dirs_path = '' 19 | 20 | before do 21 | @vars_dirs_path = AnsibleSpec.get_vars_dirs_path() 22 | end 23 | 24 | it "vars_dirs_path is '#{expected_default_vars_dirs_path}'" do 25 | expect(@vars_dirs_path).to eq expected_default_vars_dirs_path 26 | end 27 | end 28 | 29 | context 'Normal system (environment variable VARS_DIRS_PATH specified as current directory and vars_dirs_path not specified in .ansiblespec)' do 30 | require 'yaml' 31 | tmp_vars_dirs_path = '' 32 | 33 | before do 34 | ENV['VARS_DIRS_PATH'] = tmp_vars_dirs_path 35 | @vars_dirs_path = AnsibleSpec.get_vars_dirs_path() 36 | end 37 | 38 | it "vars_dirs_path is #{tmp_vars_dirs_path}" do 39 | expect(@vars_dirs_path).to eq tmp_vars_dirs_path 40 | end 41 | 42 | after do 43 | ENV.delete('VARS_DIRS_PATH') 44 | end 45 | end 46 | 47 | context 'Normal system (environment variable VARS_DIRS_PATH specified relative to playbook base directory and vars_dirs_path not specified in .ansiblespec)' do 48 | require 'yaml' 49 | tmp_vars_dirs_path = 'inventories/staging' 50 | 51 | before do 52 | ENV['VARS_DIRS_PATH'] = tmp_vars_dirs_path 53 | @vars_dirs_path = AnsibleSpec.get_vars_dirs_path() 54 | end 55 | 56 | it "vars_dirs_path is #{tmp_vars_dirs_path}" do 57 | expect(@vars_dirs_path).to eq tmp_vars_dirs_path 58 | end 59 | 60 | after do 61 | ENV.delete('VARS_DIRS_PATH') 62 | end 63 | end 64 | 65 | context 'Normal system (environment variable VARS_DIRS_PATH specified as absolute path and vars_dirs_path not specified in .ansiblespec)' do 66 | require 'yaml' 67 | tmp_vars_dirs_path = '/etc/ansible/inventories/staging' 68 | 69 | before do 70 | ENV['VARS_DIRS_PATH'] = tmp_vars_dirs_path 71 | @vars_dirs_path = AnsibleSpec.get_vars_dirs_path() 72 | end 73 | 74 | it "vars_dirs_path is #{tmp_vars_dirs_path}" do 75 | expect(@vars_dirs_path).to eq tmp_vars_dirs_path 76 | end 77 | 78 | after do 79 | ENV.delete('VARS_DIRS_PATH') 80 | end 81 | end 82 | 83 | context 'Normal line (vars_dirs_path specified relative to playbook base directory in .ansiblespec and environment variable VARS_DIRS_PATH not specified)' do 84 | require 'yaml' 85 | tmp_ansiblespec = '.ansiblespec' 86 | tmp_vars_dirs_path = 'inventories/staging' 87 | 88 | before do 89 | 90 | content = <<'EOF' 91 | --- 92 | - 93 | vars_dirs_path: 'inventories/staging' 94 | EOF 95 | create_file(tmp_ansiblespec,content) 96 | @vars_dirs_path = AnsibleSpec.get_vars_dirs_path() 97 | end 98 | 99 | it "vars_dirs_path is #{tmp_vars_dirs_path}" do 100 | expect(@vars_dirs_path).to eq tmp_vars_dirs_path 101 | end 102 | 103 | after do 104 | File.delete(tmp_ansiblespec) 105 | end 106 | end 107 | 108 | context 'Normal line (vars_dirs_path specified as absolute path in .ansiblespec and environment variable VARS_DIRS_PATH not specified)' do 109 | require 'yaml' 110 | tmp_ansiblespec = '.ansiblespec' 111 | tmp_vars_dirs_path = '/etc/ansible/inventories/staging' 112 | 113 | before do 114 | 115 | content = <<'EOF' 116 | --- 117 | - 118 | vars_dirs_path: '/etc/ansible/inventories/staging' 119 | EOF 120 | create_file(tmp_ansiblespec,content) 121 | @vars_dirs_path = AnsibleSpec.get_vars_dirs_path() 122 | end 123 | 124 | it "vars_dirs_path is #{tmp_vars_dirs_path}" do 125 | expect(@vars_dirs_path).to eq tmp_vars_dirs_path 126 | end 127 | 128 | after do 129 | File.delete(tmp_ansiblespec) 130 | end 131 | end 132 | 133 | context 'Normal line (ENV VAR higher precedence check: vars_dirs_path specified in .ansiblespec and environment variable VARS_DIRS_PATH specified)' do 134 | require 'yaml' 135 | tmp_ansiblespec = '.ansiblespec' 136 | tmp_vars_dirs_path = 'inventories/staging' 137 | 138 | before do 139 | ENV['VARS_DIRS_PATH'] = tmp_vars_dirs_path 140 | content = <<'EOF' 141 | --- 142 | - 143 | vars_dirs_path: 'inventories/development' 144 | EOF 145 | create_file(tmp_ansiblespec,content) 146 | @vars_dirs_path = AnsibleSpec.get_vars_dirs_path() 147 | end 148 | 149 | it "vars_dirs_path is #{tmp_vars_dirs_path}" do 150 | expect(@vars_dirs_path).to eq tmp_vars_dirs_path 151 | end 152 | 153 | after do 154 | File.delete(tmp_ansiblespec) 155 | end 156 | end 157 | end 158 | -------------------------------------------------------------------------------- /spec/inventory_parameters_spec.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | require 'ansible_spec' 3 | 4 | def create_file(name,content) 5 | File.open(name, 'w') do |f| 6 | f.puts content 7 | end 8 | end 9 | 10 | def ready_test 11 | tmp_hosts = 'hosts' 12 | tmp_ansiblespec = '.ansiblespec' 13 | tmp_playbook = 'site.yml' 14 | 15 | content = <<'EOF' 16 | --- 17 | - 18 | playbook: site.yml 19 | inventory: hosts 20 | EOF 21 | 22 | content_p = <<'EOF' 23 | - name: Ansible-Sample-TDD 24 | hosts: normal 25 | user: root 26 | roles: 27 | - nginx 28 | - mariadb 29 | EOF 30 | 31 | content_h = <<'EOF' 32 | node ansible_ssh_port=4444 ansible_ssh_host=192.168.1.55 33 | node1 ansible_port=4444 ansible_host=192.168.1.55 34 | 35 | [normal] 36 | 192.168.0.1 37 | 192.168.0.2 ansible_ssh_port=22 38 | 192.168.0.3:5309 39 | 192.168.0.4 ansible_ssh_private_key_file=~/.ssh/id_rsa 40 | 192.168.0.5 ansible_ssh_user=git 41 | jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50 42 | node 43 | # Ansible 2.0 44 | 192.168.10.2 ansible_port=2222 45 | 192.168.10.5 ansible_user=git 46 | jumper2 ansible_port=5555 ansible_host=192.168.10.50 47 | node1 48 | EOF 49 | 50 | create_file(tmp_ansiblespec,content) 51 | create_file(tmp_playbook,content_p) 52 | create_file(tmp_hosts,content_h) 53 | end 54 | 55 | describe "load_targetsの実行" do 56 | context '正常系:複数グループ:変数' do 57 | tmp_hosts = 'hosts' 58 | before do 59 | ready_test 60 | @res = AnsibleSpec.load_targets(tmp_hosts) 61 | end 62 | 63 | it 'res is hash' do 64 | expect(@res.instance_of?(Hash)).to be_truthy 65 | end 66 | 67 | it 'exist 1 group' do 68 | expect(@res.length).to eq 1 69 | end 70 | 71 | it 'exist group' do 72 | expect(@res.key?('normal')).to be_truthy 73 | end 74 | 75 | it 'normal 192.168.0.1' do 76 | obj = @res['normal'][0] 77 | expect(obj.instance_of?(Hash)).to be_truthy 78 | expect(obj['uri']).to eq '192.168.0.1' 79 | expect(obj['port']).to eq 22 80 | end 81 | it 'normal 192.168.0.2 ansible_ssh_port=22' do 82 | obj = @res['normal'][1] 83 | expect(obj.instance_of?(Hash)).to be_truthy 84 | expect(obj['uri']).to eq '192.168.0.2' 85 | expect(obj['port']).to eq 22 86 | end 87 | it 'normal 192.168.0.3:5309' do 88 | obj = @res['normal'][2] 89 | expect(obj.instance_of?(Hash)).to be_truthy 90 | expect(obj['uri']).to eq '192.168.0.3' 91 | expect(obj['port']).to eq 5309 92 | end 93 | it '192.168.0.4 ansible_ssh_private_key_file=~/.ssh/id_rsa' do 94 | obj = @res['normal'][3] 95 | expect(obj.instance_of?(Hash)).to be_truthy 96 | expect(obj['uri']).to eq '192.168.0.4' 97 | expect(obj['port']).to eq 22 98 | expect(obj['private_key']).to eq '~/.ssh/id_rsa' 99 | end 100 | 101 | it '192.168.0.5 ansible_ssh_user=git' do 102 | obj = @res['normal'][4] 103 | expect(obj.instance_of?(Hash)).to be_truthy 104 | expect(obj['uri']).to eq '192.168.0.5' 105 | expect(obj['port']).to eq 22 106 | expect(obj['user']).to eq 'git' 107 | end 108 | 109 | it 'jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50' do 110 | obj = @res['normal'][5] 111 | expect(obj.instance_of?(Hash)).to be_truthy 112 | expect(obj['name']).to eq 'jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50' 113 | expect(obj['uri']).to eq '192.168.1.50' 114 | expect(obj['port']).to eq 5555 115 | end 116 | 117 | it 'node ansible_ssh_port=4444 ansible_ssh_host=192.168.1.55' do 118 | obj = @res['normal'][6] 119 | expect(obj.instance_of?(Hash)).to be_truthy 120 | expect(obj['name']).to eq 'node ansible_ssh_port=4444 ansible_ssh_host=192.168.1.55' 121 | expect(obj['uri']).to eq '192.168.1.55' 122 | expect(obj['port']).to eq 4444 123 | end 124 | 125 | it '192.168.10.2 ansible_port=2222' do 126 | obj = @res['normal'][7] 127 | expect(obj.instance_of?(Hash)).to be_truthy 128 | expect(obj['uri']).to eq '192.168.10.2' 129 | expect(obj['port']).to eq 2222 130 | end 131 | 132 | it '192.168.10.5 ansible_user=git' do 133 | obj = @res['normal'][8] 134 | expect(obj.instance_of?(Hash)).to be_truthy 135 | expect(obj['uri']).to eq '192.168.10.5' 136 | expect(obj['user']).to eq 'git' 137 | end 138 | 139 | it 'jumper2 ansible_port=5555 ansible_host=192.168.10.50' do 140 | obj = @res['normal'][9] 141 | expect(obj.instance_of?(Hash)).to be_truthy 142 | expect(obj['name']).to eq 'jumper2 ansible_port=5555 ansible_host=192.168.10.50' 143 | expect(obj['uri']).to eq '192.168.10.50' 144 | expect(obj['port']).to eq 5555 145 | end 146 | 147 | it 'node1 ansible_port=4444 ansible_host=192.168.1.55' do 148 | obj = @res['normal'][10] 149 | expect(obj.instance_of?(Hash)).to be_truthy 150 | expect(obj['name']).to eq 'node1 ansible_port=4444 ansible_host=192.168.1.55' 151 | expect(obj['uri']).to eq '192.168.1.55' 152 | expect(obj['port']).to eq 4444 153 | end 154 | after do 155 | File.delete(tmp_hosts) 156 | end 157 | end 158 | end 159 | 160 | describe "get_propertiesの実行" do 161 | require 'yaml' 162 | tmp_ansiblespec = '.ansiblespec' 163 | tmp_playbook = 'site.yml' 164 | tmp_hosts = 'hosts' 165 | 166 | before do 167 | ready_test 168 | @res = AnsibleSpec.get_properties 169 | end 170 | 171 | context '正常系' do 172 | it 'res is array' do 173 | expect(@res.instance_of?(Array)).to be_truthy 174 | end 175 | 176 | it 'res[0] is hash' do 177 | expect(@res[0].instance_of?(Hash)).to be_truthy 178 | end 179 | 180 | it 'check 6 group' do 181 | expect(@res[0].length).to eq 6 182 | end 183 | 184 | it 'exist name' do 185 | expect(@res[0].key?('name')).to be_truthy 186 | expect(@res[0]['name']).to eq 'Ansible-Sample-TDD' 187 | end 188 | 189 | it 'normal 192.168.0.1' do 190 | expect(@res[0]['hosts'].instance_of?(Array)).to be_truthy 191 | expect(@res[0]['hosts'][0]).to include({'name' => '192.168.0.1', 192 | 'uri' => '192.168.0.1', 193 | 'port' => 22}) 194 | end 195 | 196 | it 'normal 192.168.0.2 ansible_ssh_port=22' do 197 | obj = @res[0]['hosts'][1] 198 | expect(obj.instance_of?(Hash)).to be_truthy 199 | expect(obj['uri']).to eq '192.168.0.2' 200 | expect(obj['port']).to eq 22 201 | end 202 | 203 | it 'normal 192.168.0.3:5309' do 204 | obj = @res[0]['hosts'][2] 205 | expect(obj.instance_of?(Hash)).to be_truthy 206 | expect(obj['uri']).to eq '192.168.0.3' 207 | expect(obj['port']).to eq 5309 208 | end 209 | it 'normal 192.168.0.4 ansible_ssh_private_key_file=~/.ssh/id_rsa' do 210 | obj = @res[0]['hosts'][3] 211 | expect(obj.instance_of?(Hash)).to be_truthy 212 | expect(obj['uri']).to eq '192.168.0.4' 213 | expect(obj['port']).to eq 22 214 | expect(obj['private_key']).to eq '~/.ssh/id_rsa' 215 | end 216 | 217 | it '192.168.0.5 ansible_ssh_user=git' do 218 | obj = @res[0]['hosts'][4] 219 | expect(obj.instance_of?(Hash)).to be_truthy 220 | expect(obj['uri']).to eq '192.168.0.5' 221 | expect(obj['port']).to eq 22 222 | expect(obj['user']).to eq 'git' 223 | end 224 | 225 | it 'jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50' do 226 | obj = @res[0]['hosts'][5] 227 | expect(obj.instance_of?(Hash)).to be_truthy 228 | expect(obj['name']).to eq 'jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50' 229 | expect(obj['uri']).to eq '192.168.1.50' 230 | expect(obj['port']).to eq 5555 231 | end 232 | 233 | it 'exist user' do 234 | expect(@res[0].key?('user')).to be_truthy 235 | expect(@res[0]['user']).to eq 'root' 236 | end 237 | 238 | it 'exist roles' do 239 | expect(@res[0].key?('roles')).to be_truthy 240 | expect(@res[0]['roles'].instance_of?(Array)).to be_truthy 241 | expect(@res[0]['roles'][0]).to eq 'nginx' 242 | expect(@res[0]['roles'][1]).to eq 'mariadb' 243 | end 244 | 245 | after do 246 | File.delete(tmp_ansiblespec) 247 | File.delete(tmp_playbook) 248 | File.delete(tmp_hosts) 249 | end 250 | end 251 | end 252 | -------------------------------------------------------------------------------- /spec/load_ansible_cfg_spec.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | require 'fileutils' 3 | require 'ansible_spec' 4 | 5 | def create_file(name,content) 6 | dir = File.dirname(name) 7 | unless File.directory?(dir) 8 | FileUtils.mkdir_p(dir) 9 | end 10 | File.open(name, 'w') do |f| 11 | f.puts content 12 | end 13 | end 14 | 15 | describe "load_ansible_cfg" do 16 | context 'with ANSIBLE_CFG set' do 17 | tmp_cfg = 'tmp_ansible.cfg' 18 | saved_env = nil 19 | 20 | before do 21 | content = <<'EOF' 22 | [ansible_spec1] 23 | roles_path = roles_path1:roles_path2 24 | EOF 25 | create_file(tmp_cfg,content) 26 | saved_env = ENV['ANSIBLE_CFG'] 27 | ENV['ANSIBLE_CFG'] = tmp_cfg 28 | @res = AnsibleSpec::AnsibleCfg.load_ansible_cfg() 29 | end 30 | 31 | it 'res is hash' do 32 | expect(@res.instance_of?(Hash)).to be_truthy 33 | end 34 | 35 | it 'res has section' do 36 | expect(@res).to include('ansible_spec1') 37 | end 38 | 39 | it 'section has roles_path' do 40 | expect(@res['ansible_spec1']).to include('roles_path') 41 | end 42 | 43 | it 'roles_path is set' do 44 | expect(@res['ansible_spec1']['roles_path']).to eq('roles_path1:roles_path2') 45 | end 46 | 47 | after do 48 | File.delete(tmp_cfg) 49 | ENV['ANSIBLE_CFG'] = saved_env 50 | end 51 | end 52 | end 53 | 54 | describe "AnsibleCfg" do 55 | context 'with ANSIBLE_CFG set' do 56 | tmp_cfg = 'tmp_ansible.cfg' 57 | saved_env = nil 58 | 59 | before do 60 | content = <<'EOF' 61 | [ansible_spec1] 62 | roles_path = roles_path1:roles_path2 63 | EOF 64 | create_file(tmp_cfg,content) 65 | saved_env = ENV['ANSIBLE_CFG'] 66 | ENV['ANSIBLE_CFG'] = tmp_cfg 67 | @cfg = AnsibleSpec::AnsibleCfg.new 68 | end 69 | 70 | it 'elem handles unknown sections' do 71 | expect(@cfg.get('doesnot', 'exist')).to be_nil 72 | end 73 | 74 | it 'elem handles unknown keys' do 75 | expect(@cfg.get('ansible_spec1', 'doesnot_exist')).to be_nil 76 | end 77 | 78 | it 'elem handles known eys ' do 79 | expect(@cfg.get('ansible_spec1', 'roles_path')).to be_truthy 80 | end 81 | 82 | it 'cfg has a roles path' do 83 | expect(@cfg.roles_path).to be_truthy 84 | end 85 | 86 | after do 87 | File.delete(tmp_cfg) 88 | ENV['ANSIBLE_CFG'] = saved_env 89 | end 90 | end 91 | end 92 | -------------------------------------------------------------------------------- /spec/load_ansible_cfg_ssh_spec.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | require 'fileutils' 3 | require 'ansible_spec' 4 | 5 | def create_file(name,content) 6 | dir = File.dirname(name) 7 | unless File.directory?(dir) 8 | FileUtils.mkdir_p(dir) 9 | end 10 | File.open(name, 'w') do |f| 11 | f.puts content 12 | end 13 | end 14 | 15 | describe "get_ssh_config_file" do 16 | context 'with SSH_CONFIG_FILE set' do 17 | tmp_cfg = 'tmp_ansible.cfg' 18 | tmp_ssh_cfg = 'hoge_config' 19 | tmp_env_ssh_cfg = 'env_config' 20 | saved_env = nil 21 | 22 | before do 23 | content = <<'EOF' 24 | [ssh_connection] 25 | ssh_args = -F hoge_config 26 | EOF 27 | content_ssh = <<'EOF' 28 | EOF 29 | create_file(tmp_cfg,content) 30 | create_file(tmp_env_ssh_cfg,content_ssh) 31 | saved_env = ENV['SSH_CONFIG_FILE'] 32 | ENV['SSH_CONFIG_FILE'] = tmp_env_ssh_cfg 33 | @res = AnsibleSpec.get_ssh_config_file() 34 | end 35 | 36 | it 'res is hash' do 37 | expect(@res.instance_of?(String)).to be_truthy 38 | end 39 | 40 | it 'res has section' do 41 | expect(@res).to eq('env_config') 42 | end 43 | 44 | after do 45 | File.delete(tmp_cfg) 46 | File.delete(tmp_env_ssh_cfg) 47 | ENV['SSH_CONFIG_FILE'] = saved_env 48 | end 49 | end 50 | 51 | context 'without SSH_CONFIG_FILE set' do 52 | tmp_cfg = 'tmp_ansible.cfg' 53 | tmp_ssh_cfg = 'hoge_config' 54 | saved_env = nil 55 | saved_env_ansible = nil 56 | 57 | before do 58 | content = <<'EOF' 59 | [ssh_connection] 60 | ssh_args = -F hoge_config 61 | EOF 62 | content_ssh = <<'EOF' 63 | EOF 64 | create_file(tmp_cfg,content) 65 | saved_env_ansible = ENV['ANSIBLE_CFG'] 66 | ENV['ANSIBLE_CFG'] = tmp_cfg 67 | 68 | create_file(tmp_ssh_cfg,content_ssh) 69 | 70 | saved_env = ENV['SSH_CONFIG_FILE'] 71 | ENV['SSH_CONFIG_FILE'] = nil 72 | @res = AnsibleSpec.get_ssh_config_file() 73 | end 74 | 75 | it 'res is hash' do 76 | expect(@res.instance_of?(String)).to be_truthy 77 | end 78 | 79 | it 'res has section' do 80 | expect(@res).to eq('hoge_config') 81 | end 82 | 83 | after do 84 | File.delete(tmp_cfg) 85 | File.delete(tmp_ssh_cfg) 86 | ENV['ANSIBLE_CFG'] = saved_env_ansible 87 | ENV['SSH_CONFIG_FILE'] = saved_env 88 | end 89 | end 90 | 91 | context 'not set -F at ANSIBLE_CFG' do 92 | tmp_cfg = 'tmp_ansible.cfg' 93 | saved_env = nil 94 | saved_env_ansible = nil 95 | 96 | before do 97 | content = <<'EOF' 98 | [ssh_connection] 99 | ssh_args = -o ControlMaster=auto 100 | EOF 101 | content_ssh = <<'EOF' 102 | EOF 103 | create_file(tmp_cfg,content) 104 | saved_env_ansible = ENV['ANSIBLE_CFG'] 105 | ENV['ANSIBLE_CFG'] = tmp_cfg 106 | 107 | saved_env = ENV['SSH_CONFIG_FILE'] 108 | ENV['SSH_CONFIG_FILE'] = nil 109 | @res = AnsibleSpec.get_ssh_config_file() 110 | end 111 | 112 | it 'res has section' do 113 | expect(@res).to eq nil 114 | end 115 | 116 | after do 117 | File.delete(tmp_cfg) 118 | ENV['SSH_CONFIG_FILE'] = saved_env 119 | ENV['ANSIBLE_CFG'] = saved_env_ansible 120 | end 121 | end 122 | 123 | context 'not exist SSH_CONFIG_FILE' do 124 | tmp_cfg = 'tmp_ansible.cfg' 125 | saved_env = nil 126 | saved_env_ansible = nil 127 | 128 | before do 129 | content = <<'EOF' 130 | [ssh_connection] 131 | ssh_args = -F hoge_config 132 | EOF 133 | create_file(tmp_cfg,content) 134 | saved_env = ENV['SSH_CONFIG_FILE'] 135 | ENV['SSH_CONFIG_FILE'] = nil 136 | saved_env_ansible = ENV['ANSIBLE_CFG'] 137 | ENV['ANSIBLE_CFG'] = tmp_cfg 138 | @res = AnsibleSpec.get_ssh_config_file() 139 | end 140 | 141 | it 'res has section' do 142 | expect(@res).to eq nil 143 | end 144 | 145 | after do 146 | File.delete(tmp_cfg) 147 | ENV['SSH_CONFIG_FILE'] = saved_env 148 | ENV['ANSIBLE_CFG'] = saved_env_ansible 149 | end 150 | end 151 | end 152 | 153 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require 'specinfra' 2 | require 'rspec/mocks/standalone' 3 | require 'specinfra/helper/set' 4 | require 'simplecov' 5 | include Specinfra::Helper::Set 6 | 7 | # Test Coverage 8 | SimpleCov.start 9 | # This file was generated by the `rspec --init` command. Conventionally, all 10 | # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. 11 | # The generated `.rspec` file contains `--require spec_helper` which will cause this 12 | # file to always be loaded, without a need to explicitly require it in any files. 13 | # 14 | # Given that it is always loaded, you are encouraged to keep this file as 15 | # light-weight as possible. Requiring heavyweight dependencies from this file 16 | # will add to the boot time of your test suite on EVERY test run, even for an 17 | # individual file that may not need all of that loaded. Instead, consider making 18 | # a separate helper file that requires the additional dependencies and performs 19 | # the additional setup, and require it from the spec files that actually need it. 20 | # 21 | # The `.rspec` file also contains a few flags that are not defaults but that 22 | # users commonly want. 23 | # 24 | # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration 25 | RSpec.configure do |config| 26 | # rspec-expectations config goes here. You can use an alternate 27 | # assertion/expectation library such as wrong or the stdlib/minitest 28 | # assertions if you prefer. 29 | config.expect_with :rspec do |expectations| 30 | # This option will default to `true` in RSpec 4. It makes the `description` 31 | # and `failure_message` of custom matchers include text for helper methods 32 | # defined using `chain`, e.g.: 33 | # be_bigger_than(2).and_smaller_than(4).description 34 | # # => "be bigger than 2 and smaller than 4" 35 | # ...rather than: 36 | # # => "be bigger than 2" 37 | expectations.include_chain_clauses_in_custom_matcher_descriptions = true 38 | end 39 | 40 | # rspec-mocks config goes here. You can use an alternate test double 41 | # library (such as bogus or mocha) by changing the `mock_with` option here. 42 | config.mock_with :rspec do |mocks| 43 | # Prevents you from mocking or stubbing a method that does not exist on 44 | # a real object. This is generally recommended, and will default to 45 | # `true` in RSpec 4. 46 | mocks.verify_partial_doubles = true 47 | end 48 | 49 | # The settings below are suggested to provide a good initial experience 50 | # with RSpec, but feel free to customize to your heart's content. 51 | =begin 52 | # These two settings work together to allow you to limit a spec run 53 | # to individual examples or groups you care about by tagging them with 54 | # `:focus` metadata. When nothing is tagged with `:focus`, all examples 55 | # get run. 56 | config.filter_run :focus 57 | config.run_all_when_everything_filtered = true 58 | 59 | # Limits the available syntax to the non-monkey patched syntax that is recommended. 60 | # For more details, see: 61 | # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax 62 | # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ 63 | # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching 64 | config.disable_monkey_patching! 65 | 66 | # This setting enables warnings. It's recommended, but in some cases may 67 | # be too noisy due to issues in dependencies. 68 | config.warnings = true 69 | 70 | # Many RSpec users commonly either run the entire suite or an individual 71 | # file, and it's useful to allow more verbose output when running an 72 | # individual spec file. 73 | if config.files_to_run.one? 74 | # Use the documentation formatter for detailed output, 75 | # unless a formatter has already been configured 76 | # (e.g. via a command-line flag). 77 | config.default_formatter = 'doc' 78 | end 79 | 80 | # Print the 10 slowest examples and example groups at the 81 | # end of the spec run, to help surface which specs are running 82 | # particularly slow. 83 | config.profile_examples = 10 84 | 85 | # Run specs in random order to surface order dependencies. If you find an 86 | # order dependency and want to debug it, you can fix the order by providing 87 | # the seed, which is printed after each run. 88 | # --seed 1234 89 | config.order = :random 90 | 91 | # Seed global randomization in this process using the `--seed` CLI option. 92 | # Setting this allows you to use `--seed` to deterministically reproduce 93 | # test failures related to randomization by passing the same `--seed` value 94 | # as the one that triggered the failure. 95 | Kernel.srand config.seed 96 | =end 97 | end 98 | -------------------------------------------------------------------------------- /spec/ssh_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | require 'ansible_spec' 3 | require 'yaml' 4 | 5 | set :backend, :ssh 6 | 7 | describe 'ssh' do 8 | context 'with root user' do 9 | before do 10 | create_normality 11 | properties = AnsibleSpec.get_properties 12 | @h = Hash.new 13 | n = 0 14 | properties.each do |property| 15 | property["hosts"].each do |host| 16 | @ssh = set_ssh(property, host) 17 | @h["task_#{n}"] = @ssh 18 | n += 1 19 | end 20 | end 21 | end 22 | 23 | it '192.168.0.1' do 24 | v = @h["task_0"] 25 | expect(v.user).to eq 'root' 26 | expect(v.host).to eq '192.168.0.1' 27 | end 28 | 29 | it '192.168.0.2:22' do 30 | v = @h["task_1"] 31 | expect(v.user).to eq 'root' 32 | expect(v.host).to eq '192.168.0.2' 33 | expect(v.port).to eq '22' 34 | end 35 | it '192.168.0.3:5309' do 36 | v = @h["task_2"] 37 | expect(v.user).to eq 'root' 38 | expect(v.host).to eq '192.168.0.3' 39 | expect(v.port).to eq '5309' 40 | end 41 | it '192.168.0.4 ansible_ssh_private_key_file=~/.ssh/id_rsa' do 42 | v = @h["task_3"] 43 | expect(v.user).to eq 'root' 44 | expect(v.host).to eq '192.168.0.4' 45 | expect(v.port).to eq '22' 46 | expect(v.keys).to eq '~/.ssh/id_rsa' 47 | end 48 | 49 | it '192.168.0.5 ansible_ssh_user=git' do 50 | v = @h["task_4"] 51 | expect(v.user).to eq 'git' 52 | expect(v.host).to eq '192.168.0.5' 53 | expect(v.port).to eq '22' 54 | end 55 | 56 | it 'jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50' do 57 | v = @h["task_5"] 58 | expect(v.user).to eq 'root' 59 | expect(v.host).to eq '192.168.1.50' 60 | expect(v.port).to eq '5555' 61 | end 62 | 63 | it 'www[01:02].example.com' do 64 | v = @h["task_6"] 65 | expect(v.user).to eq 'root' 66 | expect(v.host).to eq 'www01.example.com' 67 | end 68 | 69 | it 'www[01:02].example.com' do 70 | v = @h["task_7"] 71 | expect(v.user).to eq 'root' 72 | expect(v.host).to eq 'www02.example.com' 73 | end 74 | 75 | it 'db-[a:b].example.com' do 76 | v = @h["task_8"] 77 | expect(v.user).to eq 'root' 78 | expect(v.host).to eq 'db-a.example.com' 79 | end 80 | 81 | it 'db-[a:b].example.com' do 82 | v = @h["task_9"] 83 | expect(v.user).to eq 'root' 84 | expect(v.host).to eq 'db-b.example.com' 85 | end 86 | 87 | it '192.168.1.3 ansible_connection=winrm ansible_ssh_port=5985 ansible_ssh_user=administrator ansible_ssh_pass=Passw0rd' do 88 | v = @h["task_10"] 89 | expect(v.host).to eq '192.168.1.3' 90 | expect(v.user).to eq 'administrator' 91 | expect(v.port).to eq '5985' 92 | end 93 | 94 | it '192.168.10.2 ansible_port=2222' do 95 | v = @h["task_11"] 96 | expect(v.user).to eq 'root' 97 | expect(v.host).to eq '192.168.10.2' 98 | expect(v.port).to eq '2222' 99 | end 100 | 101 | it '192.168.10.5 ansible_user=git' do 102 | v = @h["task_12"] 103 | expect(v.user).to eq 'git' 104 | expect(v.host).to eq '192.168.10.5' 105 | expect(v.port).to eq '22' 106 | end 107 | 108 | it 'jumper2 ansible_port=5555 ansible_host=192.168.10.50' do 109 | v = @h["task_13"] 110 | expect(v.user).to eq 'root' 111 | expect(v.host).to eq '192.168.10.50' 112 | expect(v.port).to eq '5555' 113 | end 114 | 115 | after do 116 | delete_normality 117 | end 118 | end 119 | end 120 | 121 | 122 | describe 'ssh with dynamic inventory' do 123 | context 'ssh with' do 124 | before do 125 | create_dynamic_inventory 126 | properties = AnsibleSpec.get_properties 127 | @h = Hash.new 128 | n = 0 129 | properties.each do |property| 130 | property["hosts"].each do |host| 131 | #t.pattern = 'roles/{' + property["roles"].join(',') + '}/spec/*_spec.rb' 132 | @ssh = set_ssh(property, host) 133 | @h["task_#{n}"] = @ssh 134 | n += 1 135 | end 136 | end 137 | end 138 | 139 | it 'tag_Name_sample_app' do 140 | v = @h["task_0"] 141 | expect(v.user).to eq 'root' 142 | expect(v.host).to eq '54.1.2.3' 143 | expect(v.port).to eq '22' 144 | end 145 | 146 | after do 147 | delete_normality 148 | end 149 | 150 | end 151 | end 152 | 153 | # summary: lib/src/Rakefile 154 | def set_ssh(property, host) 155 | @ssh = double(:ssh) 156 | set :host, host["uri"] 157 | unless host["user"].nil? 158 | user = host["user"] 159 | else 160 | user = property["user"] 161 | end 162 | set :ssh_options, :user => user, :port => host["port"].to_s, :keys => host["private_key"] 163 | allow(@ssh).to receive(:host).and_return(Specinfra.configuration.host) 164 | allow(@ssh).to receive(:user).and_return(Specinfra.configuration.ssh_options[:user]) 165 | allow(@ssh).to receive(:port).and_return(Specinfra.configuration.ssh_options[:port]) 166 | allow(@ssh).to receive(:keys).and_return(Specinfra.configuration.ssh_options[:keys]) 167 | return @ssh 168 | end 169 | 170 | def create_normality 171 | tmp_ansiblespec = '.ansiblespec' 172 | tmp_playbook = 'site.yml' 173 | tmp_hosts = 'hosts' 174 | 175 | content = <<'EOF' 176 | --- 177 | - 178 | playbook: site.yml 179 | inventory: hosts 180 | EOF 181 | 182 | content_p = <<'EOF' 183 | - name: Ansible-Sample-TDD 184 | hosts: normal 185 | user: root 186 | roles: 187 | - nginx 188 | - mariadb 189 | EOF 190 | 191 | content_h = <<'EOF' 192 | [normal] 193 | 192.168.0.1 194 | 192.168.0.2 ansible_ssh_port=22 195 | 192.168.0.3:5309 196 | 192.168.0.4 ansible_ssh_private_key_file=~/.ssh/id_rsa 197 | 192.168.0.5 ansible_ssh_user=git 198 | jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50 199 | www[01:02].example.com 200 | db-[a:b].example.com 201 | 192.168.1.3 ansible_connection=winrm ansible_ssh_port=5985 ansible_ssh_user=administrator ansible_ssh_pass=Passw0rd 202 | # Ansible 2.0 203 | 192.168.10.2 ansible_port=2222 204 | 192.168.10.5 ansible_user=git 205 | jumper2 ansible_port=5555 ansible_host=192.168.10.50 206 | EOF 207 | 208 | File.open(tmp_ansiblespec, 'w') do |f| 209 | f.puts content 210 | end 211 | File.open(tmp_playbook, 'w') do |f| 212 | f.puts content_p 213 | end 214 | File.open(tmp_hosts, 'w') do |f| 215 | f.puts content_h 216 | end 217 | end 218 | 219 | def create_dynamic_inventory 220 | tmp_ansiblespec = '.ansiblespec' 221 | tmp_playbook = 'site.yml' 222 | tmp_hosts = 'hosts' 223 | 224 | content = <<'EOF' 225 | --- 226 | - 227 | playbook: site.yml 228 | inventory: hosts 229 | EOF 230 | 231 | content_p = <<'EOF' 232 | - name: Ansible-Sample-TDD 233 | hosts: tag_Name_sample_app 234 | user: root 235 | roles: 236 | - nginx 237 | - mariadb 238 | EOF 239 | 240 | content_h = <<'EOF' 241 | #!/bin/bash 242 | echo '{ "_meta" : {"hostvars": {"54.1.2.3": {"ec2_ip_address": "54.1.2.3","ec2_key_name": "my-secret-key", "ec2_launch_time": "2016-01-06T03:59:56.000Z", "ec2_tag_Name": "sample-app", "ec2_tag_Stack": "sample-app"}}},"tag_Name_sample_app": ["54.1.2.3"]}' 243 | EOF 244 | 245 | File.open(tmp_ansiblespec, 'w') do |f| 246 | f.puts content 247 | end 248 | File.open(tmp_playbook, 'w') do |f| 249 | f.puts content_p 250 | end 251 | File.open(tmp_hosts, 'w') do |f| 252 | f.puts content_h 253 | end 254 | File.chmod(0755,tmp_hosts) 255 | end 256 | 257 | 258 | def delete_normality 259 | tmp_ansiblespec = '.ansiblespec' 260 | tmp_playbook = 'site.yml' 261 | tmp_hosts = 'hosts' 262 | File.delete(tmp_ansiblespec) 263 | File.delete(tmp_playbook) 264 | File.delete(tmp_hosts) 265 | end 266 | --------------------------------------------------------------------------------