1

I have just begun with ansible and I'm finding trouble with playbook syntax trying to provision a vagrant file. Below is my ansible playbook

---
- hosts: all
  tasks:
  - name: update apt cache
    apt: update_cache=yes
    become: yes
    become_method: sudo

  - name: create a directory for projects
    file: path=/home/projects 
          state=directory

  - name: create a directory for our project
    file: path=/home/projects/myproject 
          state=directory

  - name: install git
    apt: name=git 
    become: yes
    become_method: sudo

  - name: initiaite git
    command: git init
    args: 
       chdir: /home/projects/myproject

  - name: pull git
    git: repo=https://github.com/path/to/repo.git
         dest=/home/projects/myproject  

  - name: install mysql
    apt: name=mysql-server
    become: yes
    become_method: sudo

  - name: create mysql db for project
    mysql_db: name=mydb 
              encoding=utf8

  - name: create user and assign privileges
    mysql_user: name=foo
                password=bar 
                priv=mydb.*,GRANT

  - name: install pip
    apt: name=pip  
    become: yes
    become_method: sudo

  - name: install virtualenv
    pip: name=virtualenv
    become: yes
    become_method: sudo

  - name: Create the initial virtualenv
    command: virtualenv /home/projects/myproject/venv -p python2.7     creates="/home/projects/myproject/venv"

  - name: install requirements
    pip:
        requirements=/home/projects/myproject/requirements.txt
        virtualenv=/home/projects/bankproblem/venv      

My trouble is with the 4th task where I'm trying to install git.

ERROR: Syntax Error while loading YAML script, playbook.yml
Note: The error may actually appear before this position: line 21, column 1

    become_method: sudo

^
Ansible failed to complete successfully. Any error output should be
visible above. Please fix these errors and try again.`

Someone please explain to me what's happening.

My Vagrantfile

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "trusty-server-cloudimg-amd64-vagrant-disk1.box"
  config.vm.box_url = "https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-  vagrant-disk1.box"

  config.vm.network "forwarded_port", guest: 80, host: 8080

  config.vm.provision :ansible do |ansible|
    ansible.playbook = "playbook.yml"
  end
end

P.S. Please neglect the naive way the playbook is written as my intention is to simply get started with.

Afzal S.H.
  • 1,084
  • 2
  • 14
  • 27

1 Answers1

3

It was simply that

  1. I had to make sure unnecessary white spaces aren't there and

  2. The arguments to a task had to be in a single line separated by spaces, for instance,

Instead of

git: repo=https://github.com/path/to/repo.git
     dest=/home/projects/myproject

I needed to use

git: repo=https://github.com/path/to/repo.git dest=/home/projects/myproject

Silly me! No more syntax errors now.

Afzal S.H.
  • 1,084
  • 2
  • 14
  • 27
  • Hmm, okay, I was stuck on the become_method line :) – leucos Jul 20 '15 at 20:34
  • Yes, YAML can do multiline if you use 'git: >' (to be avoided) or the colon syntax (much preferred). Check it out there for instance https://github.com/debops/ansible-nginx/blob/master/tasks/nginx_configs.yml – leucos Jul 20 '15 at 20:36
  • @leucos please see if you know what's http://stackoverflow.com/q/31531929/4672736 about – Afzal S.H. Jul 21 '15 at 06:37