I'm currently installing Pear packages for PHP with Ansible like so:
- 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
The ignore_errors
is required because pear always reports an error when running a command that's been run/completed successfully before (like:
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
Is there a better (more idempotent) way to run pear commands, and not have to scroll through a bunch of big, red ignored errors?
Okay, so after playing around with the changed_when property a bit, I finally found a solution to this (was testing on a different playbook, where I was installing drush instead of Phergie, but the problem/solution is exactly the same:
Playbook:
- 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"
New output from Ansible:
TASK: [Setup drush PEAR channel.] *********************************************
ok: [midwesternmac]
TASK: [Install drush.] ********************************************************
ok: [midwesternmac]
So now in the summary, instead of reporting an extra 'changed' for every server and every pear command, Ansible only reports 'changed' iff there was a change. More documentation (though sparse) on changed_when and failed_when (which require Ansible >= 1.3) is available here: Error Handling In Playbooks
Starting from Ansible v2 there is an extra module for managing PEAR extensions: http://docs.ansible.com/ansible/pear_module.html
So now you can just use:
- pear: name=Net_URL2 state=latest
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With