Memo/Ansible/AWS
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
|
ログイン
]
開始行:
#contents
*AWS(Amazon Web Services) [#ifd20b13]
-[[Amazon Web Services Guide — Ansible Documentatio...
-[[Amazon - Cloud modules:https://docs.ansible.com/ansibl...
-記事
--[[Using Ansible on AWS EC2インスタンスを作成する | De...
----
** 動作バージョン [#k8aa5bad]
- python, pipのバージョン管理が難しい。特にsystemがpython...
- ansible 2.11.12, Ubuntu 20.04.4 LTS
#geshi(bash){{
pyenv -v
pyenv 2.2.4-1-4-g1e79a522
python -V
Python 3.6.8
pip list | grep -P "ansible|boto"
ansible 4.10.0
ansible-core 2.11.12
boto3 1.23.10
botocore 1.26.10
ansible-galaxy collection list | grep aws
amazon.aws 5.4.0
community.aws 5.4.0
}}
----
** amazon.aws,community.aws [#b78bec1c]
- amazon.aws, community.aws: 相互に依存関係があって、片方...
記事:
- [[[Ansible] AWS 系コレクションは 2つある(amazon.aws、c...
- ansible 2.10まで
#geshi(bash){{
ansible-galaxy collection install community.aws:4.5.0 -f
ansible-galaxy collection install amazon.aws:4.4.0 -f
}}
- python v2.7で動く
#geshi(bash){{
ansible-galaxy collection install community.aws:1.5.0 -f
ansible-galaxy collection install amazon.aws:1.5.0 -f
}}
----
** ec2_metadata_facts: ec2 metadataを参照する [#t7e558b2]
- ec2 instance idが必要な場合
-- 「ansible_board_asset_tag」にもec2 id入っていた。これ...
-- ec2_metadata_factsでは「ansible_ec2_instance_id」で参...
- [[amazon.aws.ec2_metadata_facts module – Gathers ...
- cliで実行。どんな項目が取得できるか確認できる
#geshi(bash){{
ansible -i hosts.ini -m ec2_metadata_facts -u centos host01
}}
- playbookに足す場合:
#geshi(yaml){{
tasks:
- ec2_metadata_facts:
}}
- amazon.aws 5.0以上だと「The error was: AttributeError: ...
#geshi(bash){{
ansible-galaxy collection install amazon.aws:4.4.0 --force
}}
----
** CloudFormationのyamlの読み込み [#ebfdfc24]
- [[組み込み関数リファレンス - AWS CloudFormation:https:/...
- yaml中の「!Ref」「!Sub」といったCloudFormationの短縮形...
#geshi(text){{
could not determine a constructor for the tag '!Ref'\n i...
}}
- vars.yml:
#geshi(yaml){{{
template_body: "{{ lookup('file','cloudformation/exampl...
}}}
- 置換する。関数によって「Fn::」が不用な点が注意
-- 「!Ref」->「Ref:」
-- 「!Sub」->「Fn::Sub:」
#geshi(diff){{
- !Ref AwsAccountId
+ Ref: AwsAccountId
}}
- awscliを使った検証。あくまで構文のチェックで、実行時に...
#geshi(bash){{
aws cloudformation validate-template --template-body file...
}}
- 記事
-- [[AWS CloudFormation テンプレートの検証エラーまたは形...
--[[【小ネタ】PythonでCloudFormationテンプレートをパース...
----
** Dynamic Inventory: 動的にinventoryを取得 [#y7f61485]
- [[Working With Dynamic Inventory — Ansible Docume...
- 記事
-- [[[AWS]AnsibleのDynamic Inventoryを使って実行対象のEC2...
----
** AWS assume role [#jb821c34]
-参考 ~/.aws/config: [[Memo/AmazonWebServices/IAM#tfb7638...
-account1がログイン用、account2がswitch先とする
-''boto module'' の場合、単純にansible module側「profile:...
--ansible 2.5.x: Profile given for AWS was not found. Ple...
--例:[[ec2_remote_facts:http://docs.ansible.com/ansible/...
--[[ec2_elb_facts:http://docs.ansible.com/ansible/latest/...
-- [[route53:https://docs.ansible.com/ansible/2.3/route53...
-''boto3 module'' の場合、成功した
--例:[[aws_region_facts:http://docs.ansible.com/ansible/...
-「~/.boto -> ~/.aws/credentials」のようなsymlinkがあると...
-[[sts_assume_role:http://docs.ansible.com/ansible/latest...
#geshi(yaml){{{
- name: assume role
sts_assume_role:
role_arn: "{{ role_arn }}"
role_session_name: session1
region: "{{ region }}"
profile: "{{ aws_profile }}"
register: assumed_role
- name: EC2 remote facts
ec2_remote_facts:
region: "{{ region }}"
filters:
instance-state-name: running
register: result_ec2
changed_when: False
environment:
AWS_ACCESS_KEY_ID: "{{ assumed_role.sts_creds.acc...
AWS_SECRET_ACCESS_KEY: "{{ assumed_role.sts_creds...
AWS_SESSION_TOKEN: "{{ assumed_role.sts_creds.ses...
- name: Number of running
debug: msg="{{ result_ec2.instances | count }}"
}}}
----
**コマンドのjson出力を取り込んで、yaml形式で出力する [#af...
- [[Filters For Formatting Data:https://docs.ansible.com/...
-- 文字列からjsonやyamlへの変換ができる
-- to_nice_yaml(indent=2)のような指定もできる
-ec2 describe-instancesの結果(json)を取り込んで、yaml形式...
--&attachref(Memo/Ansible/aws.ec2.list-instance.zip);
----
**aws profileを利用する [#za095b6b]
いつのバージョンから(boto3 module?)か「~/.boto」は不用に...
''assume role利用の際に邪魔になるので、「~/.boto」は作ら...
「~/.aws/credentials」が既にある場合、ansibleからその中の...
ansibleのawsモジュールには ''profile:'' オプションで boto...
-[[Boto Config — boto v2.42.0:http://boto.cloudhack...
-例:[[dynamodb_table - Create, update or delete AWS Dyna...
- ansible v2.1, boto >= 2.24.0で動作確認できた。ansible v...
#geshi(yaml){{{
tasks:
# - name: create symlink ~/.boto
# file: src=~/.aws/credentials dest=~/.boto state=link
- name: Delete dynamodb table
dynamodb_table:
profile: "{{ aws_profile }}"
region: "{{ aws_region }}"
name: "{{ item }}"
hash_key_name: "{{ item }}" # ドキュメントでは re...
state: absent
with_items:
- test
}}}
終了行:
#contents
*AWS(Amazon Web Services) [#ifd20b13]
-[[Amazon Web Services Guide — Ansible Documentatio...
-[[Amazon - Cloud modules:https://docs.ansible.com/ansibl...
-記事
--[[Using Ansible on AWS EC2インスタンスを作成する | De...
----
** 動作バージョン [#k8aa5bad]
- python, pipのバージョン管理が難しい。特にsystemがpython...
- ansible 2.11.12, Ubuntu 20.04.4 LTS
#geshi(bash){{
pyenv -v
pyenv 2.2.4-1-4-g1e79a522
python -V
Python 3.6.8
pip list | grep -P "ansible|boto"
ansible 4.10.0
ansible-core 2.11.12
boto3 1.23.10
botocore 1.26.10
ansible-galaxy collection list | grep aws
amazon.aws 5.4.0
community.aws 5.4.0
}}
----
** amazon.aws,community.aws [#b78bec1c]
- amazon.aws, community.aws: 相互に依存関係があって、片方...
記事:
- [[[Ansible] AWS 系コレクションは 2つある(amazon.aws、c...
- ansible 2.10まで
#geshi(bash){{
ansible-galaxy collection install community.aws:4.5.0 -f
ansible-galaxy collection install amazon.aws:4.4.0 -f
}}
- python v2.7で動く
#geshi(bash){{
ansible-galaxy collection install community.aws:1.5.0 -f
ansible-galaxy collection install amazon.aws:1.5.0 -f
}}
----
** ec2_metadata_facts: ec2 metadataを参照する [#t7e558b2]
- ec2 instance idが必要な場合
-- 「ansible_board_asset_tag」にもec2 id入っていた。これ...
-- ec2_metadata_factsでは「ansible_ec2_instance_id」で参...
- [[amazon.aws.ec2_metadata_facts module – Gathers ...
- cliで実行。どんな項目が取得できるか確認できる
#geshi(bash){{
ansible -i hosts.ini -m ec2_metadata_facts -u centos host01
}}
- playbookに足す場合:
#geshi(yaml){{
tasks:
- ec2_metadata_facts:
}}
- amazon.aws 5.0以上だと「The error was: AttributeError: ...
#geshi(bash){{
ansible-galaxy collection install amazon.aws:4.4.0 --force
}}
----
** CloudFormationのyamlの読み込み [#ebfdfc24]
- [[組み込み関数リファレンス - AWS CloudFormation:https:/...
- yaml中の「!Ref」「!Sub」といったCloudFormationの短縮形...
#geshi(text){{
could not determine a constructor for the tag '!Ref'\n i...
}}
- vars.yml:
#geshi(yaml){{{
template_body: "{{ lookup('file','cloudformation/exampl...
}}}
- 置換する。関数によって「Fn::」が不用な点が注意
-- 「!Ref」->「Ref:」
-- 「!Sub」->「Fn::Sub:」
#geshi(diff){{
- !Ref AwsAccountId
+ Ref: AwsAccountId
}}
- awscliを使った検証。あくまで構文のチェックで、実行時に...
#geshi(bash){{
aws cloudformation validate-template --template-body file...
}}
- 記事
-- [[AWS CloudFormation テンプレートの検証エラーまたは形...
--[[【小ネタ】PythonでCloudFormationテンプレートをパース...
----
** Dynamic Inventory: 動的にinventoryを取得 [#y7f61485]
- [[Working With Dynamic Inventory — Ansible Docume...
- 記事
-- [[[AWS]AnsibleのDynamic Inventoryを使って実行対象のEC2...
----
** AWS assume role [#jb821c34]
-参考 ~/.aws/config: [[Memo/AmazonWebServices/IAM#tfb7638...
-account1がログイン用、account2がswitch先とする
-''boto module'' の場合、単純にansible module側「profile:...
--ansible 2.5.x: Profile given for AWS was not found. Ple...
--例:[[ec2_remote_facts:http://docs.ansible.com/ansible/...
--[[ec2_elb_facts:http://docs.ansible.com/ansible/latest/...
-- [[route53:https://docs.ansible.com/ansible/2.3/route53...
-''boto3 module'' の場合、成功した
--例:[[aws_region_facts:http://docs.ansible.com/ansible/...
-「~/.boto -> ~/.aws/credentials」のようなsymlinkがあると...
-[[sts_assume_role:http://docs.ansible.com/ansible/latest...
#geshi(yaml){{{
- name: assume role
sts_assume_role:
role_arn: "{{ role_arn }}"
role_session_name: session1
region: "{{ region }}"
profile: "{{ aws_profile }}"
register: assumed_role
- name: EC2 remote facts
ec2_remote_facts:
region: "{{ region }}"
filters:
instance-state-name: running
register: result_ec2
changed_when: False
environment:
AWS_ACCESS_KEY_ID: "{{ assumed_role.sts_creds.acc...
AWS_SECRET_ACCESS_KEY: "{{ assumed_role.sts_creds...
AWS_SESSION_TOKEN: "{{ assumed_role.sts_creds.ses...
- name: Number of running
debug: msg="{{ result_ec2.instances | count }}"
}}}
----
**コマンドのjson出力を取り込んで、yaml形式で出力する [#af...
- [[Filters For Formatting Data:https://docs.ansible.com/...
-- 文字列からjsonやyamlへの変換ができる
-- to_nice_yaml(indent=2)のような指定もできる
-ec2 describe-instancesの結果(json)を取り込んで、yaml形式...
--&attachref(Memo/Ansible/aws.ec2.list-instance.zip);
----
**aws profileを利用する [#za095b6b]
いつのバージョンから(boto3 module?)か「~/.boto」は不用に...
''assume role利用の際に邪魔になるので、「~/.boto」は作ら...
「~/.aws/credentials」が既にある場合、ansibleからその中の...
ansibleのawsモジュールには ''profile:'' オプションで boto...
-[[Boto Config — boto v2.42.0:http://boto.cloudhack...
-例:[[dynamodb_table - Create, update or delete AWS Dyna...
- ansible v2.1, boto >= 2.24.0で動作確認できた。ansible v...
#geshi(yaml){{{
tasks:
# - name: create symlink ~/.boto
# file: src=~/.aws/credentials dest=~/.boto state=link
- name: Delete dynamodb table
dynamodb_table:
profile: "{{ aws_profile }}"
region: "{{ aws_region }}"
name: "{{ item }}"
hash_key_name: "{{ item }}" # ドキュメントでは re...
state: absent
with_items:
- test
}}}
ページ名: