# What's wrong with this ansible playbook ?

**URL:** <https://forum.ansible.com/t/whats-wrong-with-this-ansible-playbook/13752>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [October 23, 2013, 3:36pm UTC](https://forum.ansible.com/t/whats-wrong-with-this-ansible-playbook/13752 "2013-10-23T15:36:07Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Soumya\_Simanta](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@Soumya\_Simanta](https://forum.ansible.com/u/Soumya_Simanta)\
**Post date:** [October 23, 2013, 3:36pm UTC](https://forum.ansible.com/t/whats-wrong-with-this-ansible-playbook/13752/1 "2013-10-23T15:36:07Z")

</div>

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>

```auto

    ##########
    # 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

---

<div class="post-metadata">

**Author:** ![James\_Martin2](https://avatars.discourse-cdn.com/v4/letter/j/b9e5f3/32.png) [@James\_Martin2](https://forum.ansible.com/u/James_Martin2)\
**Post date:** [October 23, 2013, 3:45pm UTC](https://forum.ansible.com/t/whats-wrong-with-this-ansible-playbook/13752/2 "2013-10-23T15:45:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tannerjc](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/tannerjc/32/985_2.png) [@tannerjc](https://forum.ansible.com/u/tannerjc)\
**Post date:** [October 23, 2013, 3:47pm UTC](https://forum.ansible.com/t/whats-wrong-with-this-ansible-playbook/13752/3 "2013-10-23T15:47:13Z")

</div>

Soumya, can you pastebin your playbook?

---

<div class="post-metadata">

**Author:** ![Soumya\_Simanta](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@Soumya\_Simanta](https://forum.ansible.com/u/Soumya_Simanta)\
**Post date:** [October 23, 2013, 4:46pm UTC](https://forum.ansible.com/t/whats-wrong-with-this-ansible-playbook/13752/4 "2013-10-23T16:46:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Soumya\_Simanta](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@Soumya\_Simanta](https://forum.ansible.com/u/Soumya_Simanta)\
**Post date:** [October 23, 2013, 4:58pm UTC](https://forum.ansible.com/t/whats-wrong-with-this-ansible-playbook/13752/5 "2013-10-23T16:58:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Brice\_Burgess](https://avatars.discourse-cdn.com/v4/letter/b/d2c977/32.png) [@Brice\_Burgess](https://forum.ansible.com/u/Brice_Burgess)\
**Post date:** [October 23, 2013, 5:11pm UTC](https://forum.ansible.com/t/whats-wrong-with-this-ansible-playbook/13752/6 "2013-10-23T17:11:04Z")

</div>

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:

```auto
- 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

---

<div class="post-metadata">

**Author:** ![Soumya\_Simanta](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@Soumya\_Simanta](https://forum.ansible.com/u/Soumya_Simanta)\
**Post date:** [October 23, 2013, 6:04pm UTC](https://forum.ansible.com/t/whats-wrong-with-this-ansible-playbook/13752/7 "2013-10-23T18:04:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [October 23, 2013, 6:52pm UTC](https://forum.ansible.com/t/whats-wrong-with-this-ansible-playbook/13752/8 "2013-10-23T18:52:58Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Soumya\_Simanta](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@Soumya\_Simanta](https://forum.ansible.com/u/Soumya_Simanta)\
**Post date:** [October 23, 2013, 7:16pm UTC](https://forum.ansible.com/t/whats-wrong-with-this-ansible-playbook/13752/9 "2013-10-23T19:16:36Z")

</div>

@Michael,

Here are both the files for my playbook on github.  
[https://gist.github.com/soumyasd/7124813](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

---

<div class="post-metadata">

**Author:** ![Scott\_Sturdivant](https://avatars.discourse-cdn.com/v4/letter/s/f14d63/32.png) [@Scott\_Sturdivant](https://forum.ansible.com/u/Scott_Sturdivant)\
**Post date:** [October 23, 2013, 7:20pm UTC](https://forum.ansible.com/t/whats-wrong-with-this-ansible-playbook/13752/10 "2013-10-23T19:20:10Z")

</div>

[https://gist.github.com/soumyasd/7122153#file-ec2\_launch-yml-L46](https://gist.github.com/soumyasd/7122153#file-ec2_launch-yml-L46)

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

---

<div class="post-metadata">

**Author:** ![Soumya\_Simanta](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@Soumya\_Simanta](https://forum.ansible.com/u/Soumya_Simanta)\
**Post date:** [October 23, 2013, 11:53pm UTC](https://forum.ansible.com/t/whats-wrong-with-this-ansible-playbook/13752/11 "2013-10-23T23:53:31Z")

</div>

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