Ansible error: The task includes an option with an undefined variable - ansible

Below is how my complete helpA.yml playbook looks like:
- hosts: localhost
tasks:
- name: "Construct File Path {{ inventory_hostname }} before Deployment."
tags: validate
include_vars:
file: "{{ item }}"
with_fileglob:
- "vars/{{ Layer }}_*.yaml"
- name: "set_fact"
tags: validate
set_fact:
fpath_APP: "{{ fpath_APP + [ BASEPATH ~ '/' ~ vars[item.split('.')[1]] ~ '/' ~ item | basename ] }}"
when: Layer == 'APP'
with_items:
- "{{ Source_Filenames.split(',') }}"
vars:
fpath_APP: []
- debug: var=fpath_{{ Layer }}
I'm getting the below syntax error running the yml.
$ ansible-playbook /app/helpA.yml --tags validate -e
"Source_Filenames=/tmp/logs/filename1.src,/tmp/logs/33211.sql,/app/Jenkins/file1.mrt
Layer=APP" [WARNING]: provided hosts list is empty, only localhost is
available. Note that the implicit localhost does not match 'all'
PLAY [localhost]
TASK [Construct File Path localhost before Deployment.]
************************************************************************************************* ok: [localhost] => (item=/app/vars/APP_BASE_vars.yaml)
TASK [set_fact]
*************************************************************************************************************************************************************** fatal: [localhost]: FAILED! => {"msg": "The task includes an option
with an undefined variable. The error was: 'dict object' has no
attribute u'src'\n\nThe error appears to be in '/app/helpA.yml': line
12, column 6, but may\nbe elsewhere in the file depending on the exact
syntax problem.\n\nThe offending line appears to be:\n\n\n - name:
\"set_fact\"\n ^ here\n"}
PLAY RECAP
******************************************************************************************************************************************************************** localhost : ok=1 changed=0 unreachable=0
failed=1 skipped=0 rescued=0 ignored=0
I'm on the latest version on of ansible and python 2.7.5

Related

how can I complete correctly ansible playbook to get info what i want

I'm very new with ansible so I need help to complete my first ansible playbook
I need to get the results from file created below logs directory.
I also need to input container name which is migrated to find results file named with container and starts with 1 and 3 (2 results of files need)
can't find which part got wrong.
Please help me out
Thank you in advance.
---
- name : find the results for migration ended
hosts: newmigservers
tasks:
- pause:
prompt: "what is the container name?"
echo: yes
register: result
- set_fact:
container: "{{ result.user_input }}"
- debug:
var: container
- name: find where the container is
shell: "grep -lr '{{ container }}'"
args:
chdir: "logs/"
register: grep_output
- name: cat 1,3 file for the results
command: cat {{ grep_output }}
register: results
PLAY [find the results for migration ended] *************************************************************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************************************************************************************
ok: [1.1.1.1]
ok: [2.2.2.2]
ok: [3.3.3.3]
TASK [pause] ********************************************************************************************************************************************************************************************************************************
[pause]
what is the container name?:
ok: [1.1.1.1]
TASK [set_fact] *****************************************************************************************************************************************************************************************************************************
ok: [1.1.1.1]
fatal: [2.2.2.2]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'result' is undefined\n\nThe error appears to have been in '/home/amber/results.yml': line 9, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n register: result\n - set_fact:\n ^ here\n"}
fatal: [3.3.3.3]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'result' is undefined\n\nThe error appears to have been in '/home/amber/results.yml': line 9, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n register: result\n - set_fact:\n ^ here\n"}
TASK [debug] ********************************************************************************************************************************************************************************************************************************
ok: [1.1.1.1] => {
"container": "amber_test_file_size.txt"
}
TASK [find where the container is] **********************************************************************************************************************************************************************************************************
skipping: [2.2.2.2]
TASK [cat 1,3 file for the results] *********************************************************************************************************************************************************************************************************
skipping: [3.3.3.3.]
to retry, use: --limit #/home/amber/results.retry
PLAY RECAP **********************************************************************************************************************************************************************************************************************************
1.1.1.1 : ok=4 changed=0 unreachable=0 failed=0
2.2.2.2 : ok=1 changed=0 unreachable=0 failed=1
3.3.3.3 : ok=1 changed=0 unreachable=0 failed=1
Pause runs only once for the play batch, causing only the first server in the batch (1.1.1.1 in my case) to pick the variable.
You can workaround this by adding serial to your playbook as below:
---
- name : find the results for migration ended
hosts: newmigservers
serial: 1
tasks:
- pause:
prompt: "what is the container name?"
echo: yes
register: result
- set_fact:
container: "{{ result.user_input }}"
- debug:
var: container

