Ansible task variable output not displaying - ansible

Below is my simple playbook
name: "test"
hosts: webservers
tasks:
- name: Echo my_env_var
shell: "echo $MY_ENV_VARIABLE"
environment:
MY_ENV_VARIABLE: whatever_value
- name: Echo my_env_var again
shell: "echo $MY_ENV_VARIABLE"
register: stdd
- debug: msg={{stdd.stdout_lines}}
My output is always msg:"" or msg: []. Why am i not able to see the value of variable

I took your example and changed it from debug msg to debug var. I also simplified it by only running the task once, and found the error in the process. The environment argument is specific to a task. You aren't including it in your second shell task.
Here's the example I used.
echo.yml
- hosts: localhost
tasks:
- name: Echo my_env_var
shell: "echo $MY_ENV_VARIABLE"
environment:
MY_ENV_VARIABLE: whatever_value
register: stdd
- debug: var=stdd
execution
$ ansible-playbook -c local -i "localhost," echo.yml
PLAY [localhost] **************************************************************
GATHERING FACTS ***************************************************************
ok: [localhost]
TASK: [Echo my_env_var] *******************************************************
changed: [localhost]
TASK: [debug var=stdd] ********************************************************
ok: [localhost] => {
"var": {
"stdd": {
"changed": true,
"cmd": "echo $MY_ENV_VARIABLE",
"delta": "0:00:00.005332",
"end": "2016-07-25 19:42:54.320667",
"invocation": {
"module_args": "echo $MY_ENV_VARIABLE",
"module_complex_args": {},
"module_name": "shell"
},
"rc": 0,
"start": "2016-07-25 19:42:54.315335",
"stderr": "",
"stdout": "whatever_value",
"stdout_lines": [
"whatever_value"
],
"warnings": []
}
}
}
PLAY RECAP ********************************************************************
localhost : ok=3 changed=1 unreachable=0 failed=0

Related

Unable to start logstash using /etc/init.d/logstash start

I'm trying to start the logstash service using below playbook. Output says starting but when I checked the status its in stopped state.
---
- hosts: test
gather_facts: False
remote_user: test
become: yes
become_user: root
become_method: sudo
tasks:
- name: starting /etc/init.d/logstash start
shell: /etc/init.d/logstash start
- name: status /etc/init.d/logstash status
shell: /etc/init.d/logstash status
register: logstash_status
- name: output
debug:
msg: "{{logstash_status}}"
Output
PLAY [test] ************************************************************************************************************************************************************
TASK [starting /etc/init.d/logstash start] *****************************************************************************************************************************
changed: [192.168.1.10]
TASK [status /etc/init.d/logstash status] ******************************************************************************************************************************
fatal: [192.168.1.10]: FAILED! => {"changed": true, "cmd": "/etc/init.d/logstash status", "delta": "0:00:00.021383", "end": "2021-06-02 20:31:17.701169", "msg": "non-zero return code", "rc": 1, "start": "2021-06-02 20:31:17.679786", "stderr": "", "stderr_lines": [], "stdout": "Stopped", "stdout_lines": ["Stopped"]}
to retry, use: --limit #/home/test/logstat-config/new.retry
PLAY RECAP *************************************************************************************************************************************************************
192.168.1.10 : ok=1 changed=1 unreachable=0 failed=1
I was able to start the service by running it in background.
---
- hosts: test
gather_facts: False
remote_user: test
become: yes
become_user: root
become_method: sudo
tasks:
- name: starting /etc/init.d/logstash start
shell: nohup /etc/init.d/logstash start &
register: logstash
- debug:
msg: "{{logstash}}"
- name: status /etc/init.d/logstash status
shell: /etc/init.d/logstash status
register: logstash_status
- name: output
debug:
msg: "{{logstash_status}}"
Output:
PLAY [test] ************************************************************************************************************************************************************
TASK [starting /etc/init.d/logstash start] *****************************************************************************************************************************
changed: [192.168.1.10]
TASK [debug] ***********************************************************************************************************************************************************
ok: [192.168.1.10] => {
"msg": {
"changed": true,
"cmd": "nohup /etc/init.d/logstash start &",
"delta": "0:00:00.014488",
"end": "2021-06-03 17:31:02.914306",
"failed": false,
"rc": 0,
"start": "2021-06-03 17:31:02.899818",
"stderr": "",
"stderr_lines": [],
"stdout": "Starting logstash",
"stdout_lines": [
"Starting logstash"
]
}
}
TASK [status /etc/init.d/logstash status] ******************************************************************************************************************************
changed: [192.168.1.10]
TASK [output] **********************************************************************************************************************************************************
ok: [192.168.1.10] => {
"msg": {
"changed": true,
"cmd": "/etc/init.d/logstash status",
"delta": "0:00:00.011286",
"end": "2021-06-03 17:31:03.272873",
"failed": false,
"rc": 0,
"start": "2021-06-03 17:31:03.261587",
"stderr": "",
"stderr_lines": [],
"stdout": "Running",
"stdout_lines": [
"Running"
]
}
}
PLAY RECAP *************************************************************************************************************************************************************
192.168.1.10 : ok=4 changed=2 unreachable=0 failed=0

