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 实例上执行任务。

要求

以下要求是在执行此连接的本地控制器节点上所需的。

参数

参数

注释

access_key_id

字符串

在 community.aws 1.3.0 中添加

通过会话管理器连接时使用的 STS 访问密钥。

配置

bucket_endpoint_url

字符串

在 community.aws 5.3.0 中添加

用于文件传输的存储桶的 S3 端点 URL。

配置

  • 变量:ansible_aws_ssm_bucket_endpoint_url

bucket_name

字符串

用于文件传输的 S3 存储桶的名称。

配置

  • 变量:ansible_aws_ssm_bucket_name

bucket_sse_kms_key_id

字符串

在 community.aws 2.2.0 中添加

使用 bucket_sse_mode=aws:kms 加密对象时使用的 KMS 密钥 ID。否则忽略。

配置

  • 变量:ansible_aws_ssm_bucket_sse_kms_key_id

bucket_sse_mode

字符串

在 community.aws 2.2.0 中添加

对用于文件传输的 S3 存储桶上的上传使用服务器端加密模式。

选项

  • "AES256"

  • "aws:kms"

配置

  • 变量:ansible_aws_ssm_bucket_sse_mode

instance_id

字符串

EC2 实例 ID。

配置

  • 变量:ansible_aws_ssm_instance_id

plugin

字符串

这定义了 session-manager-plugin 二进制文件的位置。

默认值: "/usr/local/bin/session-manager-plugin"

配置

  • 变量:ansible_aws_ssm_plugin

profile

字符串

在 community.aws 1.5.0 中添加

设置要使用的 AWS 配置文件。

配置

  • 环境变量:AWS_PROFILE

  • 变量:ansible_aws_ssm_profile

reconnection_retries

整数

连接尝试次数。

默认值: 3

配置

  • 变量:ansible_aws_ssm_retries

region

字符串

EC2 实例所在的区域。

默认值: "us-east-1"

配置

s3_addressing_style

字符串

在 community.aws 5.2.0 中添加

使用 S3 URL 时使用的寻址样式。

当 S3 存储桶与实例不在同一区域时,可能需要显式地将寻址样式设置为“virtual” https://repost.aws/knowledge-center/s3-http-307-response,因为这会强制使用特定端点。

选项

  • "path"

  • "virtual"

  • "auto" ← (默认)

配置

  • 变量:ansible_aws_ssm_s3_addressing_style

secret_access_key

字符串

在 community.aws 1.3.0 中添加

通过会话管理器连接时使用的 STS 密钥。

配置

session_token

字符串

在 community.aws 1.3.0 中添加

通过会话管理器连接时使用的 STS 会话令牌。

配置

ssm_document

字符串

在 community.aws 5.2.0 中添加

连接时使用的 SSM 会话文档。

配置 `remote_user` (当 become=False 时,可以使用 SSM Session 文档并定义 runAsEnabledrunAsDefaultUser 参数。另见 https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-schema.html

配置

  • 变量:ansible_aws_ssm_document

ssm_timeout

整数

连接超时秒数。

默认值: 60

配置

  • 变量:ansible_aws_ssm_timeout

备注

注意

  • `community.aws.aws_ssm` 连接插件不支持使用 ``remote_user`` 和 ``ansible_user`` 变量来配置远程用户。应使用 ``become_user`` 参数来配置以哪个用户身份运行命令。远程命令通常默认为以 ``ssm-agent`` 用户身份运行,但这也会取决于 SSM 的配置方式。

  • 此插件需要一个 S3 存储桶来向远程实例发送/接收文件。即使对于那些没有明确发送文件(例如 shellcommand 模块)的模块,这也同样需要,因为 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

作者

  • Pat Sharkey (@psharkey)

  • HanumanthaRao MVL (@hanumantharaomvl)

  • Gaurav Ashtikar (@gau1991)

提示

每种条目类型的配置项具有从低到高的优先级顺序。例如,列表中较低的变量将覆盖列表中较高的变量。