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

**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 }}'

Related

Display Ansible playbook with lookups interpolated

I have an Ansible playbook that looks, in part, like this:
...
environment:
F2B_DB_PURGE_AGE: "{{ lookup('env','F2B_DB_PURGE_AGE') }}"
F2B_LOG_LEVEL: "{{ lookup('env','F2B_LOG_LEVEL') }}"
SSMTP_HOST: "{{ lookup('env','SSMTP_HOST') }}"
SSMTP_PORT: "{{ lookup('env','SSMTP_PORT') }}"
SSMTP_TLS: "{{ lookup('env','SSMTP_TLS') }}"
...
Is there any way to run ansible-playbook so that it will show the results of the YAML file after replacing the lookups with their values? That is, I would like to be able to run something like ansible-playbook file.yaml --dry-run and see on standard output (assuming the environment variables were set appropriately):
...
environment:
F2B_DB_PURGE_AGE: "20"
F2B_LOG_LEVEL: "debug"
SSMTP_HOST: "smtp.example.com"
SSMTP_PORT: "487"
SSMTP_TLS: "true"
...
Set the environment for testing
shell> cat env.sh
#!/usr/bin/bash
export F2B_DB_PURGE_AGE="20"
export F2B_LOG_LEVEL="debug"
export SSMTP_HOST="smtp.example.com"
export SSMTP_PORT="487"
export SSMTP_TLS="true"
shell> source env.sh
Given the inventory
shell> cat hosts
localhost ansible_connection=local
Q: "Run something like ansible-playbook file.yaml --dry-run and see environment"
A: The below playbook does the job
shell> cat file.yml
- hosts: all
vars:
my_environment:
F2B_DB_PURGE_AGE: "{{ lookup('env','F2B_DB_PURGE_AGE') }}"
F2B_LOG_LEVEL: "{{ lookup('env','F2B_LOG_LEVEL') }}"
SSMTP_HOST: "{{ lookup('env','SSMTP_HOST') }}"
SSMTP_PORT: "{{ lookup('env','SSMTP_PORT') }}"
SSMTP_TLS: "{{ lookup('env','SSMTP_TLS') }}"
tasks:
- block:
- debug:
msg: |
my_environment:
{{ my_environment|to_nice_yaml|indent(2) }}
- meta: end_play
when: dry_run|d(false)|bool
- debug:
msg: Continue ...
Set dry_run=true
shell> ansible-playbook file.yml -e dry_run=true
PLAY [all] ***********************************************************************************
TASK [debug] *********************************************************************************
ok: [localhost] =>
msg: |-
my_environment:
F2B_DB_PURGE_AGE: '20'
F2B_LOG_LEVEL: debug
SSMTP_HOST: smtp.example.com
SSMTP_PORT: '487'
SSMTP_TLS: 'true'
TASK [meta] **********************************************************************************
PLAY RECAP ***********************************************************************************
localhost: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
By default, the playbook will execute tasks
shell> ansible-playbook file.yml
PLAY [all] ***********************************************************************************
TASK [debug] *********************************************************************************
skipping: [localhost]
TASK [meta] **********************************************************************************
skipping: [localhost]
TASK [debug] *********************************************************************************
ok: [localhost] =>
msg: Continue ...
PLAY RECAP ***********************************************************************************
localhost: ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
Optionally, let the playbook gather facts and use the dictionary ansible_env. Use the filer ansible.utils.keep_keys to select your variables
- hosts: all
gather_facts: true
vars:
my_environment_vars:
- F2B_DB_PURGE_AGE
- F2B_LOG_LEVEL
- SSMTP_HOST
- SSMTP_PORT
- SSMTP_TLS
my_environment: "{{ ansible_env|
ansible.utils.keep_keys(target=my_environment_vars) }}"
tasks:
- block:
- debug:
msg: |
my_environment:
{{ my_environment|to_nice_yaml|indent(2) }}
- meta: end_play
when: dry_run|d(false)|bool
- debug:
msg: Continue ...

Ansible delegate_to: localhost task running on remote host

