Osmc media player needs a specific path for playbooks
https://github.com/osmc/osmc/issues/319
environment:
PATH: "{{ ansible_env.PATH }}:/sbin:/usr/sbin"
I was wondering whether I can set this as an environmental variable in the inventory for those machines, rather than have it in every playbook or create separate playbooks.
In common usage - is that path likely to cause problems for general *nix machines if it is implemented on non-osmc installations?
If you can't set this an an inventory variable:
Is that just because it's no implemented/ useful to most?
Or because the inventory has no relation to path - e.g. it's not invoked at that point?
Or is a better way for all of this to have it as a machine specific variable/ task in a role?
How would that look please?
New to ansible and still trying to get my head round some of the concepts.
As said, the environment keyword can be used only at task or playbook level.
You will be able to use an standard playbook just adding the following:
---
- name: Environment
hosts: localhost
connection: local
gather_facts: False
tasks:
- name: Setup
setup:
gather_subset:
- "!all"
or
---
- name: Environment
hosts: localhost
connection: local
gather_facts: True
gather_subset:
- "!all"
If you debug the variable:
---
- name: Environment
hosts: localhost
connection: local
gather_facts: False
tasks:
- name: Setup
setup:
gather_subset:
- "!all"
- name: Debug
debug:
var: ansible_env.PATH
You will get something like:
TASK [Setup] *******************************************************************************************************************************************************
ok: [localhost]
TASK [Debug] *******************************************************************************************************************************************************
ok: [localhost] => {
"ansible_env.PATH": "/Users/imjoseangel/source/venv/ansible/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
}
And what if you want to pass that variable to another play with different inventory?
Just do hostvars.localhost.ansible_env.PATH
- name: Environment2
hosts: windows
connection: local
gather_facts: False
tasks:
- name: Debug
debug:
var: hostvars.localhost.ansible_env.PATH
So the
environment:
PATH: "{{ ansible_env.PATH }}:/sbin:/usr/sbin"
Will be valid only with gather_facts or setup module under the defined inventory but you don't need to split playbooks.
Related
Getting below warning when trying to run serverless.
Serverless Warning --------------------------------------------
A valid option to satisfy the declaration 'opt:stage' could not be found.
Below is my serverless.yml file
# Serverless Config
service: api-service
# Provider
provider:
name: aws
runtime: nodejs8.10
region: ${opt:region, 'ap-east-1'}
stage: ${opt:stage, 'dev'}
# Enviroment Varibles
environment:
STAGE: ${self:custom.myStage}
MONGO_DB_URI: ${file(./serverless.env.yml):${opt:stage}.MONGO_DB_URI}
LAMBDA_ONLINE: ${file(./serverless.env.yml):${opt:stage}.LAMBDA_ONLINE}
# Constants Varibles
custom:
# environments Variables used for convert string in upper case format
environments:
myStage: ${opt:stage, self:provider.stage}
stages:
- dev
- qa
- staging
- production
region:
dev: 'ap-east-1'
stage: 'ap-east-1'
production: 'ap-east-1'
# Function
functions:
testFunc:
handler: index.handler
description: ${opt:stage} API's
events:
- http:
method: any
path: /{proxy+}
cors:
origin: '*'
#package
package:
exclude:
- .env
- node_modules/aws-sdk/**
- node_modules/**
In the description of the testFunc you're using ${opt:stage}. If you use that directly you need to pass the --stage flag when you run the deploy command.
What you should do there is to use the ${self:provider.stage}, because there you will have the stage calculated.
I will suggest you to do below implementation
provider:
name: aws
runtime: nodejs8.10
region: ${opt:region, self:custom.environments.region.${self:custom.environments.myStage}}
stage: ${opt:stage, self:custom.environments.myStage}
# Enviroment Varibles
environment:
STAGE: ${self:custom.myStage}
MONGO_DB_URI: ${file(./serverless.env.yml):${self:provider.stage}.MONGO_DB_URI}
LAMBDA_ONLINE: ${file(./serverless.env.yml):${self:provider.stage}.LAMBDA_ONLINE}
# Constants Varibles
custom:
# environments Variables used for convert string in upper case format
environments:
# set the default stage if not specified
myStage: dev
stages:
- dev
- qa
- staging
- production
region:
dev: 'ap-east-1'
stage: 'ap-east-1'
production: 'ap-east-1'
Basically, if stage and region is not specified using command line, then use defaults. Otherwise the command line one will be used.
I was trying to use vars_prompt in Ansible with default values taken from facts (or otherwise a previously defined variable). The playbook is intended be used as an ad-hoc one for initial provisioning.
My playbook:
---
- hosts: server01
gather_facts: True
vars_prompt:
- name: new_hostname
prompt: please enter the name for the target
default: "{{ ansible_hostname }}"
private: no
tasks:
- debug: msg="{{ new_hostname }}"
Current result:
please enter the name for the target [{{ ansible_hostname }}]:
ERROR! 'ansible_hostname' is undefined
Expected results (assuming ansible_hostname=server01:
please enter the name for the target [server01]:
Is it possible to achieve in Ansible?
This can be implemented using the pause module:
---
- hosts: server01
gather_facts: True
tasks:
- pause:
prompt: please enter the name for the target [{{ ansible_hostname }}]
register: prompt
- debug:
msg: "{{ prompt.user_input if prompt.user_input else ansible_hostname }}"
This has been answered before here on Stack.
ansible get aws ebs volume id which already exist
Get volume id from newly created ebs volume using ansible
For the life of me I am trying ec2_vol.volume_id and some other jmespath query bits but not getting the right output help. I just want the vol id. Nothing more.
---
- hosts: localhost
connection: local
gather_facts: no
tasks:
- name: get associated vols
ec2_vol:
instance: i-xxxxxxxxxxxxx
state: list
profile: default
region: us-east-1
register: ec2_vol
- debug:
msg: "{{ ec2_vol.volume_id }}"
also doesn't work
---
- hosts: localhost
connection: local
gather_facts: no
tasks:
- name: get associated vols
ec2_vol:
instance: i-xxxxxxxxxxxxxx
state: list
profile: default
region: us-east-1
register: ec2_vol
- debug: msg="{{ item.volume_id }}"
with_items: ec2_vol.results
Ansible 2.2 and 2.3 tested
Taking bits from the prior answer you will need to understand JMESPATH like filtering to get what you want out of the output.
Here is the answer
---
- hosts: localhost
connection: local
gather_facts: no
tasks:
- name: get associated vols
ec2_vol:
instance: i-xxxxxxxxxxxxxx
state: list
profile: default
region: us-east-1
register: ec2_vol
- debug: msg="{{ ec2_vol.volumes | map(attribute='id') | list }}"
How do we extend the scope of the wait_items to span multiple task items with a wait_for (sleep) in between.
---
- hosts: localhost
connection: local
gather_facts: false
tasks:
- name: Debug1
debug: msg="Debug1 - {{item}} " <-- Errors out here
- name: Wait Buffer
wait_for: timeout=10
with_items: ["dir1","dir2"]
The idea has to perform a task sequentially from the list and wait couple of seconds and then move the next item
How do we extend the scope of the wait_items to span multiple task items with a wait_for (sleep) in between.
You should use shell module with 'with_items' and add there sleep or wait until file will exit.
PS:
Fixed version:
---
- hosts: localhost
connection: local
gather_facts: false
tasks:
- name: Debug1
debug: msg="Debug1 - {{item}} "
with_items: ["dir1","dir2"]
- name: Wait Buffer
wait_for: timeout=10
I'm attempting to store all my environment variables in a file called variables.yml that looks like so:
---
doo: "external"
Then I have a playbook like so:
---
- hosts: localhost
tasks:
- name: "i can totally echo"
environment:
include: variables.yml
ugh: 'internal'
shell: echo "$doo vs $ugh"
register: results
- debug: msg="{{ results.stdout }}"
The result of the echo is ' vs internal'.
How can I change this so that the result is 'external vs internal'. Many thanks!
Assuming the external variable file called variables.ext is structured as follow
---
EXTERNAL:
DOO: "external"
than, according Setting the remote environment and Load variables from files, dynamically within a task a small test could look like
---
- hosts: localhost
become: false
gather_facts: false
tasks:
- name: Load environment variables
include_vars:
file: variables.ext
- name: Echo variables
shell:
cmd: 'echo "${DOO} vs ${UGH}"'
environment:
DOO: "{{ EXTERNAL.DOO }}"
UGH: "internal"
register: result
- name: Show result
debug:
msg: "{{ result.stdout }}"
resulting into an output of
TASK [Show result] ********
ok: [localhost] =>
msg: external vs internal