How can I check service status using Ansible and compare that value with the expected value provided by client? - linux

I have a code to check service status however it is not working as expected. Any advice on it would be of great help, also I need to know how can various service statuses be verified and compared using this code.
Tasks file for ypbind_tab3
- shell: "systemctl status ypbind.service "
register: _cat
- assert:
that:
- lookup('file', 'customer-expects.txt') == _cat.stdout
fail_msg: missed
success_msg: matched
The output is
[root#ansible-master /]# ansible-playbook tab3.role.yml -v
Using /etc/ansible/ansible.cfg as config file
PLAY [This output is for Tab-3 of Function Design document] *****************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
ok: [ansible-client]
TASK [ypbind_tab3 : shell] **************************************************************************************************************
fatal: [ansible-client]: FAILED! => {"changed": true, "cmd": "systemctl status ypbind.service ", "delta": "0:00:00.013814", "end": "2022-01-08 15:30:27.082785", "msg": "non-zero return code", "rc": 4, "start": "2022-01-08 15:30:27.068971", "stderr": "Unit ypbind.service could not be found.", "stderr_lines": ["Unit ypbind.service could not be found."], "stdout": "", "stdout_lines": []}
PLAY RECAP ******************************************************************************************************************************
ansible-client : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0

Instead of shell use the module service_facts. For example
- ansible.builtin.service_facts:
- ansible.builtin.debug:
var: ansible_facts.services['sshd.service']
gives
TASK [ansible.builtin.debug] **************************************
ok: [localhost] =>
ansible_facts.services['sshd.service']:
name: sshd.service
source: systemd
state: active
status: enabled
Then you can test the state of the service. For example, given the file
shell> cat customer-expects-sshd-state.txt
active
the task below
- ansible.builtin.assert:
that:
- lookup('file', 'customer-expects-sshd-state.txt') ==
ansible_facts.services['sshd.service']['state']
fail_msg: missed
success_msg: matched
gives
TASK [ansible.builtin.assert] ************************************
ok: [localhost] => changed=false
msg: matched

Related

Ansible- How to show authentication token after adding user to using post?

I am trying to add user to db the response shows authentication token, but i see some errors that dont print anyting from debug. Any Idea why? any help is Welcome.
Heres the playbook-
- name: uri module demo
hosts: localhost
become: false
vars:
server: "http://localhost:8080"
endpoint: "/api/v1/users"
tasks:
- name: list applicant by id
ansible.builtin.uri:
url: "{{ server }}{{ endpoint }}"
method: POST
body_format: json
body: '{
"firstName":"user1",
"lastName":"localuser",
"mobile":"9090909090",
"password":"12121212",
"roleId":"1",
"roleCode":"helpdesk_user"}'
status_code: 200
timeout: 30
register: result
- debug:
msg: "{{ result.json.token }}"
Output after running playbook
linux:~$ ansible-playbook ansible_api_test.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match
'all'
PLAY [uri module demo] ***********************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************
ok: [localhost]
TASK [list applicant by id] ******************************************************************************************
ok: [localhost]
TASK [debug] *********************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'token'\n\nThe error appears to be in '/home/ansible_api_test.yml': line 27, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - debug:\n ^ here\n"}
PLAY RECAP ***********************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0

How can I compare a remote file with a reference on controller without failing the playbook when different?

