Ansible tips and tricks: Difference between revisions

From Lolly's Wiki
Jump to navigationJump to search
No edit summary
Line 1: Line 1:
== Gathering facts from file ==
== Gathering facts from file ==
=== Variables from an Oracle response 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>.
<source lang=yaml>
  vars:
    oracle_user:          oracle
    oracle_version:      12cR2
    oracle_response_file: /install/tepmplate_{{ oracle_version }}/db_{{ oracle_version | lower}}.rsp
</source>
<source lang=yaml>
<source lang=yaml>
   - name: "Get variables for version {{ oracle_version }} from response file"
   - name: "Getting variables for version {{ oracle_version }} from response file"
     shell: |
     shell: |
       awk -F '=' '/{{ item }}/{print $2;}' /install/tepmplate_{{ oracle_version }}/db_{{ oracle_version | lower}}.rsp
       awk -F '=' '/{{ item }}/{print $2;}' {{ oracle_response_file }}
     register: oracle_response_variables
     register: oracle_response_variables
     with_items:
     with_items:
Line 14: Line 23:
       - oracle_install
       - oracle_install


   - name: Set facts from response file to oracle_environment
   - name: Setting facts from response file to oracle_environment
     set_fact:
     set_fact:
       "{{ 'oracle_' + item.item | lower | regex_replace('oracle_','') }}": "{{ item.stdout }}"
       "{{ 'oracle_' + item.item | lower | regex_replace('oracle_','') }}": "{{ item.stdout }}"
Line 24: Line 33:
       - oracle_install
       - oracle_install
</source>
</source>
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>.




[[ Kategorie: Ansible | Tips and tricks ]]
[[ Kategorie: Ansible | Tips and tricks ]]

Revision as of 11:19, 19 April 2017

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


Tips and tricks