ansible variable reference : unable to read stdout_lines from register variable after ssh and command

Team,
My task is running she’ll commands on hosts that are pulled from registered variable. Currently two hosts but there will be 100 in production.
am unable to read out stdout or stdout_lines. my task and output is below. Its sshing to the remove server and then runnning df -h command and storing output.
actual output ( with some texts stripped but not any brackets)
ok: [localhost] => {
"raid_info.results": [
{
"ansible_loop_var": "item",
"changed": true,
"cmd": "ssh -F /home/svcngcctal.net \"df -kh /raid/\"",
"delta": "0:00:02.095839",
"end": "2019-10-24 22:55:38.323679",
"failed": false,
"failed_when_result": false,
"invocation": {
"module_args": {
"_raw_params": "ssh -F /home/metal.net \"df -kh /raid/\"",
"_uses_shell": true,
"warn": true
}
},
"item": {
"nodeType": "4.15.0-45-generic",
"node_name": "hostB"
},
"rc": 0,
"start": "2019-10-24 22:55:36.227840",
"stderr": "Warning: Permanently***",
"stderr_lines": [
"Warning:asd"
],
"stdout": "Filesystem Size Used Avail Use% Mounted on\n/dev/sdb1 7.0T 175G 6.5T 3% /raid",
"stdout_lines": [
"Filesystem Size Used Avail Use% Mounted on",
"/dev/sdb1 7.0T 175G 6.5T 3% /raid"
]
},
{
"ansible_loop_var": "item",
"changed": true,
"cmd": "ssh -F /home/svcngcc/jenkinstal.net \"df -kh /raid/\"",
"delta": "0:00:02.115591",
"invocation": {
"module_args": {
"_raw_params": "ssh -F /home/sal.net \"df -kh /raid/\"",
"warn": true
}
},
"item": {
"nodeType": "4.15.0-45-generic",
"node_name": "hostA"
},
"rc": 0,
"start": "2019-10-24 22:55:38.467007", ",
"stderr_lines": [
"Warning: Permanently "
],
"stdout": "Filesystem Size Used Avail Use% Mounted on\n/dev/sdb1 7.0T 176G 6.5T 3% /raid",
"stdout_lines": [
"Filesystem Size Used Avail Use% Mounted on",
"/dev/sdb1 7.0T 176G 6.5T 3% /raid"
]
}
]
}
from above output am not able to read stdout lines to verify the mount point..
task:
- name: "RAID mount check for fscache on GPU Nodes"
shell: ssh -F {{ ssh_cfg_path.stdout }} {{ item.node_name }}.{{ ssh_host }} "df -kh /raid/"
ignore_errors: no
register: raid_info
failed_when: raid_info.rc != 0
with_items: "{{ gpu_nodes }}"
- name: raid_info results1_stdout_lines
debug:
var: raid_info.results[1].stdout_lines
does not output anything..
I cannot reproduce your results - I believe that you left out some information when posting.
For example, do your tasks by any chance run on different hosts? The "raid_info" variable is registered per host, so if your tasks run on different hosts this might cause your issue.
My test, with a similar loop:
- playbook.yml
---
- hosts: localhost
gather_facts: false
vars_files:
- packages.yml
tasks:
- name: Run a command
command: "echo {{item}}"
register: my_output
loop:
- 10.10.80.193
- 10.10.80.194
- name: Print the results
debug:
var: my_output
- name: Print only the second item in the list
debug:
var: my_output.results[1].stdout_lines
result:
# ansible-playbook -i inventory.yml playbook.yml
PLAY [localhost] *************************************************************************************************************************
TASK [Run a command] *********************************************************************************************************************
changed: [localhost] => (item=10.10.80.193)
changed: [localhost] => (item=10.10.80.194)
TASK [Print the results] *****************************************************************************************************************
ok: [localhost] => {
"my_output": {
"changed": true,
"msg": "All items completed",
"results": [
{ <snip>
},
{ <snip>
}
]
}
}
TASK [Print only the second item in the list] ********************************************************************************************
ok: [localhost] => {
"my_output.results[1].stdout_lines": [
"10.10.80.194"
]
}
PLAY RECAP *******************************************************************************************************************************
localhost : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

