-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathansible
137 lines (114 loc) · 4.38 KB
/
ansible
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# Check connetctions to hosts
ansible all -i inventory.tmp -m ping -k
# Run command on remote nodes
ansible all -i inventory.tmp -kb --ask-become-pass -m command -a "ls /etc/httpd/conf/"
# List all vars per host
ansible sjc01-c01-gws31 -i production -m debug -a "var=hostvars['sjc01-c01-gws31']"
# get list of packages from one server
function get_packages () { ansible $1 -i $2 -m command -a "rpm -qa" | grep -v $1 | sort > ~/tmp/rpm_check/$1 ; }
# Change callback
export ANSIBLE_LOAD_CALLBACK_PLUGINS=1
ANSIBLE_STDOUT_CALLBACK=minimal /usr/bin/ansible-playbook ${WORKSPACE}/Ansible/Revitas/install.yml --tags "${Tags}"
timer - callback that show only execution time of playbook
# We can use jinja2 inside ansible strings
- name: run some command
command: echo item {{item}}
register: my_result
with_items:
- 1
- 2
- 3
- name: my debug
debug:
msg: "{% set output = [] %}\
{% for x in (my_result.results | map(attribute='stdout')) %}\
{{ output.append( 'The result was: ' ~ x ) }}\
{% endfor %}\
{{output}}"
Output is something like: -
ok: [host1]: => {
"msg": [
"the result was: item 1",
"the result was: item 2",
"the result was: item 3"
]
}
ok: [host2]: => {
"msg": [
"the result was: item 1",
"the result was: item 2",
"the result was: item 3"
]
}
#Multiline strings in ansible
https://yaml-multiline.info/
# if you need to show dict it is better to transform it to nice json
- fail:
msg: "Cannot determine LS url properly. \n {{ lsExternalName_out | to_nice_json }}"
when: lsExternalName_out.stdout_lines | length != 1
# Run task one by one for each host. play_hosts - list of hosts
- name: run {{ keycloak_service }} service
service: name="{{ keycloak_service }}" enabled=yes state=started
delegate_to: "{{ item }}"
with_items: "{{ play_hosts }}"
run_once: True
# And with role
- name: Iterate sg-mount role
include_role:
name: sg-mount
vars:
instanceID: "{{ hostvars[item].ec2_id }}"
file_share_id: "share-869511EC"
mount_point: "{{ shared_dir }}"
s3path: "{{ externalName }}"
mountAction: "create"
delegate_to: "{{ item }}"
with_items: "{{ ansible_play_hosts }}"
run_once: True
tags: cluster_prerequisits
# Variable for all hosts in group
- name: Set hugememory value
set_fact:
rac_hugememory: "{{ hugememory.stdout }}"
- set_fact: rac_hugememories="{{ rac_hugememories | default([]) + [ hostvars[item]['rac_hugememory'] | int ] }}"
with_items: "{{ groups['ec2Oracle'] }}"
- debug: msg="mem={{ rac_hugememories }}"
# Check for pattern
- fail: msg="There is conflicts {{ outer_item.patchid }}"
when: 'oracle_conflict_patches.stdout is search("Prereq.+failed")'
# true false check
- { role: revitas-flexbi, cognos_version: "{{ cognosVersion }}",
BIlicensedCustomer: "{{ True if 'analyzer' in subscription else False }}",
tags: ['ecmbi'] }
# regex search in failed_when in multiline output
register: ecmf_configmigrate
failed_when: >
( not ecmf_configmigrate.stdout | regex_search("^BUILD SUCCESSFUL", multiline=True) ) and
("No such file or directory" not in ecmf_configmigrate.stdout )
changed_when: >
( ecmf_configmigrate.stdout | regex_search("^BUILD SUCCESSFUL", multiline=True) )
# Validate that string in the output
- name: try https://{{ bastion_dns }}
uri:
url: "https://{{ bastion_dns }}/signin"
return_content: yes
validate_certs: no
register: result
retries: 3
delay: 10
until: result.content.find("Subspace") != -1
# indexed item - https://docs.ansible.com/ansible/latest/plugins/lookup/indexed_items.htmlhttps://docs.ansible.com/ansible/latest/plugins/lookup/indexed_items.html
- name: indexed loop demo
debug:
msg: "at array position {{ item.0 }} there is a value {{ item.1 }}"
with_indexed_items:
- "{{ some_list }}"
# create dict inside jinja2
recovery_keys: "{% set output = dict() %}
{% for line in init_out.stdout_lines -%}
{% if line.find('Recovery Key') != -1 -%}
{% set index = output | length %}
{% set _ = output.update( { 'recovery_key_' + index | string : line.split(':')[1] | trim } ) %}
{%- endif %}
{%- endfor %}
{{ output }}"