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