How to escape double-quotes in ansible variable

I have an ansible playbook which accepts a variable, passing a variable with quotes in it(its needed), that variable will used to query against DB
Playbook
- name: Execute clear script
script: scripts/clear-documents.sh {{ids}}
Command
ansible-playbook playbooks/maintenance.yml -i hosts -t clear -e ids=["foo", "bar"]
in this process script receives the input as [foo, bar] instead of ["foo", "bar"]
I tried escaping using backslash but that did not help
ansible-playbook playbooks/maintenance.yml -i hosts -t clear -e ids=[\"foo\", \"bar\"]
Adding double quotes in playbook, makes the input like "[foo,bar]" and not ["foo", "bar"]
script: scripts/clear-documents.sh "{{ids}}"
I searched a lot but did not get any proper solution, is there a way to handler this
Note:
ansible version - 2.2.3.0
The thing you are looking for is quote, in combination with #JGK's correct usage of -e ids='["foo", "bar"]' because you were not quoting them on the way into ansible, and then you were not quoting them on the way out of ansible in that shell: task
- shell: scripts/clear-documents.sh {{ ids | quote }}
As is mentioned in How to escape backslash and double quote in Ansible (script module) you need to surround the {{ ids }} with ' ' in your shell command.
#!/usr/bin/env ansible-playbook
- hosts: localhost
gather_facts: false
become: false
tasks:
- name: Escape characters for fun and profit
vars:
string_list: '["one", "two"]'
shell: "echo '{{ string_list }}'"
register: output1
- name: Print it out
debug:
msg: "{{ output1 }}"
- name: Don't escape characters
vars:
string_list: '["one", "two"]'
shell: "echo {{ string_list }}"
register: output2
- name: Print it out
debug:
msg: "{{ output2 }}"
PLAY [localhost] ************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
TASK [Escape characters for fun and profit] *********************************************************************************************************************************************************************************************************************************************************************************************************************************
changed: [localhost]
TASK [Print it out] *********************************************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": {
"changed": true,
"cmd": "echo '[\"one\", \"two\"]'",
"delta": "0:00:00.003400",
"end": "2019-05-07 12:02:32.897856",
"failed": false,
"rc": 0,
"start": "2019-05-07 12:02:32.894456",
"stderr": "",
"stderr_lines": [],
"stdout": "[\"one\", \"two\"]",
"stdout_lines": [
"[\"one\", \"two\"]"
]
}
}
TASK [Don't escape characters] **********************************************************************************************************************************************************************************************************************************************************************************************************************************************
changed: [localhost]
TASK [Print it out] *********************************************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": {
"changed": true,
"cmd": "echo [\"one\", \"two\"]",
"delta": "0:00:00.002990",
"end": "2019-05-07 12:02:33.192049",
"failed": false,
"rc": 0,
"start": "2019-05-07 12:02:33.189059",
"stderr": "",
"stderr_lines": [],
"stdout": "[one, two]",
"stdout_lines": [
"[one, two]"
]
}
}
PLAY RECAP ******************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
localhost : ok=4 changed=2 unreachable=0 failed=0

Register Ansible variable property

