Why isn't the debug module printing the expected output? [closed] - ansible

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

Related

How to login to server host without specifying it in limit from the tower provide only the client host [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 days ago.
Improve this question
I have scenario where i need to run some configuration on hostA which will be the limit and then login to ServerA and do ssh or ping test on hostA
Currently I have
1.main_playbook.yml
---
- import playbook_config.yml
- import playbook_ping.yml
2.Playbook_config.yml
---
- name: configure endpoint_host
hosts: hosts
roles:
- configure
3.playbook_ping.yml
- hosts: "{{ ServerA }}"
gather_facts: true
become: yes
roles:
- ping_check
To work this out i had to provide the serverA in the limit if i dont give the serverA the playbook execution skips the playbook_ping
Inventory:
ServerA: 10.10.10.10
ServerB: 10.10.10.11
Example 1:
Execution: ansible-playbook main_playbook.yml --limit=HostA, ServerA
Result: this execution proceeds to the ping check
Example 2:
Execution: ansible-playbook main_playbook.yml --limit=HostA
Result: it doesn't proceed to the playbook_ping.yml
here I want the
ServerA
variable is been set in config.yml and it carries the ServerA string
to the ping playbook

Ansible logical AND with conditions not working [closed]

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 months ago.
Improve this question
I try to run task depending on two conditions using logical "and". For the first condition I created the following task:
- name: Check if configuration already exists, then skip next task
stat:
path: "{{openldap_config}}/cn=config"
register: is_configured
Then I crated the following task, which is working fine:
- name: add new basic-configuration
shell: /opt/symas/sbin/slapadd -n 0 -F {{openldap_config}} -l /home/{{ansible_user}}/config.ldif
args:
executable: /bin/bash
when: not is_configured.stat.exists
If a configuration file exists the task will be skipped. Then I have a few task where I use "group_names" like this on:
- name: generating deltasyncrepl LDIF for main DB
template:
src: main_db_repl.j2
dest: /home/{{ansible_user}}/main-db-repl.ldif
owner: "{{ansible_user}}"
group: "{{ansible_group}}"
mode: '660'
when: "'ldap_provider' in group_names"
That's also working. BUT now I would like to have a logical "and" for both conditions:
- name: add replication of cn=config to all provider
shell: /opt/symas/bin/ldapmodify -Y EXTERNAL -H ldapi:/// -f /home/{{ansible_user}}/repl_config.ldif
args:
executable: /bin/bash
when: not is_configured.stat.exists
and "'ldap_provider' in group_names"
This is not working. I tried different quoting but I could not find a a working solution. I try it with brackets too, also not working. Some how it must be possible to have a logical AND with a int- and a string- variable.
Thanks for any help
While you can manually stat the file and that works just fine, this is actually exactly the case that the creates parameter to the shell and command modules is designed to handle. You should also migrate to the cmd parameter (supported since Ansible 2.0) instead of using args, so that people reading your code don't have to understand that rarely-used syntax:
- name: Basic configuration for OpenLDAP
shell:
cmd: /opt/symas/sbin/slapadd -n 0 -F {{ openldap_config }} -l /home/{{ ansible_user }}/config.ldif
executable: /bin/bash
creates: "{{ openldap_config }}/cn=config"
register: openldap_config_result
While you didn't include your other attempts at solving this problem, generally the best way to apply multiple conditions to a task is to use a list, which will implicitly apply and between each condition in the list:
- name: Add replication of cn=config to all providers
shell:
cmd: /opt/symas/bin/ldapmodify -Y EXTERNAL -H ldapi:/// -f /home/{{ ansible_user }}/repl_config.ldif
executable: /bin/bash
when:
- openldap_config_result is changed
- "'ldap_provider' in group_names"
You can also do it as a single string, though you have to make sure your quoting is correct (which it isn't in your question):
when: openldap_config_result is changed
and 'ldap_provider' in group_names

ERROR! A malformed role declaration was encountered [closed]

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

Substraction within an Ansible space calculation [closed]

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.

Ansible - expand a variable inside default [closed]

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.

Resources