ansible playbook只运行一次任务

时间:2017-04-20 13:31:10

标签: ansible

我需要运行一个任务,设置一个标志,第二次播放运行只有在没有设置标志时运行任务

稍后播放

Informe de error -
Error SQL: ORA-04091: table HR.CONTRACTS is mutating, trigger/function may not see it
ORA-06512: at "HR.CHK_APARTADO_D", line 5
ORA-04088: error during execution of trigger 'HR.CHK_APARTADO_D'
ORA-06512: at "HR.CHK_APARTADO_D", line 8
ORA-04088: error during execution of trigger 'HR.CHK_APARTADO_D'
04091. 00000 -  "table %s.%s is mutating, trigger/function may not see it"
*Cause:    A trigger (or a user defined plsql function that is referenced in
           this statement) attempted to look at (or modify) a table that was
           in the middle of being modified by the statement which fired it.
*Action:   Rewrite the trigger (or function) so it does not read that table.

问题在于这个游戏第一次发挥它的错误。

- 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

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

一种选择是利用事实缓存。启用后,您可以在剧本中设置可缓存的事实并进行检查。

总是进行事实缓存。有许多事实缓存插件可用,其中默认使用内存插件,而json文件和Redis缓存是最受欢迎的插件。您只能设置一个插件。参考 https://docs.ansible.com/ansible/latest/plugins/cache.html

当您要使用json文件浏览时,可以如下设置环境变量:

export ANSIBLE_CACHE_PLUGIN=jsonfile
export ANSIBLE_CACHE_PLUGIN_CONNECTION="~/ansiblefactcache"

在您的剧本中,您可以检查事实并将其设置为可缓存,请参阅 https://docs.ansible.com/ansible/latest/collections/ansible/builtin/set_fact_module.html

一个小例子playbook.yaml

- name: Example Fact Cache Playbook
  hosts: all
  gather_facts: false # the default is true; it gathers various host facts
  tasks:
    - name: Runs when examplefact equals something
      debug:
        msg: "Runs when examplefact equals something"
      when: ansible_facts['examplefact'] is defined and ansible_facts['examplefact'] == "something"
    - name: Does not run when examplefact equals something
      debug:
        msg: "does not run when examplefact equals something"
      when: ansible_facts['examplefact'] is not defined or ansible_facts['examplefact'] != "something"
    - name: Set the examplefact to something
      set_fact: 
        examplefact: "something"
        cacheable: true

请注意cacheable指令的用法。当true进入缓存时。

运行完这个小手册后,您会注意到在您家的子文件夹localhost中创建了一个ansiblefactcache文件,其中包含您的缓存事实。

还要注意gather_facts指令的用法。默认值为true,它将在您的计算机上扫描各种详细信息,例如环境变量,网络详细信息等。所有这些信息均已缓存。您可以使用它,并查看其中填充了localhost文件。

您还可以尝试自己编辑localhost文件,甚至删除该文件并重新运行播放。

我使用了以下清单文件inventory.yaml

all:
  hosts:
    localhost:
      ansible_connection: local

然后我按以下方式运行ansibleansible-playbook playbook.yaml -i inventory.yaml -vvv

第一次运行将产生以下结果:

PLAY [Example Fact Cache Playbook] ********************************************************************************************************************************************************************************

TASK [Runs when examplefact equals something] *********************************************************************************************************************************************************************
skipping: [localhost]

TASK [Does not run when examplefact equals something] *************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "does not run when examplefact equals something"
}

TASK [Set the examplefact to something] ***************************************************************************************************************************************************************************
ok: [localhost]

第二次运行将产生以下结果:

PLAY [Example Fact Cache Playbook] ********************************************************************************************************************************************************************************

TASK [Runs when examplefact equals something] *********************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "Runs when examplefact equals something"
}

TASK [Does not run when examplefact equals something] *************************************************************************************************************************************************************
skipping: [localhost]

TASK [Set the examplefact to something] ***************************************************************************************************************************************************************************
ok: [localhost]
相关问题