I have a very peculiar problem, in my code I have to compare ansible value with customer expected value and customer expected un_matched value, however the final playbook output should be failed =0 instead of failed =1.
The code is:
- name: show file contents of customer-expects.txt
debug:
msg: "{{ lookup('file', '/customer-expects.txt') }}"
- shell: cat /etc/issue
register: issue
changed_when: false
ignore_errors: yes
- assert:
that:
- lookup('file', '/customer-expects.txt') == issue.stdout
success_msg: "matched! {{ lookup('file', '/customer-expects.txt') }} = {{ issue.stdout }}"
- name: show file contents customer-expects_unmatched.txt
debug:
msg: "{{ lookup('file', '/customer-expects_unmatched.txt') }}"
- shell: cat /etc/issue
register: issue
changed_when: false
ignore_errors: yes
- assert:
that:
- lookup('file', '/customer-expects_unmatched.txt') == issue.stdout
fail_msg: "unmatched! {{ lookup('file', '/customer-expects_unmatched.txt') }} = {{ issue.stdout }}"
success_msg: "matched! {{ lookup('file', '/customer-expects.txt') }} = {{ issue.stdout }}"
Output is:
[root#ansible-master /]# ansible-playbook tab8.role.yml -v
Using /etc/ansible/ansible.cfg as config file
PLAY [This output is for Tab-8 of Function Design document] *********
TASK [Gathering Facts] **********************************************
ok: [ansible-client1]
TASK [issue_tab8 : show file contents of customer-expects.txt] ******
ok: [ansible-client1] => {
"msg": ""
}
TASK [issue_tab8 : shell] *******************************************
ok: [ansible-client1] => {"changed": false, "cmd": "cat /etc/issue", "delta": "0:00:00.005200", "end": "2022-01-25 14:17:57.070688", "rc": 0, "start": "2022-01-25 14:17:57.065488", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
TASK [issue_tab8 : assert] ******************************************
ok: [ansible-client1] => {
"changed": false,
"msg": "matched! = "
}
TASK [issue_tab8 : show file contents customer-expects_unmatched.txt] *
ok: [ansible-client1] => {
"msg": "abc"
}
TASK [issue_tab8 : shell] *******************************************
ok: [ansible-client1] => {"changed": false, "cmd": "cat /etc/issue", "delta": "0:00:00.004603", "end": "2022-01-25 14:17:57.674059", "rc": 0, "start": "2022-01-25 14:17:57.669456", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
TASK [issue_tab8 : assert] ******************************************
fatal: [ansible-client1]: FAILED! => {
"assertion": "lookup('file', '/customer-expects_unmatched.txt') == issue.stdout",
"changed": false,
"evaluated_to": false,
"msg": "unmatched! abc = "
}
PLAY RECAP **********************************************************
ansible-client1 : ok=6 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
You are taking this the wrong way and make it unnecessarily complicated IMO.
You have to understand that laying out a playbook in ansible consists of describing the state in which you want to find the target machine. In this particular situation, you want to describe the state in which you want to find a particular file on the target. This is basically done with a task using the copy module. Once you have done that, knowing if the file is similar or different is just a matter of command line switches or task options.
For more on the below explanation see Validating tasks
Ansible has a check_mode which will let you run an entire playbook just to verify what it would do. You can also unconditionally apply that mode to a single task so that it always runs as a check (check_mode: true), or always makes changes to the remote target however the playbook was called (check_mode: false). We are interested here in the first form.
Similarly, ansible has a diff possibility that will let you see what are the differences between the state you described and the modification it (should have to) apply on the target to reach that state.
An example whith your above scenario. I made the test targeting my local machine only but you can get the exact same result on any remote target.
First let's create the reference file we want to check against in our ansible directory.
mkdir -p files
echo "I'm the reference file" > files/ansible_reference.txt
For the example, I'll now create one identical and one different file on our target so that we can compare both cases:
echo "I'm the reference file" > /tmp/ansible_similar.txt
echo "I'm a different file" > /tmp/ansible_different.txt
This is the playbook compare.yml
---
- name: compare remote file to a local reference
hosts: localhost
gather_facts: false
vars:
local_reference: ansible_reference.txt
remote_files_2_check:
- /tmp/ansible_similar.txt
- /tmp/ansible_different.txt
tasks:
- name: Dry run a copy with diff to check if remote file is aligned
copy:
src: "{{ local_reference }}"
dest: "{{ item }}"
check_mode: true
diff: true
loop: "{{ remote_files_2_check }}"
Which gives:
$ ansible-playbook compare.yml
PLAY [compare remote file to a local reference] *****************************
TASK [Dry run a copy with diff to check if remote file is aligned] **********
ok: [localhost] => (item=/tmp/ansible_similar.txt)
--- before: /tmp/ansible_different.txt
+++ after: /home/user/ansible_project/files/ansible_reference.txt
## -1 +1 ##
-I'm a different file
+I'm the reference file
changed: [localhost] => (item=/tmp/ansible_different.txt)
PLAY RECAP ******************************************************************
localhost : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Notes:
Although Ansible reports changed for the different file this is only what would happen. Since we are in dry-run for the given task, the above will not change your target. You can run it any number of times, it will report the same result as long as you don't change the reference or the target state.
I you are not happy with the default reporting of the task, you can register the result, explore its content and use it in subsequent tasks to meet your exact requirement.
Note that the above may report a difference on the permission of the target file. You may have to tune this in the copy module to avoid false positive.

filtering the ansible playbook output [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have output of an ansible playbook for which i need to apply fileters in such a way to proceed with next tasks. Please find the below output.
ok: [localhost] => {
"results": [
{
"actions": {
"namespaces": {},
"state": "present",
"xpath": "/Storage/SSL/KeyStorePath"
},
"ansible_loop_var": "item",
"changed": false,
"pod": 1,
"failed": false,
"invocation": {
"module_args": {
"mount": true,
"input_type": "yaml",
}
},
"item": "100.108.22.102",
"msg": "found 1 nodes"
},
{
"actions": {
"namespaces": {},
"state": "present",
"xpath": "/Storage/SSL/KeyStorePath"
},
"ansible_loop_var": "item",
"changed": false,
"pod": 0,
"failed": false,
"invocation": {
"module_args": {
"mount": true,
"input_type": "yaml",
}
},
"item": "100.108.22.103",
"msg": "found 0 nodes"
}
]
}
Here, i want the next task to be executed when the node respective pod value is 1 if the pod value of the node is 0 then the next task should not run on the respective node ip.
Please assist ..
Q: "Execute next task when the node's respective pod value is 1"
A: There are many ways how to proceed with the next task. For example, given the variable results
Use delegate_to and loop selected items
- hosts: localhost
tasks:
- debug:
msg: 'Task is running on {{ item.item }}'
loop: "{{ results|selectattr('pod', 'eq', 1)|list }}"
loop_control:
label: "{{ item.item }}"
delegate_to: "{{ item.item }}"
gives (abridged)
TASK [debug] ****
ok: [localhost -> 100.108.22.102] => (item=100.108.22.102) =>
msg: Task is running on 100.108.22.102
Create a list of selected hosts and test the host is in the list
- hosts: 100.108.22.102,100.108.22.103
tasks:
- debug:
msg: "Task is running on {{ inventory_hostname }}"
when: inventory_hostname in pods
vars:
pods: "{{ results|
selectattr('pod', 'eq', 1)|
map(attribute='item')|
list }}"
gives (abridged)
TASK [debug] ****
ok: [100.108.22.102] =>
msg: Task is running on 100.108.22.102
skipping: [100.108.22.103]
Use add_host to create an inventory group in the first play and use it in the second one
- hosts: localhost
tasks:
- add_host:
name: '{{ item }}'
groups: pods_enabled
loop: "{{ results|
selectattr('pod', 'eq', 1)|
map(attribute='item')|
list }}"
- hosts: pods_enabled
tasks:
- debug:
msg: "Task is running on {{ inventory_hostname }}"
gives (abridged)
PLAY [localhost] ****
TASK [add_host] ****
changed: [localhost] => (item=100.108.22.102)
PLAY [pods_enabled] ****
TASK [debug] ****
ok: [100.108.22.102] =>
msg: Task is running on 100.108.22.102
PLAY RECAP ****
100.108.22.102: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
localhost: ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
You could use with_items and when in your next task as below
- name: validate pod value
debug:
msg: "The pod value is 1"
with_items: "{{ your_var.results }}"
when: item.pod == "1"

