2015-07-20 41 views
1

我刚刚开始与ansible和我找到了尝试提供vagrant文​​件的playbook语法的麻烦。下面是我的可靠的剧本问题与责任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  

我的麻烦是与我试图安装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.` 

有人请向我解释发生了什么事。

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 

附:请忽略写剧本的天真方式,因为我的目的是简单地开始使用。

+1

很奇怪。你能否删除'become_method:sudo'行来查看会发生什么? (无论如何,sudo是默认的成为方法)。 – leucos

+0

@leucos这太愚蠢了。请参阅回答 –

回答

3

它只是认为

  1. 我必须确保不必要的空格是不是有和

  2. 给任务的参数必须是在用空格隔开一行,对例如,

而不是

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

我需要使用

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

我傻!现在没有更多的语法错误。

+0

嗯,好吧,我被困在了become_method行:) – leucos

+0

是的,如果您使用'git:>'(要避免)或冒号语法(更喜欢),YAML可以做多行。检查它,例如https://github.com/debops/ansible-nginx/blob/master/tasks/nginx_configs.yml – leucos

+0

@leucos thanx男子。将会被使用。 –