You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

create.yml 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. ---
  2. - name: Create
  3. hosts: localhost
  4. connection: local
  5. gather_facts: false
  6. no_log: "{{ not (lookup('env', 'MOLECULE_DEBUG') | bool or molecule_yml.provisioner.log|default(false) | bool) }}"
  7. vars:
  8. ssh_user: centos
  9. ssh_port: 22
  10. security_group_name: molecule
  11. security_group_description: Security group for testing Molecule
  12. security_group_rules:
  13. - proto: tcp
  14. from_port: "{{ ssh_port }}"
  15. to_port: "{{ ssh_port }}"
  16. cidr_ip: '0.0.0.0/0'
  17. - proto: icmp
  18. from_port: 8
  19. to_port: -1
  20. cidr_ip: '0.0.0.0/0'
  21. security_group_rules_egress:
  22. - proto: -1
  23. from_port: 0
  24. to_port: 0
  25. cidr_ip: '0.0.0.0/0'
  26. keypair_name: molecule_key_docker_engine
  27. keypair_path: "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}/ssh_key"
  28. tasks:
  29. - name: Create security group
  30. ec2_group:
  31. name: "{{ security_group_name }}"
  32. description: "{{ security_group_name }}"
  33. rules: "{{ security_group_rules }}"
  34. rules_egress: "{{ security_group_rules_egress }}"
  35. - name: Delete remote keypair
  36. ec2_key:
  37. name: "{{ keypair_name }}"
  38. state: absent
  39. - name: Create keypair
  40. ec2_key:
  41. name: "{{ keypair_name }}"
  42. register: keypair
  43. - name: Persist the keypair
  44. copy:
  45. dest: "{{ keypair_path }}"
  46. content: "{{ keypair.key.private_key }}"
  47. mode: 0600
  48. when: keypair.changed
  49. - name: Create molecule instance(s)
  50. ec2:
  51. key_name: "{{ keypair_name }}"
  52. image: "{{ item.image }}"
  53. instance_type: "{{ item.instance_type }}"
  54. vpc_subnet_id: "{{ item.vpc_subnet_id }}"
  55. group: "{{ security_group_name }}"
  56. instance_tags:
  57. instance: "{{ item.name }}"
  58. wait: true
  59. assign_public_ip: true
  60. exact_count: 1
  61. count_tag:
  62. instance: "{{ item.name }}"
  63. volumes:
  64. - device_name: /dev/sda1
  65. volume_type: gp2
  66. volume_size: 8
  67. delete_on_termination: yes
  68. register: server
  69. with_items: "{{ molecule_yml.platforms }}"
  70. async: 7200
  71. poll: 0
  72. - name: Wait for instance(s) creation to complete
  73. async_status:
  74. jid: "{{ item.ansible_job_id }}"
  75. register: ec2_jobs
  76. until: ec2_jobs.finished
  77. retries: 300
  78. with_items: "{{ server.results }}"
  79. # Mandatory configuration for Molecule to function.
  80. - name: Populate instance config dict
  81. set_fact:
  82. instance_conf_dict: {
  83. 'instance': "{{ item.instances[0].tags.instance }}",
  84. 'address': "{{ item.instances[0].public_ip }}",
  85. 'user': "{{ ssh_user }}",
  86. 'port': "{{ ssh_port }}",
  87. 'identity_file': "{{ keypair_path }}",
  88. 'instance_ids': "{{ item.instance_ids }}", }
  89. with_items: "{{ ec2_jobs.results }}"
  90. register: instance_config_dict
  91. when: server.changed | bool
  92. - name: Convert instance config dict to a list
  93. set_fact:
  94. instance_conf: "{{ instance_config_dict.results | map(attribute='ansible_facts.instance_conf_dict') | list }}"
  95. when: server.changed | bool
  96. - name: Dump instance config
  97. copy:
  98. content: "{{ instance_conf | to_json | from_json | molecule_to_yaml | molecule_header }}"
  99. dest: "{{ molecule_instance_config }}"
  100. when: server.changed | bool
  101. - name: Wait for SSH
  102. wait_for:
  103. port: "{{ ssh_port }}"
  104. host: "{{ item.address }}"
  105. search_regex: SSH
  106. delay: 10
  107. timeout: 320
  108. with_items: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}"
  109. - name: Wait for boot process to finish
  110. pause:
  111. minutes: 2