community.aws.aws_ssm 连接 – 通过 AWS Systems Manager 连接到 EC2 实例
注意
此连接插件是 community.aws 集合 (版本 9.0.0) 的一部分。
如果您使用的是 ansible
包,则可能已经安装了此集合。它不包含在 ansible-core
中。要检查它是否已安装,请运行 ansible-galaxy collection list
。
要安装它,请使用: ansible-galaxy collection install community.aws
。您需要其他要求才能使用此连接插件,请参阅 要求 获取详细信息。
要在 playbook 中使用它,请指定: community.aws.aws_ssm
。
概要
此连接插件允许 Ansible 通过 AWS SSM 会话在 EC2 实例上执行任务。
要求
以下要求是在执行此连接的本地控制器节点上所需的。
远程 EC2 实例必须运行 AWS Systems Manager 代理 (SSM 代理)。 https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-getting-started.html
控制机器必须安装 AWS 会话管理器插件。 https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-working-with-install-plugin.html
远程 EC2 Linux 实例必须安装 curl。
远程 EC2 Linux 实例和控制器都需要与 S3 的网络连接。
远程实例不需要 S3 的 IAM 凭证。此模块将从控制器生成 S3 的预签名 URL,然后将该 URL 通过 SSM 传递给目标,并告知目标使用
curl
从 S3 下载/上传。控制器需要 IAM 权限才能从指定的 S3 存储桶上传、下载和删除文件。这包括 `s3:GetObject`、`s3:PutObject`、`s3:ListBucket`、`s3:DeleteObject` 和 `s3:GetBucketLocation`。
参数
参数 |
注释 |
---|---|
用于文件传输的存储桶的 S3 端点 URL。 配置
|
|
用于文件传输的 S3 存储桶的名称。 配置
|
|
使用 配置
|
|
对用于文件传输的 S3 存储桶上的上传使用服务器端加密模式。 选项
配置
|
|
EC2 实例 ID。 配置
|
|
这定义了 session-manager-plugin 二进制文件的位置。 默认值: 配置
|
|
连接尝试次数。 默认值: 配置
|
|
使用 S3 URL 时使用的寻址样式。 当 S3 存储桶与实例不在同一区域时,可能需要显式地将寻址样式设置为“virtual” https://repost.aws/knowledge-center/s3-http-307-response,因为这会强制使用特定端点。 选项
配置
|
|
连接时使用的 SSM 会话文档。 配置 `remote_user` (当 配置
|
|
连接超时秒数。 默认值: 配置
|
备注
注意
`community.aws.aws_ssm` 连接插件不支持使用 ``remote_user`` 和 ``ansible_user`` 变量来配置远程用户。应使用 ``become_user`` 参数来配置以哪个用户身份运行命令。远程命令通常默认为以 ``ssm-agent`` 用户身份运行,但这也会取决于 SSM 的配置方式。
此插件需要一个 S3 存储桶来向远程实例发送/接收文件。即使对于那些没有明确发送文件(例如
shell
或command
模块)的模块,这也同样需要,因为 Ansible 通过 S3 发送模块本身的.py
文件。通过 S3 发送的文件将在 S3 中以 EC2 主机 ID(例如
i-123abc/
)作为前缀命名。playbook 运行结束时,S3 中的文件将被删除。如果 play 意外终止,这些文件可能会保留在存储桶中。如果存储桶启用了版本控制,则这些文件将保留在版本历史记录中。如果您的任务涉及向远程实例发送/接收密钥(例如,在
shell
命令中,或community.postgresql.postgresql_query
模块中的 SQL 密码),则这些密码将无限期地以明文形式包含在 S3 中的这些文件中,任何有权访问该存储桶的人都可以看到。因此,建议使用禁用/暂停版本控制的存储桶。即使
keep_remote_files
设置为true
,S3 中的文件也会被删除。
示例
---
# Wait for SSM Agent to be available on the Instance
- name: Wait for connection to be available
vars:
ansible_connection: aws_ssm
ansible_aws_ssm_bucket_name: nameofthebucket
ansible_aws_ssm_region: us-west-2
# When the S3 bucket isn't in the same region as the Instance
# Explicitly setting the addressing style to 'virtual' may be necessary
# https://repost.aws/knowledge-center/s3-http-307-response
ansible_aws_ssm_s3_addressing_style: virtual
tasks:
- name: Wait for connection
wait_for_connection:
# Stop Spooler Process on Windows Instances
- name: Stop Spooler Service on Windows Instances
vars:
ansible_connection: aws_ssm
ansible_shell_type: powershell
ansible_aws_ssm_bucket_name: nameofthebucket
ansible_aws_ssm_region: us-east-1
tasks:
- name: Stop spooler service
win_service:
name: spooler
state: stopped
# Install a Nginx Package on Linux Instance
- name: Install a Nginx Package
vars:
ansible_connection: aws_ssm
ansible_aws_ssm_bucket_name: nameofthebucket
ansible_aws_ssm_region: us-west-2
tasks:
- name: Install a Nginx Package
yum:
name: nginx
state: present
# Create a directory in Windows Instances
- name: Create a directory in Windows Instance
vars:
ansible_connection: aws_ssm
ansible_shell_type: powershell
ansible_aws_ssm_bucket_name: nameofthebucket
ansible_aws_ssm_region: us-east-1
tasks:
- name: Create a Directory
win_file:
path: C:\Windows\temp
state: directory
---
# Making use of Dynamic Inventory Plugin
# =======================================
# # aws_ec2.yml (Dynamic Inventory - Linux)
# plugin: aws_ec2
# regions:
# - us-east-1
# hostnames:
# - instance-id
# # This will return the Instances with the tag "SSMTag" set to "ssmlinux"
# filters:
# tag:SSMTag: ssmlinux
# -----------------------
- name: install aws-cli
hosts: all
gather_facts: false
vars:
ansible_connection: aws_ssm
ansible_aws_ssm_bucket_name: nameofthebucket
ansible_aws_ssm_region: us-east-1
tasks:
- name: aws-cli
raw: yum install -y awscli
tags: aws-cli
---
# Execution: ansible-playbook linux.yaml -i aws_ec2.yml
# =====================================================
# # aws_ec2.yml (Dynamic Inventory - Windows)
# plugin: aws_ec2
# regions:
# - us-east-1
# hostnames:
# - instance-id
# # This will return the Instances with the tag "SSMTag" set to "ssmwindows"
# filters:
# tag:SSMTag: ssmwindows
# -----------------------
- name: Create a dir.
hosts: all
gather_facts: false
vars:
ansible_connection: aws_ssm
ansible_shell_type: powershell
ansible_aws_ssm_bucket_name: nameofthebucket
ansible_aws_ssm_region: us-east-1
tasks:
- name: Create the directory
win_file:
path: C:\Temp\SSM_Testing5
state: directory
---
# Execution: ansible-playbook win_file.yaml -i aws_ec2.yml
# The playbook tasks will get executed on the instance ids returned from the dynamic inventory plugin using ssm connection.
# Install a Nginx Package on Linux Instance; with specific SSE CMK used for the file transfer
- name: Install a Nginx Package
vars:
ansible_connection: aws_ssm
ansible_aws_ssm_bucket_name: nameofthebucket
ansible_aws_ssm_region: us-west-2
ansible_aws_ssm_bucket_sse_mode: 'aws:kms'
ansible_aws_ssm_bucket_sse_kms_key_id: alias/kms-key-alias
tasks:
- name: Install a Nginx Package
yum:
name: nginx
state: present
# Install a Nginx Package on Linux Instance; using the specified SSM document
- name: Install a Nginx Package
vars:
ansible_connection: aws_ssm
ansible_aws_ssm_bucket_name: nameofthebucket
ansible_aws_ssm_region: us-west-2
ansible_aws_ssm_document: nameofthecustomdocument
tasks:
- name: Install a Nginx Package
yum:
name: nginx
state: present