Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
How do I set the default value to a variable? The following does not work:
{{ var_name | default( another_var) }}
another_var is a variable - how do I expand it?
According the documentation Providing default values
you can now simply use a default with a value in a nested data structure
and a test with an
Example
---
- hosts: localhost
become: false
gather_facts: false
vars:
DEFAULT: "test"
tasks:
- name: Define and show default value
debug:
msg: "{{ VAR | default(DEFAULT) }}"
resulting in an output of
TASK [Define and show default value] ******
ok: [localhost] =>
msg: test
it is working as described.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last month.
Improve this question
Expected output is the version of nginx, but it outputs a blank string instead:
- hosts: localhost
tasks:
- name: check if nginx exists
stat:
path: /usr/sbin/nginx
register: result
- name: check nginx version
command: nginx -v
register: version
- debug: msg="{{version.stdout}}"
I am getting the below output instead:
TASK [debug] *********************************************************************************************************************
ok: [localhost] => {
"msg": ""
}
It's because nginx -v sends its output to stderr, as one can see with
$ nginx -v >/dev/null
nginx version: nginx/1.23.3
$ nginx -v 2>/dev/null
$
so your output is actually in version.stderr, as you would have seen for yourself had you increased the ansible verbosity or used
- debug: var=version
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 months ago.
Improve this question
When, I execute a playbook, I got below error:
➜ ansible ansible-playbook site.yml
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
ERROR! A malformed role declaration was encountered.
The error appears to be in '/Users/channa/random_poc/ansible/site.yml': line 2, column 4, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
---
- name: "Play 1"
^ here
I have checked and tried changing my own my playbook, site.xml, by referring some example found online. But, nothing seems to be working out and I am always getting the same error.
Here's my playbook site.xml:
---
- name: "Play 1"
hosts: localhost
roles:
— myuser
Can someone explain me what I am doing wrong?
There's an issue, with your "-" character.
The following works:
---
- name: "Play 1"
hosts: localhost
roles:
- myuser
Note the difference, between:
— myuser
And
- myuser
And as suggested by β.εηοιτ.βε, execute with:
ansible-playbook /path/to/playbook
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
I try to subtract 2 variables in Ansible, but what I get is zero in the result.
- name: ansible calculate space
hosts: pghosts
gather_facts: false
tasks:
- name: Check /srv freespace
shell: df /srv --output\=avail | tail -1
register: srv_freespace
- name: Check postgres data dir size
shell: du -ks /srv/postgresql/data |awk '{ print $1 }'
register: postgres_data
- debug:
msg: "substraction {{ srv_freespace|int - postgres_data|int }}"
- copy: content="{{ srv_freespace|int - postgres_data|int }}" dest=/tmp/results.txt
Try changing the last line to:
- copy: content="{{ srv_freespace.stdout|int - postgres_data.stdout|int }}" dest=/tmp/results.txt
Explanation: the registered variable is not a string, its a dict. The dict includes several keys like the command name, command start and end time, exit code and other good stuff. The actual value i.e. what is printed to standard output is under the stdout key.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have an ansible task that writes 2 lines into journald.conf, however it doesn't perform idempotency when running again.
I have seen the following questions that didn't work for me:
Ansible lineinfile duplicates line
Idempotency in ansible playbook
https://github.com/ansible/ansible/issues/4531
My regex seems to be okay, you can see below my task:
- name: set cpu affinity settings in systemd
lineinfile:
dest: /etc/systemd/journald.conf
line: "{{ item.key }}={{ item.value }}"
regexp: "^#?{{ item.value }}"
state: present
with_dict:
RateLimitIntervalSec: 0
RateLimitBurst: 0
tags: journald
notify: restart journald
The expected behavior should be: keep the commented line and add new ones at the end of the file with the items in the list, unless the uncommented lines already exists.
My file journald.conf file is like this:
[Journal]
#Storage=auto
#Compress=yes
#Seal=yes
#SplitMode=uid
#SyncIntervalSec=5m
#RateLimitIntervalSec=30s
#RateLimitBurst=1000
#SystemMaxUse=
#SystemKeepFree=
#SystemMaxFileSize=
#SystemMaxFiles=100
#RuntimeMaxUse=
#RuntimeKeepFree=
#RuntimeMaxFileSize=
#RuntimeMaxFiles=100
#MaxRetentionSec=
#MaxFileSec=1month
#ForwardToSyslog=yes
#ForwardToKMsg=no
#ForwardToConsole=no
#ForwardToWall=yes
#TTYPath=/dev/console
#MaxLevelStore=debug
#MaxLevelSyslog=debug
#MaxLevelKMsg=notice
#MaxLevelConsole=info
#MaxLevelWall=emerg
#LineMax=48K
RateLimitIntervalSec=0
RateLimitBurst=0
RateLimitIntervalSec=0
RateLimitBurst=0
RateLimitIntervalSec=0
RateLimitBurst=0
RateLimitIntervalSec=0
RateLimitBurst=0
I tried to use the parameter backrefs: yes as suggested in above mentioned articles, but it performs idempotency every time, even when there is no any uncommented line.
Do you guys have any suggestions?
I'm using ansible 2.9.0
I'd suggest an alternative approach - use the ini_file module, as settings in journald.conf are INI style key=value (with a section as well). This will simplify the task required and be idempotent as well.
Example:
- name: set cpu affinity settings in systemd
ini_file:
path: /etc/systemd/journald.conf
section: Journal
option: "{{ item.key }}"
value: "{{ item.value }}"
no_extra_spaces: yes
with_dict:
RateLimitIntervalSec: 0
RateLimitBurst: 0
Note: If you want a reference of settings prior to change, add backup: yes to the task.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
All,
My playbook is almost ready. Only the last line with alias is giving me issues. I tried to look at what I am doing wrong in the yaml syntax checker but still not able to find it. Any clue what I am doing wrong? I tried escaping the forward slash but still no luck :(
I am bad at finding the syntax errors in general. So appreciate any clues you folks can provide in finding the problem.
tasks:
- name: Create a KMS key for using the aws cli
command: 'aws kms create-key --profile="{{ aws_profile }}" --region="{{ aws_region }}"'
register: newkeydetails
- name: Display the values for the variable output
set_fact: newkeydetails="{{ newkeydetails.stdout | from_json }}"
- name: Display the value of keyid
debug:
msg: "{{ newkeydetails.KeyMetadata.KeyId }}"
- name: Create a alias name for KMS key using the aws cli
command: 'aws kms create-alias --alias-name 'alias/anothernewkeydetailskey' --target-key-id '"{{ newkeydetails.KeyMetadata.KeyId }}"' --profile="{{ aws_profile }}" --region="{{ aws_region }}"'
The reason for the issue here was due to the usage of single quotes in the last line, as they were not escaped, resulting in parsing errors.
The working solution involves removing unnecessary quotes, resulting in the following:
'aws kms create-alias --alias-name "alias/anothernewkeydetailskey" --target-key-id "{{ newkeydetails.KeyMetadata.KeyId }}" --profile="{{ aws_profile }}" --region="{{ aws_region }}"