Ansible tips and tricks: Difference between revisions
From Lolly's Wiki
Jump to navigationJump to search
No edit summary |
|||
(15 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
[[ | [[Category: Ansible | Tips and tricks]] | ||
= Ansible commandline = | |||
== Get settings for host == | |||
Gathering settings for host in ${hostname}: | === Show inventory for host === | ||
< | <syntaxhighlight lang=bash> | ||
$ ansible-inventory --host ${hostname} | |||
</syntaxhighlight> | |||
=== Gathering settings for host in ${hostname}: === | |||
<syntaxhighlight lang=bash> | |||
$ ansible -m debug -a 'var=hostvars[inventory_hostname]' ${hostname} | $ ansible -m debug -a 'var=hostvars[inventory_hostname]' ${hostname} | ||
</ | </syntaxhighlight> | ||
For example: | For example: | ||
< | <syntaxhighlight lang=bash> | ||
$ ansible -m debug -a 'var=hostvars[inventory_hostname]' localhost | $ ansible -m debug -a 'var=hostvars[inventory_hostname]' localhost | ||
</ | </syntaxhighlight> | ||
=== Gathering groups for host in ${hostname}: === | |||
<syntaxhighlight lang=bash> | |||
$ ansible -m debug -a 'var=group_names' ${hostname} | |||
</syntaxhighlight> | |||
== Get informations from host == | |||
=== Get all installed kernel versions: === | |||
<syntaxhighlight lang=bash> | |||
$ ansible -m shell -a 'uname -r' 'all' | perl -pe 's#\s+\|\s+CHANGED\s+\|\s+rc=\d+\s>>\s*\n#;#g' > /tmp/kernel.csv | |||
</syntaxhighlight> | |||
=== Get all installed releases: === | |||
<syntaxhighlight lang=bash> | |||
$ ansible -m setup -a 'filter=ansible_distribution_version' 'all' | |||
</syntaxhighlight> | |||
= Using ansible variables filled by gathering = | |||
== ansible_mounts == | |||
<syntaxhighlight lang=yaml> | |||
--- | |||
- hosts: all | |||
gather_facts: false | |||
vars: | |||
# query all cifs filesystems mounted under /media/cifs. See community.general.json_query below. | |||
query: "@[?fstype=='cifs'] | @[?starts_with(mount,'/media/cifs/')]" | |||
tasks: | |||
- name: "Just gather mounts" | |||
setup: | |||
gather_subset: | |||
- mounts | |||
- name: "Show all mounts matching query {{ query }}" | |||
debug: | |||
msg: "{{ item.options }}" | |||
loop: "{{ ansible_mounts | community.general.json_query(query) }}" | |||
loop_control: | |||
label: "{{ item.device }} -> {{ item.mount }}" | |||
</syntaxhighlight> | |||
= Gathering facts from file = | |||
== Variables from an Oracle response file == | |||
This snippet gets some variables from the response file and puts them into an environment variable <i>oracle_environment</i> and sets the variable name itself (prepended with oracle_ if not already there). The variable <i>oracle_environment</i> can be used for <i>environment:</i> when you use <i>shell:</i>. | This snippet gets some variables from the response file and puts them into an environment variable <i>oracle_environment</i> and sets the variable name itself (prepended with oracle_ if not already there). The variable <i>oracle_environment</i> can be used for <i>environment:</i> when you use <i>shell:</i>. | ||
< | <syntaxhighlight lang=yaml> | ||
vars: | vars: | ||
oracle_user: oracle | oracle_user: oracle | ||
oracle_version: 12cR2 | oracle_version: 12cR2 | ||
oracle_response_file: /install/tepmplate_{{ oracle_version }}/db_{{ oracle_version | lower}}.rsp | oracle_response_file: /install/tepmplate_{{ oracle_version }}/db_{{ oracle_version | lower}}.rsp | ||
</ | </syntaxhighlight> | ||
< | <syntaxhighlight lang=yaml> | ||
- name: "Getting variables for version {{ oracle_version }} from response file" | - name: "Getting variables for version {{ oracle_version }} from response file" | ||
shell: | | shell: | | ||
Line 46: | Line 89: | ||
- oracle | - oracle | ||
- oracle_install | - oracle_install | ||
</ | </syntaxhighlight> | ||
= Gathering oracle environment = | |||
< | <syntaxhighlight lang=yaml> | ||
- name: Calling oraenv | - name: Calling oraenv | ||
shell: | | shell: | | ||
Line 74: | Line 117: | ||
- debug: var=ora_env | - debug: var=ora_env | ||
</ | </syntaxhighlight> | ||
= NetApp Modules = | |||
== NetApp role == | |||
=== Snapshot user === | |||
< | <syntaxhighlight> | ||
security login role create -vserver cluster01 -role ansible-snapshot-only -cmddirname DEFAULT -access none | security login role create -vserver cluster01 -role ansible-snapshot-only -cmddirname DEFAULT -access none | ||
security login role create -vserver cluster01 -role ansible-snapshot-only -cmddirname "event generate-autosupport-log" -access all | security login role create -vserver cluster01 -role ansible-snapshot-only -cmddirname "event generate-autosupport-log" -access all | ||
Line 86: | Line 129: | ||
security login role create -vserver cluster01 -role ansible-snapshot-only -cmddirname "volume snapshot delete" -query "-snapshot ansible_*" -access all | security login role create -vserver cluster01 -role ansible-snapshot-only -cmddirname "volume snapshot delete" -query "-snapshot ansible_*" -access all | ||
security login create -vserver cluster01 -role ansible-snapshot-only -application ontapi -authentication-method password -user-or-group-name ansible-snapuser | security login create -vserver cluster01 -role ansible-snapshot-only -application ontapi -authentication-method password -user-or-group-name ansible-snapuser | ||
</ | </syntaxhighlight> |
Latest revision as of 08:13, 21 June 2024
Ansible commandline
Get settings for host
Show inventory for host
$ ansible-inventory --host ${hostname}
Gathering settings for host in ${hostname}:
$ ansible -m debug -a 'var=hostvars[inventory_hostname]' ${hostname}
For example:
$ ansible -m debug -a 'var=hostvars[inventory_hostname]' localhost
Gathering groups for host in ${hostname}:
$ ansible -m debug -a 'var=group_names' ${hostname}
Get informations from host
Get all installed kernel versions:
$ ansible -m shell -a 'uname -r' 'all' | perl -pe 's#\s+\|\s+CHANGED\s+\|\s+rc=\d+\s>>\s*\n#;#g' > /tmp/kernel.csv
Get all installed releases:
$ ansible -m setup -a 'filter=ansible_distribution_version' 'all'
Using ansible variables filled by gathering
ansible_mounts
---
- hosts: all
gather_facts: false
vars:
# query all cifs filesystems mounted under /media/cifs. See community.general.json_query below.
query: "@[?fstype=='cifs'] | @[?starts_with(mount,'/media/cifs/')]"
tasks:
- name: "Just gather mounts"
setup:
gather_subset:
- mounts
- name: "Show all mounts matching query {{ query }}"
debug:
msg: "{{ item.options }}"
loop: "{{ ansible_mounts | community.general.json_query(query) }}"
loop_control:
label: "{{ item.device }} -> {{ item.mount }}"
Gathering facts from file
Variables from an Oracle response file
This snippet gets some variables from the response file and puts them into an environment variable oracle_environment and sets the variable name itself (prepended with oracle_ if not already there). The variable oracle_environment can be used for environment: when you use shell:.
vars:
oracle_user: oracle
oracle_version: 12cR2
oracle_response_file: /install/tepmplate_{{ oracle_version }}/db_{{ oracle_version | lower}}.rsp
- name: "Getting variables for version {{ oracle_version }} from response file"
shell: |
awk -F '=' '/{{ item }}/{print $2;}' {{ oracle_response_file }}
register: oracle_response_variables
with_items:
- ORACLE_HOME
- ORACLE_BASE
- INVENTORY_LOCATION
tags:
- oracle
- oracle_install
- name: Setting facts from response file to oracle_environment
set_fact:
"{{ 'oracle_' + item.item | lower | regex_replace('oracle_','') }}": "{{ item.stdout }}"
oracle_environment: "{{oracle_environment|default([]) + [ {item.item: item.stdout} ] }}"
with_items:
- "{{ oracle_response_variables.results }}"
tags:
- oracle
- oracle_install
Gathering oracle environment
- name: Calling oraenv
shell: |
# Set ORAENV_ASK=NO and ORACLE_SID, ORACLE_HOME, PATH from /etc/oratab
eval $(awk -F':' '!/^[ ]*(#|$)/ && $3=="Y"{printf "export ORAENV_ASK=NO ORACLE_SID=%s ORACLE_HOME=%s PATH=${PATH}:%s/bin\n",$1,$2,$2}' /etc/oratab)
# Call /usr/local/bin/oraenv for additional settings
. /usr/local/bin/oraenv -s
# Just register what we need for Oracle
env | egrep "(ORACLE_.*|PATH|LD_LIBRARY_PATH)="
register: env
changed_when: False
- name: Creating environment ora_env
set_fact:
ora_env: |
{# Creating empty dictionary #}
{%- set tmp_env={} -%}
{# For each line from env call tmp_env._setitem_(<variable>,<value>) #}
{%- for line in env.stdout_lines -%}
{{ tmp_env.__setitem__(line.split('=')[0], line.split('=')[1]) }}
{%- endfor -%}
{# Print the created variable #}
{{ tmp_env }}
- debug: var=ora_env
NetApp Modules
NetApp role
Snapshot user
security login role create -vserver cluster01 -role ansible-snapshot-only -cmddirname DEFAULT -access none
security login role create -vserver cluster01 -role ansible-snapshot-only -cmddirname "event generate-autosupport-log" -access all
security login role create -vserver cluster01 -role ansible-snapshot-only -cmddirname "volume snapshot" -access readonly
security login role create -vserver cluster01 -role ansible-snapshot-only -cmddirname "volume snapshot create" -query "-snapshot ansible_*" -access all
security login role create -vserver cluster01 -role ansible-snapshot-only -cmddirname "volume snapshot delete" -query "-snapshot ansible_*" -access all
security login create -vserver cluster01 -role ansible-snapshot-only -application ontapi -authentication-method password -user-or-group-name ansible-snapuser