Using Ansible I'm having a problem registering a variable the way I want. Using the implementation below I will always have to call .stdout on the variable - is there a way I can do better?
My playbook:
Note the unwanted use of .stdout - I just want to be able to use the variable directly without calling a propery...?
---
- name: prepare for new deployment
hosts: all
user: ser85
tasks:
- name: init deploy dir
shell: echo ansible-deploy-$(date +%Y%m%d-%H%M%S-%N)
# http://docs.ansible.com/ansible/playbooks_variables.html
register: deploy_dir
- debug: var=deploy_dir
- debug: var=deploy_dir.stdout
- name: init scripts dir
shell: echo {{ deploy_dir.stdout }}/scripts
register: scripts_dir
- debug: var=scripts_dir.stdout
The output when I execute the playbook:
TASK [init deploy dir] *********************************************************
changed: [123.123.123.123]
TASK [debug] *******************************************************************
ok: [123.123.123.123] => {
"deploy_dir": {
"changed": true,
"cmd": "echo ansible-deploy-$(date +%Y%m%d-%H%M%S-%N)",
"delta": "0:00:00.002898",
"end": "2016-05-27 10:53:38.122217",
"rc": 0,
"start": "2016-05-27 10:53:38.119319",
"stderr": "",
"stdout": "ansible-deploy-20160527-105338-121888719",
"stdout_lines": [
"ansible-deploy-20160527-105338-121888719"
],
"warnings": []
}
}
TASK [debug] *******************************************************************
ok: [123.123.123.123] => {
"deploy_dir.stdout": "ansible-deploy-20160527-105338-121888719"
}
TASK [init scripts dir] ********************************************************
changed: [123.123.123.123]
TASK [debug] *******************************************************************
ok: [123.123.123.123] => {
"scripts_dir.stdout": "ansible-deploy-20160527-105338-121888719/scripts"
}
Any help or insights appreciated - thank you :)
If I understood it right you want to assign deploy_dir.stdout to a variable that you can use without stdout key. It can be done with set_fact module:
tasks:
- name: init deploy dir
shell: echo ansible-deploy-$(date +%Y%m%d-%H%M%S-%N)
# http://docs.ansible.com/ansible/playbooks_variables.html
register: deploy_dir
- set_fact: my_deploy_dir="{{ deploy_dir.stdout }}"
- debug: var=my_deploy_dir

Ansible playbook - environment variables

I am trying (newbie) to setup a playbook, which will use lookup plugin to fetch secrets from vault (https://github.com/jhaals/ansible-vault), but it will fail on missing environment variables every time. Can anyone help? Thanks for the help.
PS: token is for a test purposes
There is condition in lookup module :
url = os.getenv('VAULT_ADDR')
if not url:
raise AnsibleError('VAULT_ADDR environment variable is missing')
Playbook:
---
- hosts: localhost
vars:
vault1_env:
VAULT_ADDR: https://localhost:8200/
VAULT_TOKEN: my-token-id
VAULT_SKIP_VERIFY: True
tasks:
- shell: echo VAULT_ADDR is $VAULT_ADDR, VAULT_TOKEN is $VAULT_TOKEN, VAULT_SKIP_VERIFY is $VAULT_SKIP_VERIFY
environment: "{{ vault1_env }}"
register: shellout
- debug: var=shellout
- debug: msg="{{ lookup('vault', 'secret/hello', 'value') }}"
output:
PLAY ***************************************************************************
TASK [setup] *******************************************************************
ok: [localhost]
TASK [command] *****************************************************************
changed: [localhost]
TASK [debug] *******************************************************************
ok: [localhost] => {
"shellout": {
"changed": true,
"cmd": "echo VAULT_ADDR is $VAULT_ADDR, VAULT_TOKEN is $VAULT_TOKEN, VAULT_SKIP_VERIFY is $VAULT_SKIP_VERIFY",
"delta": "0:00:00.001268",
"end": "2016-05-17 15:46:34.144735",
"rc": 0,
"start": "2016-05-17 15:46:34.143467",
"stderr": "",
"stdout": "VAULT_ADDR is https://localhost:8200/, VAULT_TOKEN is ab9b16c6-52d9-2051-0802-6f047d929b63, VAULT_SKIP_VERIFY is True",
"stdout_lines": [
"VAULT_ADDR is https://localhost:8200/, VAULT_TOKEN is ab9b16c6-52d9-2051-0802-6f047d929b63, VAULT_SKIP_VERIFY is True"
],
"warnings": []
}
}
TASK [debug] *******************************************************************
fatal: [localhost]: FAILED! => {"failed": true, "msg": "ERROR! VAULT_ADDR environment variable is missing"}
PLAY RECAP *********************************************************************
localhost : ok=3 changed=1 unreachable=0 failed=1
Here you are only setting environmental variables for the shell module, and not for the others. If you want to use variables across multiple modules, or for an entire a host, you should use the environment attribute on all of the modules, or on the host itself, something like this:
---
- hosts: localhost
environment:
VAULT_ADDR: https://localhost:8200/
VAULT_TOKEN: my-token-id
VAULT_SKIP_VERIFY: True
Why don't you make use of the vault feature to encrypt a variable file and then include this file in your playbook.
http://docs.ansible.com/ansible/playbooks_vault.html#running-a-playbook-with-vault

Resources