--- # ANSIBLE_LOCALHOST_WARNING=false ansible-playbook test.yml - hosts: 127.0.0.1 connection: local gather_facts: no tasks: - set_fact: myList: [ 'a', 'b', 'c', '4', 'd', '9' ] groceryList: [ 'fruit', 'vegetables', 'milk', 'FAT', 'SALT', 'SUGAR' ] unhealthyFood: [ 'FAT', 'SALT', 'SUGAR' ] - set_fact: rejectSingleItem: "{{ myList | reject('search', '4') | list }}" rejectNonMatching: "{{ myList | reject('match', '[^a-z]') | list }}" healthyGroceryList_forLoop: "[ {% for item in groceryList if item not in unhealthyFood %}'{{ item }}'{{ ', ' if not loop.last else '' }}{% endfor %} ]" healthyGroceryList_reject: "{{ groceryList | reject('in', unhealthyFood) | list }}" - debug: var: "{{ item }}" loop: - rejectSingleItem - rejectNonMatching - healthyGroceryList_forLoop - healthyGroceryList_reject ...
TASK [debug] ***************************************************** ok: [127.0.0.1] => (item=rejectSingleItem) => { "ansible_loop_var": "item", "item": "rejectSingleItem", "rejectSingleItem": [ "a", "b", "c", "d", "9" ] } ok: [127.0.0.1] => (item=rejectNonMatching) => { "ansible_loop_var": "item", "item": "rejectNonMatching", "rejectNonMatching": [ "a", "b", "c", "d" ] } ok: [127.0.0.1] => (item=healthyGroceryList_forLoop) => { "ansible_loop_var": "item", "healthyGroceryList_forLoop": [ "fruit", "vegetables", "milk" ], "item": "healthyGroceryList_forLoop" } ok: [127.0.0.1] => (item=healthyGroceryList_reject) => { "ansible_loop_var": "item", "healthyGroceryList_reject": [ "fruit", "vegetables", "milk" ], "item": "healthyGroceryList_reject" }
Code | Output | Notes |
---|---|---|
- fail: msg: "ALERT!" |
TASK [fail] ********************************************************************************************* fatal: [myHost]: FAILED! => {"changed": false, "msg": "ALERT!"} PLAY RECAP ********************************************************************************************** myHost : ok=n changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0 |
|
- fail: msg: "ALERT!" ignore_errors: true |
TASK [fail] ********************************************************************************************* fatal: [myHost]: FAILED! => {"changed": false, "msg": "ALERT!"} ...ignoringfollowing tasks, then finally : PLAY RECAP ********************************************************************************************** myHost : ok=n changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1 |
|
- block: - fail: msg: "ALERT!" rescue: - debug: msg: "RESCUE" |
TASK [fail] ********************************************************************************************* fatal: [myHost]: FAILED! => {"changed": false, "msg": "ALERT!"} TASK [debug] ******************************************************************************************** ok: [myHost] => { "msg": "RESCUE" }following tasks, then finally : PLAY RECAP ********************************************************************************************** myHost : ok=n changed=0 unreachable=0 failed=0 skipped=0 rescued=1 ignored=0 |
|
- mail: |
(nothing special, just like any other task) |
|
Target | Syntax | Comment |
---|---|---|
by name | ||
a list of hosts | ansible host1,host2,host3 | |
a list of hosts matching a regular expression | ansible '~server0[12]\.acme\.org' |
|
by group | ||
all hosts of a single group | ansible groupName | |
all hosts known to Ansible | ansible all | |
hosts of several groups | ansible group1:group2:group3 | The colon : actually means a logical OR. This command above applies to any host belonging either to group1 or to group2 or to group3 When it comes to complex rules with intersections and exclusions (see examples), it may not be a REAL "logical OR" |
hosts belonging to the 2 specified groups | ansible 'group1:&group2' |
|
by name + group | ||
all hosts except those matching an expression |
|
|
all hosts of a group except severalof them |
ansible 'groupName:!~(host1|host2)' |
|
- hosts: 127.0.0.1 connection: local gather_facts: no tasks: - set_fact: fruits: [ 'apple', 'orange', 'banana', ] - debug: var: item loop: - fruits - debug: var: item loop: - "{{ fruits }}" - debug: var: item loop: "{{ fruits }}"
TASK [debug] ******************************************************************** ok: [127.0.0.1] => (item=fruits) => { "item": "fruits" } TASK [debug] ******************************************************************** ok: [127.0.0.1] => (item=[u'apple', u'orange', u'banana']) => { "item": [ "apple", "orange", "banana" ] } TASK [debug] ******************************************************************** ok: [127.0.0.1] => (item=apple) => { "item": "apple" } ok: [127.0.0.1] => (item=orange) => { "item": "orange" } ok: [127.0.0.1] => (item=banana) => { "item": "banana" }
fruits
variable"{{ }}"
to pass a variable-
introduces a list item- fail: msg: "Execution stopped on purpose for whatever reason"
- fail:
fail
command, none of the following commands will be executedfail
, the playbook execution stops. Period.fail
— the execution continued (... or at least _seemed_ to continue). Here's what happened then :
fail
task, everything should go extremely wellfail
fail
, I thought the execution of the task I edited file continued.- meta: end_playBut it's fairly different from the
fail
method :
failureexit status
[defaults] host_key_checking = False inventory = /home/stuart/ansible/hosts roles_path = /home/stuart/ansible/roles vault_password_file = /var/lib/ansible/.vault_password
- name: unmount volume groups mount: src: "{{ item.device }}" name: "{{ item.mountPoint }}" will be path in Ansible 2.3+ state: unmounted with_items: - { device: '/dev/mapper/vg_data-data', mountPoint: '/var/lib/docker/devicemapper' } - { device: '/dev/mapper/vg_data-data', mountPoint: '/var/lib/docker' }