build_ami.yml 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 group
  58. add_host:
  59. name: "{{ amibase.tagged_instances.0.public_dns_name }}"
  60. groups: amibase
  61. - hosts: amibase
  62. remote_user: root
  63. tasks:
  64. - name: included required variables
  65. include_vars: vars.yml
  66. - name: run openshift image preparation
  67. include_role:
  68. name: openshift_ami_prep
  69. vars:
  70. r_openshift_ami_prep_yum_repositories: "{{ provision.build.yum_repositories }}"
  71. r_openshift_ami_prep_node: atomic-openshift-node
  72. r_openshift_ami_prep_master: atomic-openshift-master
  73. - hosts: localhost
  74. connection: local
  75. become: no
  76. tasks:
  77. - name: bundle ami
  78. ec2_ami:
  79. instance_id: "{{ amibase.tagged_instances.0.id }}"
  80. region: "{{ provision.region }}"
  81. state: present
  82. description: "This was provisioned {{ ansible_date_time.iso8601 }}"
  83. name: "{{ provision.build.ami_name }}{{ lookup('pipe', 'date +%Y%m%d%H%M')}}"
  84. wait: yes
  85. register: amioutput
  86. - debug: var=amioutput
  87. - when: provision.build.use_encryption | default(False)
  88. block:
  89. - name: setup kms key for encryption
  90. include_role:
  91. name: openshift_aws_iam_kms
  92. vars:
  93. r_openshift_aws_iam_kms_region: "{{ provision.region }}"
  94. r_openshift_aws_iam_kms_alias: "alias/{{ provision.clusterid }}_kms"
  95. - name: augment the encrypted ami tags with source-ami
  96. set_fact:
  97. source_tag:
  98. source-ami: "{{ amioutput.image_id }}"
  99. - name: copy the ami for encrypted disks
  100. include_role:
  101. name: openshift_aws_ami_copy
  102. vars:
  103. r_openshift_aws_ami_copy_region: "{{ provision.region }}"
  104. r_openshift_aws_ami_copy_name: "{{ provision.build.ami_name }}{{ lookup('pipe', 'date +%Y%m%d%H%M')}}-encrypted"
  105. r_openshift_aws_ami_copy_src_ami: "{{ amioutput.image_id }}"
  106. r_openshift_aws_ami_copy_kms_alias: "alias/{{ provision.clusterid }}_kms"
  107. r_openshift_aws_ami_copy_tags: "{{ source_tag | combine(provision.build.openshift_ami_tags) }}"
  108. r_openshift_aws_ami_copy_encrypt: "{{ provision.build.use_encryption }}"
  109. # this option currently fails due to boto waiters
  110. # when supported this need to be reapplied
  111. #r_openshift_aws_ami_copy_wait: True
  112. - name: Display newly created encrypted ami id
  113. debug:
  114. msg: "{{ r_openshift_aws_ami_copy_retval_custom_ami }}"
  115. - name: terminate temporary instance
  116. ec2:
  117. state: absent
  118. region: "{{ provision.region }}"
  119. instance_ids: "{{ amibase.tagged_instances.0.id }}"