Last active
January 4, 2019 21:53
-
-
Save Kento75/13e6374f6d54b194f2a11b0d61470af7 to your computer and use it in GitHub Desktop.
【備忘録】Ansible応用編① EC2インスタンス作成から鍵認証ログインの自動化 ref: https://qiita.com/Kento75/items/5175460b669cf1d1a325
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ssh_connection] | |
ssh_args = -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no | |
private_key_file = /home/ec2-user/.ssh/dev-key.pem |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ~/create-ec2.yml | |
- name: EC2インスタンス作成 | |
hosts: localhost | |
connection: local | |
gather_facts: False | |
vars: | |
keypair_name: dev-key # 作成する認証鍵の名前 | |
ec2_name: test-ec2 # 作成するEC2の名前 | |
profile: default | |
region: ap-northeast-1 | |
tasks: | |
- name: AWSに認証鍵を登録 | |
ec2_key: | |
name: "{{ keypair_name }}" | |
region: "{{ region }}" # リージョンの指定 | |
profile: "{{ profile }}" # プロフィールの指定 | |
register: keypair_regst | |
- name: 認証鍵ファイル生成(権限:0600) | |
file: | |
path=~/.ssh/{{ keypair_regst.key.name }}.pem | |
state=touch | |
mode=0600 | |
- name: 認証鍵ファイルにデータを追加 | |
shell: echo "{{ keypair_regst.key.private_key }}" > ~/.ssh/{{ keypair_name }}.pem | |
when: keypair_regst.key.private_key is defined | |
- name: EC2インスタンスの生成 | |
ec2: | |
instance_tags: | |
Name: "{{ ec2_name }}" # インスタンスの名前を指定 | |
key_name: "{{ keypair_name }}" # インスタンスにログインするための認証鍵を指定 | |
instance_type: t2.micro # インスタンスタイプを指定 | |
image: ami-0a2de1c3b415889d2 # Amazon Linux2 を指定 | |
region: ap-northeast-1 # 東京リージョンを指定 | |
vpc_subnet_id: subnet-xxxxxxx # サブネットを指定(ここは適宜変える) | |
group: your-group-name # セキュリティグループ名を指定(ここは適宜変える) | |
wait: yes | |
count: 1 # インスタンスは1つ | |
assign_public_ip: no | |
register: ec2_regst | |
- name: インベントリにホストを追加 | |
add_host: | |
groups=my_host | |
name="{{ ec2_regst.instances[0].private_ip }}" | |
- name: 作成したEC2が接続可能状態になるまで待つ | |
local_action: wait_for port=22 host="{{ ec2_regst.instances[0].private_ip }}" search_regex=OpenSSH delay=5 | |
- name: EC2インスタンスにsshログイン | |
hosts: my_host # 作成時にインベトリに追加したグループを指定 | |
serial: 1 | |
user: ec2-user # EC2 デフォルトユーザーを指定 | |
tasks: | |
- debug: | |
msg: "Hello world!" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ansible-playbook create-ec2.yml | |
PLAY [EC2インスタンス作成] *************************************************************************************************************************************************************************************************************************** | |
TASK [AWSに認証鍵を登録] **************************************************************************************************************************************************************************************************************************** | |
changed: [127.0.0.1] | |
TASK [認証鍵ファイル生成(権限:0600)] ******************************************************************************************************************************************************************************************************************** | |
changed: [127.0.0.1] | |
TASK [認証鍵ファイルにデータを追加] ************************************************************************************************************************************************************************************************************************ | |
changed: [127.0.0.1] | |
TASK [EC2インスタンスの生成] ************************************************************************************************************************************************************************************************************************** | |
changed: [127.0.0.1] | |
TASK [インベントリにホストを追加] ************************************************************************************************************************************************************************************************************************* | |
changed: [127.0.0.1] | |
TASK [作成したEC2が接続可能状態になるまで待つ] ***************************************************************************************************************************************************************************************************************** | |
ok: [127.0.0.1 -> localhost] | |
PLAY [EC2インスタンスにsshログイン] ********************************************************************************************************************************************************************************************************************* | |
TASK [Gathering Facts] *********************************************************************************************************************************************************************************************************************** | |
ok: [xxx.xx.xx.xxx] | |
TASK [debug] ********************************************************************************************************************************************************************************************************************************* | |
ok: [xxx.xx.xx.xxx] => { | |
"msg": "Hello world!" | |
} | |
PLAY RECAP *********************************************************************************************************************************************************************************************************************************** | |
127.0.0.1 : ok=6 changed=5 unreachable=0 failed=0 | |
xxx.xx.xx.xxx : ok=2 changed=0 unreachable=0 failed=0 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment