在Ansible中放置Breadcrumbs不起作用

以下是我在Ansible中实现的一段代码,它试图configuration一个MySQL复制:

- stat: path=/etc/mysql/ansible.repl register: check_sql_path - name: create replicator user mysql_user: name: "replicator" host: "%" password: "{{ mypass.password_replication }}" priv: "*.*:REPLICATION SLAVE" state: present notify: - restart mysql - mysql_replication: mode: changemaster master_host: hostvars[inventory_hostname]['ansible_default_ipv4']['address'] master_user: replicator master_password: "{{ mypass.password_replication }}" when: check_sql_path.stat.exists == false notify: - restart mysql - command: touch /etc/mysql/repl.ansible when: check_sql_path.stat.exists == false 

但在这之前,它会检查是否

 check_sql_path.stat.exists is false 

当Ansible第一次运行时,variablescheck_sql_path.stat.exists被设置为true,所以第二次Ansible被调用,它不执行这个代码块。

但不知何故,这是在我的第二次运行中执行,我得到以下exception:

 ==> site: TASK [mysql_replication] ******************************************************* ==> site: fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "msg": "(1198, 'This operation cannot be performed with a running slave; run STOP SLAVE first'). Query == CHANGE MASTER TO ['MASTER_HOST=%(master_host)s', 'MASTER_USER=%(master_user)s', 'MASTER_PASSWORD=%(master_password)s']"} ==> site: to retry, use: --limit @/vagrant/ansible/playbook.retry 

看起来Ansible并没有考虑when情况。

我也检查了服务器和文件/etc/mysql/ansible.repl是第一次创build。

任何想法我可能在这里做错了吗?

您正在检查/etc/mysql/ansible.repl是否存在,并在/etc/mysql/repl.ansible上运行touch命令。 文件名称是不同的。

为了避免错误使用常量(定义为variables,可能与有意义的名称)。

也使用一致的YAML语法(即不要混用= :样式)和本地模块( file而不是command: touch )。

 vars: mysql_repl_flag_path: /etc/mysql/ansible.repl tasks: - stat: path: "{{ mysql_repl_flag_path }}" register: mysql_repl_flag # two tasks skipped for clarity - file: path: "{{ mysql_repl_flag_path }}" state: touch # because it is "touch" operation, the following condition is not necessary: # when: mysql_repl_flag.stat.exists == false # it also could be simpler: # when: not mysql_repl_flag.stat.exists