I have paly to copy the web server logs from remote machine to Ansible machine. At the end of the playbook, I want to find the file with pattern and remove it. in the task I have defined delegate_to: 127.0.0.1 but this task is running one of the remote machine.
How to make these task run on only localhost and run once:
- name: Finds files and folders
find:
paths: "/tmp/"
patterns: "access-*.tar.gz"
recurse: no
use_regex: yes
register: result
delegate_to: 127.0.0.1
run_once: true
tags:
- pulllogs
- name: display filename
debug:
msg: "{{ result }}"
delegate_to: 127.0.0.1
run_once: true
tags:
- pulllogs
Output:
TASK [operate : Finds files and folders] ***********************************************************************************************************************************************
ok: [host01]
TASK [operate : display filename] ******************************************************************************************************************************************************
ok: [host01] =>
msg:
changed: false
examined: 640
failed: false
files: []
matched: 0
msg: ''
In Ansible 2.10 the delegation works as expected. In the output, I can't see the delegation either. But both plays, running at localhost and at delegated localhost respectively, show the same results. For example, given the archive at localhost
shell> sudo find /tmp -name 'access-*.tar.gz'
/tmp/access-01.tar.gz
and no archives at the remote host
shell> ssh admin#test_11 sudo find /tmp -name 'access-*.tar.gz'
the playbook
- hosts: localhost
tasks:
- find:
paths: /tmp
patterns: "access-*.tar.gz"
recurse: false
use_regex: false
register: result
- debug:
msg: "{{ result.files|map(attribute='path')|list }}"
- hosts: test_11,test_12,test_13
tasks:
- block:
- find:
paths: /tmp
patterns: "access-*.tar.gz"
recurse: false
use_regex: false
register: result
- debug:
msg: "{{ result.files|map(attribute='path')|list }}"
delegate_to: 127.0.0.1
run_once: true
shows the same results at localhost and delegated localhost
PLAY [localhost] **********************************************************
TASK [find] ***************************************************************
ok: [localhost]
TASK [debug] **************************************************************
ok: [localhost] =>
msg:
- /tmp/access-01.tar.gz
PLAY [test_11,test_12,test_13] ********************************************
TASK [find] ***************************************************************
ok: [test_11]
TASK [debug] **************************************************************
ok: [test_11] =>
msg:
- /tmp/access-01.tar.gz
PLAY RECAP ****************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
test_11 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

ansible: create a list from comma separated string

I want to create a list from comma separated string to pass to loop in ansible, sometime variable can have only one value also
var1=test1,test2 and it can be var1=test1 also
here is my code
- name: Separate facts
set_fact: groups="{{ var1.split(',') }}"
- name: delete
gcp_compute_instance_group:
name: "{{ item }}"
zone: xxx
project: xxx
auth_kind: serviceaccount
service_account_file: xxx
state: absent
loop: "{{ groups }}"
this doesn't work, how can i achieve my requirement
your filter is correct, you do get a list variable. please see below PB and output:
---
- hosts: localhost
gather_facts: false
vars:
var1: test1,test2
var2: test3
tasks:
- name: Create the list
set_fact:
list_var1: "{{ var1.split(',') }}"
list_var2: "{{ var2.split(',') }}"
- debug:
var: list_var1
- debug:
var: list_var2
result:
[is#orangehat-29 temp]$ ansible-playbook test.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [localhost] ***********************************************************************************************************************************************************************************************************************
TASK [Create the list] *****************************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [debug] ***************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"list_var1": [
"test1",
"test2"
]
}
TASK [debug] ***************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"list_var2": [
"test3"
]
}
PLAY RECAP *****************************************************************************************************************************************************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[is#orangehat-29 temp]$

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

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

Ansible: move on to the next task if the task is completed on one host

In ansible what i require is to check for a file is available on two hosts. But if the file is available on even one host i need to cancel the task on other host and move onto the next task. The reason why i require this is because a the next task can only be done if that particular file is available and that file can be randomly written to any of the hosts.
The following play does exactly what you want:
---
- hosts:
- server1
- server2
gather_facts: False
vars:
file_name: 'foo.bar'
tasks:
- name: wait for file
wait_for:
path: '{{ file_name }}'
state: present
timeout: 30
ignore_errors: True
- name: stat
stat:
path: '{{ file_name }}'
register: result
- name: next
debug:
msg: "File {{ file_name }} available on {{ ansible_host }}"
when: result.stat.isreg is defined and result.stat.isreg
The output is:
PLAY [server1,server2] *********************************************************
TASK [wait for file] ***********************************************************
ok: [server1]
fatal: [server2]: FAILED! => {"changed": false, "elapsed": 3, "msg": "Timeout when waiting for file foo.bar"}
...ignoring
TASK [stat] ********************************************************************
ok: [server1]
ok: [server2]
TASK [next] ********************************************************************
skipping: [server2]
ok: [server1] => {
"msg": "File foo.bar available on server1"
}
PLAY RECAP *********************************************************************
server1 : ok=3 changed=0 unreachable=0 failed=0
server2 : ok=0 changed=0 unreachable=0 failed=0
You can use the stat module to check the status like below and for also you can add the serial:1 below hosts: in your playbook
stat:
path: /path/to/something
register: p
debug:
msg: "Path exists and is a directory"
when: p.stat.isdir is defined and p.stat.isdir
https://docs.ansible.com/ansible/latest/modules/stat_module.html for more details

Resources