将 Ansible 指向一组动态 EC2 节点的 .pem 文件



我对Ansible很陌生。我得到: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Permission denied (publickey).", "unreachable": true} 在我尝试使用这本 Ansible 剧本的最后一步

---
- name: find EC2 instaces
  hosts: localhost
  connection: local
  gather_facts: false
  vars:
    ansible_python_interpreter: "/usr/bin/python3"
    ansible_ssh_common_args: "-o StrictHostKeyChecking=no"
    aws_region: "us-west-2"
    vpc_subnet_id: "subnet-xxx"
    ec2_filter:
      "tag:Name": "airflow-test"
      "tag:Team": 'data-science'
      "tag:Environment": 'staging'
      "instance-state-name": ["stopped", "running"]
  vars_files:
    - settings/vars.yml
  tasks:
    - name: Find EC2 Facts
      ec2_instance_facts:
        region: "{{ aws_region }}"
        filters:
          "{{ ec2_filter }}"
      register: ec2
    - name: Add new instance to host group
      add_host:
        hostname: "{{ item.public_dns_name }}"
        groupname: launched
      loop: "{{ ec2.instances }}"
    - name: Wait for the instances to boot by checking the ssh port
      wait_for:
        host: "{{  item.public_dns_name  }}"
        port: 22
        sleep: 10
        timeout: 120
        state: started
      loop: "{{ ec2.instances }}"
- name: install required packages on instances
  hosts: launched
  become: True
  gather_facts: True
  vars:
    ansible_ssh_common_args: "-o StrictHostKeyChecking=no"
  tasks:
    - name: ls
      command: ls

我知道我需要将 Ansible 指向.pem文件,我试图将ansible_ssh_private_key_file添加到库存文件中,但考虑到节点是动态的,不确定该怎么做。

添加ansible_ssh_user解决了问题

- name: install required packages on instances
  hosts: launched
  become: True
  gather_facts: True
  vars:
    ansible_ssh_common_args: "-o StrictHostKeyChecking=no"
    ansible_ssh_user: "ec2-user"
  tasks:
    - name: ls
      command: ls

最新更新