how to display a message in ansible - ansible

I'm trying to debug the ansible_hostname variable in order to have the names of the different hosts to display.
unfortunately I am unable for the moment to display the value of the variable even the name I wrote in the task (:
- name: display variable
debug:
msg: "value variable {{ ansible_hostname }}"
verbosity: 4
tags:
- attrest
do i ve to add a parameters in my playbook or in other files ?

msg: value variable "{{ ansible_hostname }}"
Parenthesis to add in the playbook where variable is used

Related

Use value of CLI variable as the name of a host_vars variable?

Is there a way to use the value of one Ansible variable as the name of another variable so I can extract a value from its list?
host_vars:
this:
does: walk
says: hi
that:
does: run
says: hello
On the CLI when I run the playbook, I add -e="thing=this".
In the playbook, I've tried all manner of things to expand the variable thing to its value this, then use this to extract the value of does in the host_vars file.
Using the variable name directly obviously works:
- name: Check what the thing does
debug:
msg: "{{ this['does'] }}"
But the following do not:
{{ thing['does'] }}
{{ {{ thing }}['does'] }}
Those, plus several other iterations I've tried all either throw an error or print out the literal string.
You need the vars lookup plugin to address variables indirectly. See
shell> ansible-doc -t lookup vars
For example,
- debug:
msg: "{{ lookup('vars', thing).does }}"
should give (abridged)
shell> ansible-playbook pb.yml -e "thing=this"
...
msg: walk
Example of a complete playbook for testing
- hosts: localhost
vars:
this:
does: walk
says: hi
that:
does: run
says: hello
tasks:
- debug:
msg: "{{ lookup('vars', thing).does }}"
You can simplify the code further by putting all declarations into the vars. For example, into the group_vars/all
shell> cat group_vars/all/thing.yml
this:
does: walk
says: hi
that:
does: run
says: hello
_thing: "{{ lookup('vars', thing) }}"
Then, the simplified playbook below gives the same results
- hosts: localhost
tasks:
- debug:
var: _thing.does

Ansible: variable is defined but error shows not defined

I have an ansible playbook as follows:
---
- name: test variables
hosts: all
tasks:
- name: test
command: echo {{ ansible_role_dir }}
register: result
- name: show result
debug:
msg="{{ result.stdout }}"
#roles: #line12
# - "{{ ansible_role_dir }}/testrole" #line13
the variable ansible_role_dir is defined under group_vars/all.yaml.
if I run the playbook which comments out the line12 and line13, it shows the result of variable correctly. Obviously it knows where the variable ansible_role_dir is defined. But if I uncomment line12 and line13, it shows error ERROR! 'ansible_role_dir' is undefined. Why it does not know where the ansible_role_dir is defined this time?
Ansible does not like jinja tags without quotes. try this:
---
- name: test variables
hosts: all
tasks:
- name: test
command: "echo {{ ansible_role_dir }}"
register: result
- name: show result
debug:
msg="{{ result.stdout }}"
#roles: #line12
# - "{{ ansible_role_dir }}/testrole" #line13
EDIT:
sorry, i think i misread the question.
have you tried putting in the real path instead of the var, just to see if it works? usually role paths are given in ansible's config, i never saw them run with a direct path

ansible: check if one of array elements is in variable string

I've got a list in ansible:
ssh_port_patterns: [
"domain.tld",
"example.tld",
"something.tld"
]
and I'd like to run one task, only if its inventory_hostname will containt one of these items phrase. For example task should be run when inventory_hostname will be set to: extra.domain.tld, but shouldn't in case of something.else.tld
Is it possible to make such check with ansible?
There is an Ansible fact for ansible_domain which usually contains the domain suffix of the host on which Ansible is performing tasks. The simplest way would be to match this against the list ssh_port_patterns.
Example debug message will only run when the suffix of hostname is in the list:
- debug:
msg: "run on {{ ansible_hostname }}"
when: ansible_domain in ssh_port_patterns
Update:
If this has to be achieved only with inventory_hostname for whatever reeason, then split and join can be used to form the domain suffix and matched with the list.
- debug:
msg: "run on {{ ansible_hostname }}"
when: inventory_hostname.split('.')[1:]|join('.') in ssh_port_patterns

Overwrite ansible variable from file fails

I'm trying to overwrite a previously defined variable my_var (when it's set to LATEST) by loading a value from a file (containing, say, NEWVALUE).
- name: Load from file
vars:
my_var: "{{ lookup('file', '~/file.txt') }}"
my_var2: "{{ lookup('file', '~/file.txt') }}"
debug: msg="my_var is {{ my_var }} my_var2 is {{ my_var2 }}"
when: "{{ my_var=='LATEST' }}"
This prints
ok: [host] ==> {
"msg": "my_var is LATEST my_var2 is NEWVALUE"
}
So I feel that I've verified that I'm loading the value correctly.. but for some reason I can't set the result of lookup in a previously set variable. Disabling the when clause doesn't seem to make any difference.
Should I be able to do this? As an alternative I'm going to use a third variable and just set it to either the preexisting value or the value from the file - but this seems like an unnecessary step to me.
Ansible version 2.1.0.0 b.t.w.
The vars you defined in your example only are available for the single debug task.
As you mentioned in the comment you figured this out and used set_fact instead. And yes, this won't work if you have passed the same variable as extra-var, as it has the highest precedence. There is no way to override a variable you passed as extra-var.

How can Ansible "register" in a variable the result of including a playbook?

How can an Ansible playbook register in a variable the result of including another playbook?
For example, would the following register the result of executing tasks/foo.yml in result_of_foo?
tasks:
- include: tasks/foo.yml
- register: result_of_foo
How else can Ansible record the result of a task sequence?
The short answer is that this can't be done.
The register statement is used to store the output of a single task into a variable. The exact contents of the registered variable can vary widely depending on the type of task (for example a shell task will include stdout & stderr output from the command you run in the registered variable, while the stat task will provide details of the file that is passed to the task).
If you have an include file with an arbitrary number of tasks within it then Ansible would have no way of knowing what to store in the variable in your example.
Each individual task within your include file can register variables, and you can reference those variables elsewhere, so there's really no need to even do something like this.
I was able to do this by passing a variable name as a variable to be used in the task. I included my main.yaml and included cgw.yaml files below.
main.yaml:
- name: Create App A CGW
include: cgw.yaml
vars:
bgp_asn: "{{ asn_spoke }}"
ip_address: "{{ eip_app_a.public_ip }}"
name: cgw-app-a
region: "{{ aws_region }}"
aws_access_key: "{{ ec2_access_key }}"
aws_secret_key: "{{ ec2_secret_key }}"
register: cgw_app_a
cgw.yaml:
- name: "{{ name }}"
ec2_customer_gateway:
bgp_asn: "{{ bgp_asn }}"
ip_address: "{{ ip_address }}"
name: "{{ name }}"
region: "{{ region }}"
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
register: "{{ register }}"

Resources