build_ami.yml 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. ---
  2. - hosts: localhost
  3. connection: local
  4. gather_facts: no
  5. tasks:
  6. - name: get the necessary vars for ami building
  7. include_vars: vars.yml
  8. - name: create a vpc with the name <clusterid>
  9. include_role:
  10. name: openshift_aws_vpc
  11. vars:
  12. r_openshift_aws_vpc_clusterid: "{{ provision.clusterid }}"
  13. r_openshift_aws_vpc_cidr: "{{ provision.vpc.cidr }}"
  14. r_openshift_aws_vpc_subnets: "{{ provision.vpc.subnets }}"
  15. r_openshift_aws_vpc_region: "{{ provision.region }}"
  16. r_openshift_aws_vpc_tags: "{{ provision.vpc.tags }}"
  17. r_openshift_aws_vpc_name: "{{ provision.vpc.name | default(provision.clusterid) }}"
  18. - name: create aws ssh keypair
  19. include_role:
  20. name: openshift_aws_ssh_keys
  21. vars:
  22. r_openshift_aws_ssh_keys_users: "{{ provision.instance_users }}"
  23. r_openshift_aws_ssh_keys_region: "{{ provision.region }}"
  24. - name: fetch the default subnet id
  25. ec2_vpc_subnet_facts:
  26. region: "{{ provision.region }}"
  27. filters:
  28. "tag:Name": "{{ provision.vpc.subnets[provision.region][0].az }}"
  29. register: subnetout
  30. - name: create instance for ami creation
  31. ec2:
  32. assign_public_ip: yes
  33. region: "{{ provision.region }}"
  34. key_name: "{{ provision.node_group_config.ssh_key_name }}"
  35. group: "{{ provision.clusterid }}"
  36. instance_type: m4.xlarge
  37. vpc_subnet_id: "{{ subnetout.subnets[0].id }}"
  38. image: "{{ provision.build.base_image }}"
  39. volumes:
  40. - device_name: /dev/sdb
  41. volume_type: gp2
  42. volume_size: 100
  43. delete_on_termination: true
  44. wait: yes
  45. exact_count: 1
  46. count_tag:
  47. Name: ami_base
  48. instance_tags:
  49. Name: ami_base
  50. register: amibase
  51. - name: wait for ssh to become available
  52. wait_for:
  53. port: 22
  54. host: "{{ amibase.tagged_instances.0.public_ip }}"
  55. timeout: 300
  56. search_regex: OpenSSH
  57. - name: add host to nodes
  58. add_host:
  59. groups: nodes
  60. name: "{{ amibase.tagged_instances.0.public_dns_name }}"
  61. - name: set the user to perform installation
  62. set_fact:
  63. ansible_ssh_user: root
  64. - name: normalize groups
  65. include: ../../byo/openshift-cluster/initialize_groups.yml
  66. - name: run the std_include
  67. include: ../../common/openshift-cluster/evaluate_groups.yml
  68. - name: run the std_include
  69. include: ../../common/openshift-cluster/initialize_facts.yml
  70. - name: run the std_include
  71. include: ../../common/openshift-cluster/initialize_openshift_repos.yml
  72. - hosts: nodes
  73. remote_user: root
  74. tasks:
  75. - name: get the necessary vars for ami building
  76. include_vars: vars.yml
  77. - set_fact:
  78. openshift_node_bootstrap: True
  79. - name: run openshift image preparation
  80. include_role:
  81. name: openshift_node
  82. - hosts: localhost
  83. connection: local
  84. become: no
  85. tasks:
  86. - name: bundle ami
  87. ec2_ami:
  88. instance_id: "{{ amibase.tagged_instances.0.id }}"
  89. region: "{{ provision.region }}"
  90. state: present
  91. description: "This was provisioned {{ ansible_date_time.iso8601 }}"
  92. name: "{{ provision.build.ami_name }}{{ lookup('pipe', 'date +%Y%m%d%H%M')}}"
  93. tags: "{{ provision.build.openshift_ami_tags }}"
  94. wait: yes
  95. register: amioutput
  96. - debug: var=amioutput
  97. - when: provision.build.use_encryption | default(False)
  98. block:
  99. - name: setup kms key for encryption
  100. include_role:
  101. name: openshift_aws_iam_kms
  102. vars:
  103. r_openshift_aws_iam_kms_region: "{{ provision.region }}"
  104. r_openshift_aws_iam_kms_alias: "alias/{{ provision.clusterid }}_kms"
  105. - name: augment the encrypted ami tags with source-ami
  106. set_fact:
  107. source_tag:
  108. source-ami: "{{ amioutput.image_id }}"
  109. - name: copy the ami for encrypted disks
  110. include_role:
  111. name: openshift_aws_ami_copy
  112. vars:
  113. r_openshift_aws_ami_copy_region: "{{ provision.region }}"
  114. r_openshift_aws_ami_copy_name: "{{ provision.build.ami_name }}{{ lookup('pipe', 'date +%Y%m%d%H%M')}}-encrypted"
  115. r_openshift_aws_ami_copy_src_ami: "{{ amioutput.image_id }}"
  116. r_openshift_aws_ami_copy_kms_alias: "alias/{{ provision.clusterid }}_kms"
  117. r_openshift_aws_ami_copy_tags: "{{ source_tag | combine(provision.build.openshift_ami_tags) }}"
  118. r_openshift_aws_ami_copy_encrypt: "{{ provision.build.use_encryption }}"
  119. # this option currently fails due to boto waiters
  120. # when supported this need to be reapplied
  121. #r_openshift_aws_ami_copy_wait: True
  122. - name: Display newly created encrypted ami id
  123. debug:
  124. msg: "{{ r_openshift_aws_ami_copy_retval_custom_ami }}"
  125. - name: terminate temporary instance
  126. ec2:
  127. state: absent
  128. region: "{{ provision.region }}"
  129. instance_ids: "{{ amibase.tagged_instances.0.id }}"