playbook_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package ansiblev2
  15. import (
  16. "testing"
  17. )
  18. func TestPlaybookString(t *testing.T) {
  19. play := NewPlay(
  20. &Task{
  21. Name: "Enable ip_forward",
  22. ModuleName: "sysctl",
  23. ModuleArgs: map[string]interface{}{
  24. "name": "net.ipv4.ip_forward",
  25. "value": "1",
  26. "state": "present",
  27. "reload": "yes",
  28. },
  29. },
  30. &Task{
  31. Name: "Enable EPEL",
  32. ModuleName: "package",
  33. ModuleArgs: map[string]interface{}{
  34. "name": "epel-release",
  35. "state": "present",
  36. },
  37. When: `ansible_distribution != "Fedora"`,
  38. },
  39. &Task{
  40. Name: "Install wireguard packages",
  41. ModuleName: "package",
  42. ModuleArgs: map[string]interface{}{
  43. "name": "{{ item }}",
  44. "state": "present",
  45. },
  46. WithPlugin: "items",
  47. WithPluginVal: []interface{}{"wireguard-dkms", "wireguard-tools"},
  48. },
  49. &Task{
  50. Name: "Create /etc/wireguard",
  51. ModuleName: "file",
  52. ModuleArgs: map[string]interface{}{
  53. "path": "/etc/wireguard",
  54. "staet": "directory",
  55. "owner": "root",
  56. "group": "root",
  57. },
  58. },
  59. &IncludeRole{
  60. Name: "foo_app_instance",
  61. Vars: map[string]interface{}{
  62. "dir": "/opt/a",
  63. },
  64. Tags: []string{"bar", "baz"},
  65. When: "ansible_facts['os_family'] == 'RedHat'",
  66. },
  67. )
  68. play.Hosts = "all"
  69. configureBlock := NewBlock(
  70. &Task{
  71. Name: "Configure {{ item }}",
  72. ModuleName: "template",
  73. ModuleArgs: map[string]interface{}{
  74. "src": "wgX.conf.j2", //XXX
  75. "dest": "/etc/wireguard/{{ item }}.conf",
  76. "mode": 0600,
  77. },
  78. Register: "configuration",
  79. },
  80. &Task{
  81. Name: "Enable wg-quick@{{ item }} service",
  82. ModuleName: "service",
  83. ModuleArgs: map[string]interface{}{
  84. "name": "wg-quick@{{ item }}",
  85. "state": "started",
  86. "enabled": "yes",
  87. },
  88. },
  89. &Task{
  90. Name: "Restart wg-quick@{{ item }} service",
  91. ModuleName: "service",
  92. ModuleArgs: map[string]interface{}{
  93. "name": "wg-quick@{{ item }}",
  94. "state": "restarted",
  95. },
  96. When: "configuration is changed",
  97. },
  98. )
  99. configureBlock.Name = "Configure wireguard networks"
  100. play.Tasks = append(play.Tasks, configureBlock)
  101. pb := NewPlaybook(play)
  102. t.Logf("\n%s", pb.String())
  103. }