How to caching Windows fact of ansible_fact to Redis? - ansible

I have ansible_fact caching using redis.
It is well gathering to cache of redis for RHEL/AIX/HPUX and using ansible_fact of playbook.
but, Windows fact is not caching to redis.
What problem? or What configuration will do?
My ansible version is
[user]$ ansible --version
ansible 2.8.0
config file = /home/user/.ansible.cfg
configured module search path = [u'/home/user/utility/module']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /bin/ansible
python version = 2.7.5 (default, Mar 26 2019, 22:13:06) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
My ansible.cfg is
$ ansible-config dump --only-changed
ANSIBLE_PIPELINING(/home/user/.ansible.cfg) = True
CACHE_PLUGIN(/home/user/.ansible.cfg) = redis
CACHE_PLUGIN_CONNECTION(/home/user/.ansible.cfg) = localhost:6379:0
CACHE_PLUGIN_TIMEOUT(/home/user/.ansible.cfg) = 86100
DEFAULT_FACT_PATH(/home/user/.ansible.cfg) = /home/user/facts.d
DEFAULT_GATHERING(/home/user/.ansible.cfg) = smart
DEFAULT_REMOTE_USER(/home/user/.ansible.cfg) = user
First playbook is well done (gather_fact: yes), gathering ansible_memtotal_mb for windows
- name: gather fact
hosts: windows
gather_facts: yes
tasks:
- name: insertline
lineinfile
path: "fact.html"
insertbefore: "</th></tr>"
line: "{{ ansible_memtotal_mb }}"
Second playbook is problem (gather_fact: no), it is not use fact for cache data and fatal error occur.
- name: gather fact
hosts: windows
gather_facts: no
tasks:
- name: insertline
lineinfile
path: "fact.html"
insertbefore: "</th></tr>"
line: "{{ ansible_memtotal_mb }}"
The ansible_memtotal_mb is not using cache data of redis, despite of using cache for redis.
TASK [(lineinfile) Save result DATA to HTML file. ****************************
Tuesday 30 July 2019 10:41:40 +0900 (0:00:00.613) 0:00:07.639 **********
fatal: [hostname]: FAILED! => {"msg": "The task includes an option with an
undefined variable. The error was: 'ansible_memtotal_mb' is undefined
The error appears to be in '/home/user/systeminfo.yml': line 73, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: (lineinfile) Save result DATA to HTML file. [\"{{ WEB_DIR }}\"]
^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:
with_items:
- {{ foo }}
Should be written as:
with_items:
- \"{{ foo }}\"
"}

Related

Ansible with no action detected in task

I would like to check TLS/SSL certificate expiration date from Ansible.
After execute a valid YML (ansible-playbook TEST.yml) appears the following error:
ERROR! no action detected in task. This often indicates a misspelled module name, or incorrect module path.
The error appears to have been in '/scripts/Ansible/TEST.yml': line 15, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: check validity
^ here
This is the code:
- name: find cerfication files & expiration dates
hosts: 10.0.1.41
gather_facts: false
tasks:
- name: Find cert files under /etc/pki/tls/certs
find:
paths: /etc/pki/tls/certs
file_type: file
patterns: "*.crt"
recurse: yes
excludes: "localhost.crt"
register: find_result
- name: check validity
openssl_certificate_info:
path: "{{ item.path }}"
valid_at:
point_1: "+1w"
point_2: "+10w"
register: result
loop: "{{ find_result.files|flatten(levels=1) }}"
- debug: msg= "{{ result }}"
What's the exactly wrong?
My version:
[root#ansible Ansible]# ansible-playbook --version
ansible-playbook 2.6.20
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.6/site-packages/ansible
executable location = /usr/bin/ansible-playbook
python version = 2.6.6 (r266:84292, Jun 20 2019, 14:14:55) [GCC 4.4.7 20120313 (Red Hat 4.4.7-23)]
You are on a really old version of Ansible, I would recommend you to upgrade since your version had an end of support long ago (November 6, 2019).
Now, the module you are trying to use didn't exist in the version 2.6, that's the reason for the error raised by Ansible.
In the version 2.6, the way to check a certificate was to use the openssl_certificate module.
Something like:
- name: check validity
openssl_certificate:
path: "{{ item.path }}"
provider: assertonly
valid_at: 20171221162800Z
register: result
loop: "{{ find_result.files }}"

Ansible yaml file reporting error on Azure Pipelines

Please find below the Run Playbook task:
It is given me errors as below:
However, there are no syntax issues, I can assure you that for sure. Ran the syntax check on My CentOS VM (CentOS 7.6)
ansible-playbook -i azure_rm.py ../playbooks/common.yml --syntax-check
[ERROR]: /usr/lib/python2.7/site-packages/requests/__init__.py:91: RequestsDependencyWarning: urllib3 (1.24.1) or chardet (2.2.1) doesn't match a supported version!
RequestsDependencyWarning) No handlers could be found for logger "msrestazure.azure_active_directory"
playbook: ../playbooks/common.yml
The content of common.yml is below:
- name: Execute common operations across all Windows servers
hosts: all
gather_facts: yes
tasks:
- name: Set timezone to A.U.S. Eastern Time (DST)
win_timezone:
timezone: AUS Eastern Standard Time
- name: Set the region format to English Australia and copy settings to new profiles
win_region:
location: 12
format: en-AU
copy_settings: True
unicode_language: en-AU
register: result
- win_reboot:
when: result.restart_required
- name: Create directory structure
win_file:
path: C:\Temp\
state: directory
# - win_copy:
# src: /data/OnlyOnMyPC/ansible/scripts/
# dest: 'C:\temp\'
# remote_src: no
# force: yes

Only run Ansible task on box in specific group

I am trying to create a task that will only run on a box that is in a specific group (called pi).
I am using Ansible version:
ansible 2.3.2.0
config file = /Users/user/Development/raspbian-homeassistant/ansible.cfg
configured module search path = Default w/o overrides
python version = 3.6.3 (default, Dec 3 2017, 10:37:53) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.38)]
This is my current code:
- name: Set up zwave USB dongle
when: inventory_hostname in groups['pi']
blockinfile:
path: /var/local/homeassistant/.homeassistant/configuration.yaml
marker: "# {mark} ANSIBLE MANAGED BLOCK #"
insertafter: "# zwave dongle"
content: |2
zwave:
usb_path: /dev/ttyACM0
tags:
- config
- hass
It seems to work correctly when the hostname is in the group but throws an error when not.
This is the error I get when I run it on my vagrant box (in group vagrant):
fatal: [192.168.33.123]: FAILED! => {"failed": true, "msg": "The conditional check 'inventory_hostname in groups['pi']' failed. The error was: error while evaluating conditional (inventory_hostname in groups['pi']): Unable to look up a name or access an attribute in template string ({% if inventory_hostname in groups['pi'] %} True {% else %} False {% endif %}).\nMake sure your variable name does not contain invalid characters like '-': argument of type 'StrictUndefined' is not iterable\n\nThe error appears to have been in '/Users/andy/Development/raspbian-homeassistant/ansible/roles/configure-hass/tasks/main.yml': line 21, 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- name: Set up zwave USB dongle\n ^ here\n"}
Check if a list contains an item in Ansible suggests I have the syntax right but I guess that is for an older version of Ansible?
How do I fix this?
Generally, if you want tasks to apply only to hosts in a particular group, the way you do that is by creating a play that targets that group:
- hosts: pi
tasks:
- name: Set up zwave USB dongle
blockinfile:
path: /var/local/homeassistant/.homeassistant/configuration.yaml
marker: "# {mark} ANSIBLE MANAGED BLOCK #"
insertafter: "# zwave dongle"
content: |2
zwave:
usb_path: /dev/ttyACM0
tags:
- config
- hass
The error you're getting is because groups['pi']is undefined. There are a couple of ways of preventing the error. For example, you can explicitly check thatgroups['pi']` is defined before trying to use it:
- name: set up zwave USB dongle
when: groups['pi'] is defined and inventory_hostname in groups['pi']
Or you can use the default filter to provide a default value:
- name: set up zwave USB dongle
when: inventory_hostname in groups['pi']|default([])

ANSIBLE doesn't work - syntax check

I'm trying to setup on ansible my first playbooks.
Hovever... no matter which example I'm trying to test.. i receive similar error:
"xyz is not a valid attribute for a Play"
For example:
$ cat a2.yml
- name: Update and upgrade
become: true
apt:
upgrade: yes
update_cache: yes
$ ansible-playbook a2.yml --syntax-check
ERROR! 'apt' is not a valid attribute for a Play
The error appears to have been in '/home/pi/My/ansible/a2.yml': line 1, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: Update and upgrade
^ here
Whats wrong?
PS.
$ ansible --version
ansible 2.4.2.0
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/home/pi/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python2.7/dist-packages/ansible
executable location = /usr/local/bin/ansible
python version = 2.7.13 (default, Jan 19 2017, 14:48:08) [GCC 6.3.0 20170124]
See Playbooks Intro documentation.
Your entry should be a task item.
---
- hosts: ...
tasks:
- name: Update and upgrade
become: true
apt:
upgrade: yes
update_cache: yes

how to read json file using ansible

I have a json file in the same directory where my ansible script is. Following is the content of json file:
{ "resources":[
{"name":"package1", "downloadURL":"path-to-file1" },
{"name":"package2", "downloadURL": "path-to-file2"}
]
}
I am trying to to download these packages using get_url. Following is the approach:
---
- hosts: localhost
vars:
package_dir: "/var/opt/"
version_file: "{{lookup('file','/home/shasha/devOps/tests/packageFile.json')}}"
tasks:
- name: Printing the file.
debug: msg="{{version_file}}"
- name: Downloading the packages.
get_url: url="{{item.downloadURL}}" dest="{{package_dir}}" mode=0777
with_items: version_file.resources
The first task is printing the content of the file correctly but in the second task, I am getting the following error:
[DEPRECATION WARNING]: Skipping task due to undefined attribute, in the future this
will be a fatal error.. This feature will be removed in a future release. Deprecation
warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
You have to add a from_json jinja2 filter after the lookup:
version_file: "{{ lookup('file','/home/shasha/devOps/tests/packageFile.json') | from_json }}"
In case if you need to read a JSON formatted text and store it as a variable, it can be also handled by include_vars .
- hosts: localhost
tasks:
- include_vars:
file: variable-file.json
name: variable
- debug: var=variable
for future visitors , if you are looking for a remote json file read. this won't work
as ansible lookups are executed in the local
you should use a module like Slurp

Resources