如何使用Ansible在启动时传递EC2用户数据来安装软件包

Arb*_*zar 4 amazon-ec2 amazon-web-services ansible ansible-playbook

我想通过在启动时将它作为用户数据传递给ec2实例来安装nginx.我这样传递它

- name: WebServer | Create the WebServer Instance(s)
  local_action:
    module: ec2
    region: "{{ vpc_region }}"
    group: "{{ ec2_security_groups[1].sg_name }}"
    keypair: "{{ key_name }}"
    instance_type: "{{ web_instance_type }}"
    ****user_data: "sudo apt-get install nginx -y"****
    image: "{{ imgae_id.ami }}"
    vpc_subnet_id: "{{ public_subnet }}"
    assign_public_ip: True
    wait: True
    wait_timeout: 600
Run Code Online (Sandbox Code Playgroud)

但是上面的方法对我来说不起作用,虽然它已经成功创建了EC2实例但没有安装nginx.

你能指点我正确的方向吗?谢谢

Con*_*ado 9

你错过了shebang,请尝试以下方法:

- name: WebServer | Create the WebServer Instance(s)
  local_action:
    module: ec2
    region: "{{ vpc_region }}"
    group: "{{ ec2_security_groups[1].sg_name }}"
    keypair: "{{ key_name }}"
    instance_type: "{{ web_instance_type }}"
    user_data: |
               #!/bin/sh
               sudo apt-get install nginx -y
    image: "{{ imgae_id.ami }}"
    vpc_subnet_id: "{{ public_subnet }}"
    assign_public_ip: True
    wait: True
    wait_timeout: 600
Run Code Online (Sandbox Code Playgroud)