ansible -i hosts.ini -m ec2_metadata_facts -u centos host01
tasks: - ec2_metadata_facts:
ansible-galaxy collection install amazon.aws:4.4.0 --force
could not determine a constructor for the tag '!Ref'\n in \"<unicode string>
template_body: "{{ lookup('file','cloudformation/example.yml') | from_yaml }}"
- !Ref AwsAccountId + Ref: AwsAccountId
aws cloudformation validate-template --template-body file://cloudformation/example.yml --profile example
- 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.access_key }}" AWS_SECRET_ACCESS_KEY: "{{ assumed_role.sts_creds.secret_key }}" AWS_SESSION_TOKEN: "{{ assumed_role.sts_creds.session_token }}" - name: Number of running debug: msg="{{ result_ec2.instances | count }}"
いつのバージョンから(boto3 module?)か「~/.boto」は不用になった。 assume role利用の際に邪魔になるので、「~/.boto」は作らない方が良い。
「~/.aws/credentials」が既にある場合、ansibleからその中のアクセスキー等を利用したい。
ansibleのawsモジュールには profile: オプションで boto configの値を指定でき、フォーマットはaws configと同じなのでsymlinkで良さそう
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 }}" # ドキュメントでは required:no だが、実行時にエラーが出るため追加 state: absent with_items: - test