如何使用Ansible

时间:2015-07-27 07:30:56

标签: amazon-web-services amazon-ec2 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

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

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

1 个答案:

答案 0 :(得分: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