Why can't I capture an environment variable with Ansible?

I am trying to get and print the value of a given environment variable (ENV_VAR):
$ cat ~/.bash_profile
ENV_VAR=Updated
ENV_VAR2=Updated
$ source ~/.bash_profile && echo $ENV_VAR
Updated
I can successfully retrieve it via terminal, however by using the Ansible playbook below I get an error:
# YAML
---
- hosts: all
vars:
env_var: "{{ lookup('env','ENV_VAR') }}"
tasks:
- name: Add/Update an environment variable to the remote user's shell
lineinfile:
dest: ~/.bash_profile
regexp: '^ENV_VAR='
line: "ENV_VAR=Updated2"
- name: Get the value of the environment variable we just added
shell: source ~/.bash_profile && echo $ENV_VAR
register: env_var_updated
- name: Print the value of the environment variable
debug:
msg: "var1: {{ env_var }} - var2 {{ env_var_updated.stdout }}"
Executing:
$ ansible-playbook playbook.yml
PLAY [all] *********************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************
ok: [192.168.0.222]
TASK [Add/Update an environment variable to the remote user's shell] ***********************************************
ok: [192.168.0.222]
TASK [Get the value of the environment variable we just added] *****************************************************
fatal: [192.168.0.222]: FAILED! => {"changed": true, "cmd": "source ~/.bash_profile && echo $ENV_VAR", "delta": "0:00:00.002337", "end": "2020-12-02 10:20:21.963968", "msg": "non-zero return code", "rc": 127, "start": "2020-12-02 10:20:21.961631", "stderr": "/bin/sh: 1: source: not found", "stderr_lines": ["/bin/sh: 1: source: not found"], "stdout": "", "stdout_lines": []}
PLAY RECAP *********************************************************************************************************
192.168.0.222 : ok=2 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
Results after execution:
$ cat ~/.bash_profile
ENV_VAR=Updated2
ENV_VAR2=Updated
$ source ~/.bash_profile && echo $ENV_VAR
Updated2
I am logged as the same user (In the terminal window and SSH for Ansible)
As shown by the following guide:
https://docs.ansible.com/ansible/2.5/modules/shell_module.html
Ansible shell actually runs in /bin/sh and not /bin/bash. You can specify the shell as /bin/bash through:
- name: Get the value of the environment variable we just added
shell: source ~/.bash_profile && echo $ENV_VAR
register: env_var_updated
args:
executable: /bin/bash

ansible-playbook extra vars passing from command line

My playbook looks like:
---
- name: Install and configure AD authentication
hosts: test
become: yes
become_user: root
vars:
hostname: "{{ host_name }}"
vars_prompt:
- name: "bind_password"
prompt: "Password for xxx.local\\user"
private: yes
tasks:
- name: Ansible prompt example.
debug:
msg: "{{ bind_password }}"
- name: Ansible prompt example.
debug:
msg: "{{ host_name }}"
and i am using below command to pass the variable
ansible-playbook hostname_set.yml --extra-vars "host_name= 'xxx.xxx.local'"
but i am not getting exact variable value what i am using for setting up hostname.
Password for xxx.xxx\user:
PLAY [Install and configure AD authentication]
TASK [Gathering Facts]
ok: [x.x.x.x]
TASK [Ansible prompt example.]
ok: [x.x.x.x] => {
"msg": "wel"
}
TASK [Ansible prompt example.]
ok: [x.x.x.x] => {
"msg": ""
}
TASK [Setup the hostname]
changed: [x.x.x.x]
PLAY RECAP
x.x.x.x : ok=4 changed=1 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0
You have an extra space in your command line that ruins how ansible is interpreting the extra vars. Just remove it:
--extra-vars "host_name='xxx.xxx.local'"
Note that you don't even need all those quotes. The following should also work as expected:
--extra-vars host_name=xxx.xxx.local

Resources