Ansible playbook is failing for 'become:yes'

The following ansible-playbook works fine for non-sudo access.
But fails when I un-comment become:yes
---
- hosts: all
become: yes
tasks:
- name: Register the policy file in a variable
read_csv:
path: policy.csv
delegate_to: localhost
register: csv_file
- name: Check rules pre-remediation
command: "{{ item.Compliance_check }}"
register: output
with_items:
"{{ csv_file.list }}"
- name: Perform remediation
command: "{{ item.item.Remediation }}"
when: item.item.Expected_result != item.stdout
with_items:
"{{ output.results }}"
The inventory file looks like:
10.136.59.110 ansible_ssh_user=username ansible_ssh_pass=password ansible_sudo_pass=password
Error I'm facing is:
user#hostname:~/git-repo/MCI$ ansible-playbook playbook.yaml -i inventory
PLAY [all] *******************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************
ok: [10.136.59.109]
TASK [Register the policy file in a variable] ********************************************************************************************
Sorry, try again.
fatal: [10.136.59.109 -> localhost]: FAILED! => {"changed": false, "module_stderr": "[sudo via ansible, key=mjcqjbcyeemygkxwycgeftiikivnylsj] password:\nsudo: no password was provided\nsudo: 1 incorrect password attempt\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}
PLAY RECAP *******************************************************************************************************************************
10.136.59.109 : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
Can't understand why this error is showing. I have tried providing correct sudo passwords in both inventory as well as at run-time using --ask-become-pass
Please help find out the cause of the error.

Ansible playbook to check OS and given mount points

Below is my playbook to check to verify OS and Mount points. Getting below error and couldn't debug it. The issue seems to be with the later part "Verify Mount points"
---
- hosts: all
gather_facts: true
vars:
M1: ['RedHat','7']
M2: ['/u01','/u02']
tasks:
- name: "Verify OS"
when: >
not ((ansible_os_family == M1[0] and ansible_distribution_major_version == M1[1]))
fail:
msg: "OS not supported"
- name: "Verify Mount Points"
with_items: ansible_mounts
when: >
not ((item.mount == M2[0] and item.mount == M2[1]))
fail:
msg: "Mount Point doesn't exist"
Output:
ansible-playbook -i dbservers test.yml
PLAY [all] **********************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************
ok: [192.168.0.107]
ok: [192.168.0.109]
TASK [Verify OS] ****************************************************************************************************************************************
skipping: [192.168.0.109]
skipping: [192.168.0.107]
TASK [Verify Mount Points] ******************************************************************************************************************************
fatal: [192.168.0.109]: FAILED! => {"msg": "The conditional check 'not ((item.mount == M2[0] and item.mount == M2[1]))\n' failed. The error was: error while evaluating conditional (not ((item.mount == M2[0] and item.mount == M2[1]))\n): 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'mount'\n\nThe error appears to be in '/etc/ansible/test.yml': line 14, 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 - name: \"Verify Mount Points\"\n ^ here\n"}
fatal: [192.168.0.107]: FAILED! => {"msg": "The conditional check 'not ((item.mount == M2[0] and item.mount == M2[1]))\n' failed. The error was: error while evaluating conditional (not ((item.mount == M2[0] and item.mount == M2[1]))\n): 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'mount'\n\nThe error appears to be in '/etc/ansible/test.yml': line 14, 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 - name: \"Verify Mount Points\"\n ^ here\n"}
PLAY RECAP **********************************************************************************************************************************************
192.168.0.107 : ok=1 changed=0 unreachable=0 failed=1 skipped=1 rescued=0 ignored=0
192.168.0.109 : ok=1 changed=0 unreachable=0 failed=1 skipped=1 rescued=0 ignored=0
First, you need to set gather_facts: true when using ansible defined variables.
Second, try to focus on the main issue here. Something along these lines should work:
---
- hosts: all
gather_facts: true
vars:
M1: ['RedHat','7']
M2: ['/u01','/u02']
tasks:
- name: "Verifying os and mount points"
debug:
msg: "{{ item }}"
loop:
- ansible_os_family
- ansible_distribution_major_version
- ansible_mounts
when:
- not item.ansible_os_family == 'M1[0]' # you could also replace 'M1[0]' with 'Redhat'.
- item.ansible_distribution_major_version == 'M1[1]'. # you could also replace 'M1[1]' with '7'.
- fail:
msg: "mount points not correctly"
loop: ansible_mounts
when:
- item.mount is defined
- item.mount != 'M2[0]'
- item.mount == 'M2[1]'
You shouldn't specify multiple fails in a single task, as fail is it's own task.

Ansible | The conditional check 'item.stat.exists' failed

**Getting an error like :**
"msg": "The conditional check 'item.stat.exists' failed. The error was: error while evaluating conditional (item.stat.exists): 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'stat'\n\nThe error appears to be in '/Ansible/roles/test/tasks/test1.yml': line 16, column 3, 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"
I have the following code:
**Inventory File :**
[test]
192.168.0.61 serverid=2
192.168.0.60 serverid=1
**Variable File :**
devangtest:
- ['1','adsdsdasd']
- ['2','kafka2sda']
- ['2','fggfdfgdf']
**Task File:**
- name: Check directory exists or not.
stat:
path: "/tmp/{{ item[1] }}"
register: alarm_details
when: "{{ serverid }} == {{ item[0] }}"
with_items:
- "{{ devangtest }}"
ignore_errors: yes
- debug:
msg: "{{ alarm_details.results }}"
- debug:
msg: "The file or directory exists"
when: item.stat.exists
with_items:
- alarm_details.results
# when: item.stat.isdir
ignore_errors: yes
- name: Create a directory if it does not exist
file:
path: "/tmp/{{ item[1] }}/test2"
state: directory
when:
- item.stat.exists == true
- "{{ serverid }} == {{ item[0] }}"
with_items:
- "{{ alarm_details.results }}"
- "{{ devangtest }}"
What I'm trying to do is check for the folders and if they don't exist or exist
Where am I wrong? Is it possible to use not stat.exists with a list of variables?
Thanks for the answer!
Given the inventory
shell> cat hosts
[test]
10.1.0.51 serverid='1'
10.1.0.52 serverid='2'
The playbook
shell> cat pb.yml
- hosts: test
vars:
devangtest:
- ['1','adsdsdasd']
- ['2','kafka2sda']
- ['2','fggfdfgdf']
tasks:
- name: Check directory exists or not.
stat:
path: "/tmp/{{ item.1 }}"
register: alarm_details
when: serverid|int == item.0|int
loop: "{{ devangtest }}"
- set_fact:
dir_stat: "{{ alarm_details.results|
json_query('[].{dir: item[1], stat: stat.exists}') }}"
- debug:
msg: "/tmp/{{ item }} exists"
loop: "{{ dir_stat|json_query('[?stat].dir') }}"
- name: Create a directory if it does not exist
file:
path: "/tmp/{{ item }}/test2"
state: directory
loop: "{{ dir_stat|json_query('[?stat].dir') }}"
gives when none of the directories exists
shell> ansible-playbook pb.yml
PLAY [test] ***
TASK [Check directory exists or not.] ***
skipping: [10.1.0.52] => (item=[u'1', u'adsdsdasd'])
ok: [10.1.0.51] => (item=[u'1', u'adsdsdasd'])
skipping: [10.1.0.51] => (item=[u'2', u'kafka2sda'])
skipping: [10.1.0.51] => (item=[u'2', u'fggfdfgdf'])
ok: [10.1.0.52] => (item=[u'2', u'kafka2sda'])
ok: [10.1.0.52] => (item=[u'2', u'fggfdfgdf'])
TASK [set_fact] ***
ok: [10.1.0.51]
ok: [10.1.0.52]
TASK [debug] ***
TASK [Create a directory if it does not exist] ***
PLAY RECAP ***
10.1.0.51: ok=2 changed=0 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0
10.1.0.52: ok=2 changed=0 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0
With existing directory /tmp/adsdsdasd at host test_01 the playbook gives
shell> ansible-playbook pb.yml
PLAY [test] ***
TASK [Check directory exists or not.] ***
skipping: [10.1.0.52] => (item=[u'1', u'adsdsdasd'])
ok: [10.1.0.52] => (item=[u'2', u'kafka2sda'])
ok: [10.1.0.51] => (item=[u'1', u'adsdsdasd'])
skipping: [10.1.0.51] => (item=[u'2', u'kafka2sda'])
skipping: [10.1.0.51] => (item=[u'2', u'fggfdfgdf'])
ok: [10.1.0.52] => (item=[u'2', u'fggfdfgdf'])
TASK [set_fact] ***
ok: [10.1.0.51]
ok: [10.1.0.52]
TASK [debug] ***
ok: [10.1.0.51] => (item=adsdsdasd) => {
"msg": "/tmp/adsdsdasd exists"
}
TASK [Create a directory if it does not exist] ***
changed: [10.1.0.51] => (item=adsdsdasd)
PLAY RECAP ***
10.1.0.51: ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
10.1.0.52: ok=2 changed=0 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0
You could use :
- name: Create a directory if it does not exist
file:
path: "/tmp/{{ item.1[1] }}/test2"
state: directory
when:
# - item.0.stat.exists = True
- item.0.stat.exists
- "{{ serverid }} == {{ item.1[0] }}"
with_together:
- '{{ alarm_details.results }}'
- '{{ devangtest }}'

