通过Ansible安装PHP Pear包,具有幂等性

gee*_*guy 8 php pear idempotent ansible

我正在使用Ansible安装PHP的Pear包,如下所示:

- name: Add Phergie PEAR channel.
  command: pear channel-discover pear.phergie.org
  ignore_errors: yes

- name: Install Phergie and related plugins.
  command: pear install pear.phergie.org/{{ item }}
  with_items:
  - Phergie
  - Phergie_Plugin_AltNick
  ignore_errors: yes
Run Code Online (Sandbox Code Playgroud)

ignore_errors是必需的,因为当运行之前已成功运行/完成的命令时,pear总是报告错误(例如:

TASK: [Add Phergie PEAR channel.] ********************************************* 
failed: [10.31.9.210] => {"changed": true, "cmd": ["pear", "channel-discover", "pear.phergie.org"], "delta": "0:00:01.089340", "end": "2013-12-27 10:16:25.640083", "item": "", "rc": 1, "start": "2013-12-27 10:16:24.550743"}
stdout: Channel "pear.phergie.org" is already initialized
...ignoring

TASK: [Install Phergie and related plugins.] ********************************** 
failed: [10.31.9.210] => (item=Phergie) => {"changed": true, "cmd": ["pear", "install", "pear.phergie.org/Phergie"], "delta": "0:00:03.698780", "end": "2013-12-27 10:16:30.337371", "item": "Phergie", "rc": 1, "start": "2013-12-27 10:16:26.638591"}
stdout: phergie/Phergie is already installed and is the same as the released version 2.1.0
install failed
...ignoring
failed: [10.31.9.210] => (item=Phergie_Plugin_AltNick) => {"changed": true, "cmd": ["pear", "install", "pear.phergie.org/Phergie_Plugin_AltNick"], "delta": "0:00:01.779589", "end": "2013-12-27 10:16:33.231524", "item": "Phergie_Plugin_AltNick", "rc": 1, "start": "2013-12-27 10:16:31.451935"}
stdout: phergie/Phergie_Plugin_AltNick is already installed and is the same as the released version 2.1.0
install failed
...ignoring
Run Code Online (Sandbox Code Playgroud)

是否有更好的(更幂等)的方式来运行pear命令,而不必滚动一堆大的,红色忽略的错误?

gee*_*guy 9

好吧,所以在玩了一下changed_when属性之后,我终于找到了解决方案(在不同的剧本上测试,我在那里安装drush而不是Phergie,但问题/解决方案完全相同:

剧本:

- name: Setup drush PEAR channel.
  command: pear channel-discover pear.drush.org
  register: channel_result
  environment: proxy_env
  changed_when: "'initialized' not in channel_result.stdout"
  # TODO: This will always error out the first time it's run.
  failed_when: "'already initialized' not in channel_result.stdout"

- name: Install drush.
  command: pear install drush/drush
  register: drush_result
  environment: proxy_env
  changed_when: "'installed' not in drush_result.stdout"
  failed_when: "'6.2.0.0' not in drush_result.stdout"
Run Code Online (Sandbox Code Playgroud)

Ansible的新产品:

TASK: [Setup drush PEAR channel.] ********************************************* 
ok: [midwesternmac]

TASK: [Install drush.] ******************************************************** 
ok: [midwesternmac]
Run Code Online (Sandbox Code Playgroud)

所以现在在摘要中,Ansible仅报告"已更改",而不是为每个服务器和每个pear命令报告额外的"已更改".有关changed_when和failed_when(需要Ansible> = 1.3)的更多文档(虽然稀疏)可在此处获得:Playbooks中的错误处理

  • 这是一个如何在第一次运行时避免错误的示例 - 不是Drush特定的,但适用于:https://github.com/wizonesolutions/protobox/blob/40d8a5369be0d23de5360ed0b6b169ea3e41446c/lib/ansible/roles/php/tasks/ pear.yml#L26-L45最大的秘密是你可以使用和/或在Ansible` when`条件下. (2认同)

Kon*_*tin 2

从 Ansible v2 开始,有一个额外的模块用于管理 PEAR 扩展:http://docs.ansible.com/ansible/pear_module.html

所以现在你可以使用:

- pear: name=Net_URL2 state=latest
Run Code Online (Sandbox Code Playgroud)