What's wrong with this ansible playbook ?

I’m new to Ansibile and having trouble with a playbook. I’ve resolved many issues with the help of members on this list. Thank you. However, I’m yet to have complete success. Here is my current issue.

I’m trying to launch an EC2 instance and then install some software on it. I can launch the instance and I can gather facts from it. However, when play-book gets to the task that tries to install something on the remote instance it tries logs in as a different user (my current user id) and NOT as user “ubuntu.” It fails because the launched EC2 instance is only configured for the user “ubuntu”.

Here is the part of the playbook where it fails. Shouldn’t the user be “ubuntu” for all tasks ? If not, is there a way to specify the user per task ?

This play targets the new host group

  • name: Configure instance
    hosts: deploy
    user: ubuntu
    sudo: yes
    gather_facts: true

Install the necessary software on each instance

tasks:

- name: Install JDK
apt: pkg=openjdk-6-jre-headless state=latest install_recommends=no update_cache=yes

When I try the following syntax it fails again.

- name: Install JDK
apt: pkg=openjdk-6-jre-headless state=latest install_recommends=no update_cache=yes

user: ubuntu

Here is the error message.
ERROR: multiple actions specified in task Install JDK

This error message makes sense if user is a module and apt is a module then we cannot have both.

But according to this example here this syntax should work.
https://gist.github.com/phred/2897937#file-pedantically_commented_playbook-yml-L379-L388


    ##########
    # Run things as other users!
    #
    # Each task has an optional 'user' and 'sudo' flag to indicate which
    # user a task should run as and whether or not to use 'sudo' to switch
    # to that user.
    - name: dump all postgres databases
      action: pg_dumpall -w -f /tmp/backup.psql
      user: postgres
      sudo: False

Here is my complete playbook.

  • name: Stage instance(s)
    hosts: cfgsvr
    connection: local
    gather_facts: false

vars:
keypair: mykeypair
image: ami-2efa9d47

instance_type: m1.small #t1.micro
subnet: subnet-XXXXX
region: us-east-1

Launch 1 instance with the following parameters. Register the output.

You can change the number of instances to lanuch by changing the “count” variable in the location action

tasks:

  • name: Launch instance
    local_action: ec2 keypair={{keypair}} vpc_subnet_id={{subnet}} instance_type={{instance_type}} image={{image}} wait=true count=1
    register: ec2

Use with_items to add each instances public IP to a new hostgroup for use in the next play.

  • name: Add new instances to host group
    local_action: add_host hostname={{item.private_ip}} groupname=deploy
    with_items: ${ec2.instances}

  • name: Wait for the instances to boot by checking the ssh port
    local_action: wait_for host={{item.public_dns_name}} port=22 delay=60 timeout=320 state=started
    with_items: ${ec2.instances}

- name: Breathing room (Ansible uses python apt, has issues running directly after boot)

pause: seconds=30

This play targets the new host group

  • name: Configure instance
    hosts: deploy
    user: ubuntu
    sudo: yes
    gather_facts: true

Install the necessary software on each instance

tasks:

- name: Install JDK
apt: pkg=openjdk-6-jre-headless state=latest install_recommends=no update_cache=yes

  • name: Install Maven2
    apt: pkg=maven2 state=latest update_cache=yes

I think perhaps you’ve unecessarily idented your tasks. the -name should line up with tasks. Also I’m not sure that you can use -name as a top level attribute to your playbook.

tasks:

  • name: foo

Soumya, can you pastebin your playbook?

Here is the playbook on gist. Let me know if you still want it on pastebin.
https://gist.github.com/soumyasd/7122153

My understanding was you can have multiple plays in a single yml file.

For example.

I’ve two plays.

  • name: Stage instance(s)

  • name: Configure instance

and each play has a bunch of tasks

tasks:

-name: task1
action:

-name: task2
action:

and each task has a name an action associated with it.

Soumya,

You may want to separate the playbooks into separate .yml files, and then use a single playbook that includes these plays.

“”“”

Includes can also be used to import one playbook file into another. This allows you to define a top-level playbook that is composed of other playbooks.

For example:

- name: this is a play at the top level of a file
  hosts: all
  remote_user: root
  tasks:
  - name: say hi
    tags: foo
    shell: echo "hi..."

- include: load_balancers.yml
- include: webservers.yml
- include: dbservers.yml

Note that you cannot do variable substitution when including one playbook inside another.

“”"

~ Brice

I tried breaking it into multiple yml files but I still get the same result i.e., it still tries to login as the logged in user and not as ubuntu.

Would be easier to browse around if this were uploaded to github, FWIW.

Also makes sure things are not left out or assumed to be one way or the other.

Thanks!

@Michael,

Here are both the files for my playbook on github.
https://gist.github.com/soumyasd/7124813

Also makes sure things are not left out or assumed to be one way or the other.

Can you explain what this means ?

Thanks
-Soumya

https://gist.github.com/soumyasd/7122153#file-ec2_launch-yml-L46

That ‘user’ is being considered a ‘user’ module. Remove that line.

@Scott - It doesn’t work even if I remove the “user” module.