Reputation: 97
I am creating an ansible-playbook that installs elasticsearch package. My playbook works fine for any one version of the elasticsearch. What I want to do is create an ansible-playbook that installs any version that the user specifies. Basically I want to give user the choice to choose any version he wants.
The playbook-below install only version 7.x.x
- name: installing elastic
hosts: <myhost>
tasks:
- name: Import the Elasticsearch PGP Key
shell: |
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
args:
warn: no
- name: Add repository
copy:
dest: /etc/apt/sources.list.d/elastic-7.x.list
content: |
deb https://artifacts.elastic.co/packages/7.x/apt stable main
- name: Install the Elasticsearch package
apt:
name: elasticsearch
state: present
update_cache: yes
I tried adding a variable file that has versions and replacing that specific version in playbook with variable names as shown below. (It doesnt work though)
- name: installing elastic
hosts: <myhost>
vars_files:
- version_var.yaml
tasks:
- name: Import the Elasticsearch PGP Key
shell: |
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
args:
warn: no
- name: Add repository
copy:
dest: /etc/apt/sources.list.d/elastic-{{version_7}}.list
content: |
deb https://artifacts.elastic.co/packages/{{version_7}}/apt stable main
- name: Install the Elasticsearch package
apt:
name: elasticsearch
state: present
update_cache: yes
Contents of version_var.yaml file
version_6: 6.x
version_7: 7.x
I believe there is some sensible way to do this, maybe use conditions. Anyone who can point me to right direction?
Upvotes: 0
Views: 1144
Reputation: 838
It would be helpful if you share error output or what exactly doesn't work through.
I've tested your playbook and it worked correctly using ansible [core 2.13.1]
.
.
├── elastic.yml
└── version_var.yaml
elastic.yml
- hosts: localhost
vars_files:
- version_var.yaml
tasks:
- debug:
msg: "{{ version_7 }}"
TASK [debug] ******************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "7.x"
}
Anyway, there is a better way to get the major version number:
- hosts: localhost
gather_facts: no
vars:
elastic_version: 7.16.2
elastic_major_version: "{{ elastic_version.split('.')[0] }}.x"
tasks:
- debug:
msg: "{{ elastic_major_version }}"
The output:
TASK [debug] ******************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "7.x"
}
Always consider using modules instead of shell commands:
- hosts: myhosts
gather_facts: no
vars:
elastic_version: 7.16.2
elastic_major_version: "{{ elastic_version.split('.')[0] }}.x"
tasks:
- name: Import the Elasticsearch PGP Key
apt_key:
url: "https://artifacts.elastic.co/GPG-KEY-elasticsearch"
state: present
- name: Add repository
apt_repository:
repo: "deb https://artifacts.elastic.co/packages/{{ elastic_major_version }}/apt stable main"
state: present
update_cache: yes
- name: Install the Elasticsearch package
apt:
name: elasticsearch
state: present
update_cache: yes
Upvotes: 1