packer buildしてできたamiだけ取得(packerの出力変わると使えない)
#!/usr/bin/env python import subprocess import re def run(cmd): p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) stdout = [] while True: line = p.stdout.readline() stdout.append(line) if not line and p.poll() is not None: for text in stdout: match = re.search('ap-northeast-1:\s{1}(.*)', str(text.decode("utf-8"))) if match: print(match.group(1), end = "") break return ''.join(str(stdout)) if __name__ == '__main__': run("packer build -var-file=var.json template.json")
プレースホルダを含んだtfを使うなど
python exec_packer.py | xargs -I{} sed -e 's/{{AMI_ID}}/{}/g' test.tf > test_e.tf