2017-04-20 81 views
0

我需要运行任务时,设置一个标志和第二次玩运行运行任务只有在标志没有在后面的阶段ansible剧本运行任务,只有一次

- name: Dump all databases 
    mysql_db: 
    state: dump 
    name: all 
    target: /root/mysql_all.sql 
    when: ansible_local.mysql.replication.setup is not defined 

- name: create directory for ansible custom facts 
    file: state=directory recurse=yes path=/etc/ansible/facts.d 
- name: install custom fact stating mysql is setup 
    template: 
    src: mysql.fact.j2 
    dest: /etc/ansible/facts.d/mysql.fact 
设置

播放

问题是,这场比赛的第一时间运行抛出一个错误。

FAILED! => {"failed": true, "msg": "The conditional check 'ansible_local.mysql.replication.setup is not defined' failed. The error was: error while evaluating conditional (ansible_local.mysql.replication.setup is not defined): 'ansible_local' is undefined 

什么是仅在第一次运行时运行任务并跳过后续运行的最佳方式。

回答

0

如果存在/root/mysql_all.sql,那么您应该先注册一个任务,然后将其添加到您的when子句中。

实施例:

- name: check if dump exists 
    stat: 
    path: /root/mysql_all.sql 
    register: mysqldump 

- name: Dump all databases 
    mysql_db: 
    state: dump 
    name: all 
    target: /root/mysql_all.sql 
    when: 
    - ansible_local.mysql.replication.setup is not defined 
    - mysqldump.stat.exists == true 

- name: create directory for ansible custom facts 
    file: state=directory recurse=yes path=/etc/ansible/facts.d 
- name: install custom fact stating mysql is setup 
    template: 
    src: mysql.fact.j2 
    dest: /etc/ansible/facts.d/mysql.fact 
+0

FO的事实的检查是仍然失败作为变量是未定义的。但是我认为只有在文件不存在的情况下第一次运行和运行独特的文件时,如您所建议的那样简单。 –