Ansible set_facts based on OS version not working

I want to set the patch based on the OS version. came up with this playbook in Ansible version 2.8. but its giving The task includes an option with an undefined variable. error message in the debug line.
---
- hosts: all
gather_facts: yes
vars:
patch_name_8: 'centos8-updates'
patch_name_7: 'centos7-updates'
tasks:
- name: Set fact for CentOS 7
set_fact:
install_patch_name: "{{ patch_name_7 }}"
when: ansible_distribution_major_version == 7
- name: Set fact for CentOS 8
set_fact:
install_patch_name: "{{ patch_name_8 }}"
when: ansible_distribution_major_version == 8
- name: patch name display
debug:
msg: "install {{ install_patch_name }}"
How to set the install_patch_name variable value based on the OS version?
adding the error message:
TASK [patch name display] ************************************************************************************************************
fatal: [host01]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'install_patch_name' is undefined\n\nThe error appears to be in 't.yaml': line 23, 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
- name: patch name display\n ^ here\n"}
thanks
TLDR;
Use
when: ansible_distribution_major_version == "8"
or
when: ansible_distribution_major_version | int == 8
Explanation
Note: all following examples have been played against a centos:8 docker image.
The fact your are looking after is returned as a string :
[root#f6408271fc8c ~]# ansible localhost -m setup -a filter=ansible_distribution_major_version
localhost | SUCCESS => {
"ansible_facts": {
"ansible_distribution_major_version": "8"
},
"changed": false
}
Variables keep their types when using comparison and need to be correctly casted if need be, as demonstrated by the following playbook.
---
- hosts: localhost
tasks:
- name: default compare
debug:
msg: Comparison is true
when: ansible_distribution_major_version == 8
- name: compare as strings
debug:
msg: Comparison is true
when: ansible_distribution_major_version == "8"
- name: compare as ints
debug:
msg: Comparison is true
when: ansible_distribution_major_version | int == 8
Which gives
[root#f6408271fc8c ~]# ansible-playbook play.yml
PLAY [localhost] **************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
ok: [localhost]
TASK [default compare] ********************************************************************************************************************************************************************************************
skipping: [localhost]
TASK [compare as strings] *****************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "Comparison is true"
}
TASK [compare as ints] ********************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "Comparison is true"
}
PLAY RECAP ********************************************************************************************************************************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0

Resources