ansible get rpm version of variable - ansible

I'm getting as an external-var the name of a service. I want to find the rpm version of that service.
I used the following playbook :
tasks:
- name: get the rpm or apt package facts
package_facts:
manager: "auto"
- name: get version of current installed rpm
set_fact:
rpm_version: "{{ packages['{{ service_name }}'] }}"
- name: print version of current installed rpm
debug:
msg: "{{ rpm_version }}"
I got the following error :
TASK [get version of current installed rpm] ************************************************************************************************************************************************
fatal: [10.20.10.74]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute '{{ service_name }}'\n\nThe error appears to be in '/home/m/my-playbook.yml': line 11, 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: get version of current installed rpm\n ^ here\n"}
I tried using escape characters like \' but it didn't really help.
How can I pass a variable to the packages dict?
When I replace the {{ service_name }} with a hard coded name it works as expected

Try this syntax:
- name: get version of current installed rpm
set_fact:
rpm_version: "{{ ansible_facts.packages[service_name] }}"

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 collection not detected when running playbook on AWX

I use modules from the collection netapp.ontap in my ansible playbook which works perfectly fine when run using ansible-playbook command.
However, when run from AWX, it fails to detect the collection and immediately throws an errors that it cannot detect the ansible module/collection.
I even tried to re-install the collection from the playbook itself but with no luck.
The ansible collection is confirmed to be installed as it already works fine when run outside AWX.
The host is running ansible 2.10.4.
Here is my playbook:
---
- hosts: all
gather_facts: yes
collections:
- netapp.ontap
tasks:
- name: Install Netapp Collection from Ansible Galaxy
shell: ansible-galaxy collection install netapp.ontap
- name: Run Task
import_tasks: tasks/hil.yml
Task:
- name: 'Gather SVMs'
netapp.ontap.na_ontap_info:
state: info
hostname: "{{ netapp_hostname }}"
username: "{{ netapp_username }}"
password: "{{ netapp_hv_password }}"
gather_subset:
- vserver_info
Error from AWX:
SSH password:
Vault password:
ERROR! couldn't resolve module/action 'netapp.ontap.na_ontap_info'. This often indicates a misspelling, missing collection, or incorrect module path.
The error appears to be in '/tmp/awx_421_gey54bdw/project/tasks/hil.yml': line 6, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: 'Gather SVMs'
^ here
This one looks easy to fix. It seems that there is a value started
with a quote, and the YAML parser is expecting to see the line ended
with the same kind of quote. For instance:
when: "ok" in result.stdout
Could be written as:
when: '"ok" in result.stdout'
Or equivalently:
when: "'ok' in result.stdout"
Update:
I created a collections/requirements.yml file, with the below details but now AWX fails the task itself.
collections/requirements.yml
collections:
name: https://github.com/ansible-collections/netapp.git
type: git
Error:
"/var/lib/awx/venv/awx/lib/python3.6/site-packages/awx/main/tasks.py",
line 1279, in run self.pre_run_hook(self.instance, private_data_dir) File
"/var/lib/awx/venv/awx/lib/python3.6/site-packages/awx/main/tasks.py",
line 1862, in pre_run_hook sync_task.run(local_project_sync.id) File
"/var/lib/awx/venv/awx/lib/python3.6/site-packages/awx/main/tasks.py",
line 698, in _wrapped return f(self, *args, **kwargs) File
"/var/lib/awx/venv/awx/lib/python3.6/site-packages/awx/main/tasks.py",
line 1444, in run raise AwxTaskError.TaskError(self.instance, rc)
Exception: project_update 435 (failed) encountered an error (rc=2), please
see task stdout for details.

How to use the listen_ports_facts module, ansible

Using ansible I want to check in which port tomcat is running, of course I think there are different ways to do it, but I found this ansible module
https://docs.ansible.com/ansible/latest/modules/listen_ports_facts_module.html
And I would like to use it, but according to the examples, I do not know how to use it.
I mean if I setup
gather_facts: true
And run the task
- name: List TCP ports
debug:
msg: "{{ ansible_facts.tcp_listen }}"
I got the error
TASK [discover-servers : List TCP ports] *******************************************************************************************************************************
task path: /home/A78252689/sap_bo/roles/discover-servers/tasks/tomcat_servers.yml:4
fatal: [2a00:da9:2:21ca:111:0:426:2]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'tcp_listen'\n\nThe error appears to be in '/home/A78252689/sap_bo/roles/discover-servers/tasks/tomcat_servers.yml': line 4, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: List TCP ports\n ^ here\n"}
If I set up the task exactly how it is in the example, I got an error in the first task Gather facts on listening ports
TASK [discover-servers : include_tasks] ********************************************************************************************************************************
task path: /home/A78252689/sap_bo/roles/discover-servers/tasks/main.yml:4
fatal: [2a00:da9:2:21ca:111:0:426:2]: FAILED! => {"reason": "no action detected in task. This often indicates a misspelled module name, or incorrect module path.\n\nThe error appears to be in '/home/A78252689/sap_bo/roles/discover-servers/tasks/tomcat_servers.yml': line 4, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Gather facts on listening ports\n ^ here\n"}
So, do you know how to use the module listen_ports_facts?
Thanks in advance for your kind support
The gather_facts stage of your playbook runs uses the setup module. It does not run listen_ports_facts, so if you don't run the module explicitly you won't have those facts available.
From the docs, the listen_ports_module creates the following facts:
tcp_listen
udp_listen
Using Ansible 2.9.2, the following works just fine:
---
- gather_facts: false
hosts: localhost
tasks:
- listen_ports_facts:
- debug:
msg: "{{ tcp_listen }}"
- debug:
msg: "{{ udp_listen }}"
If you're trying to run the listen_ports_facts module and you're getting the error "no action detected in task", it may be that you're running an older version of Ansible that doesn't have the listen_ports_facts module. It first showed up in version 2.9.

Playbook Install gpg key.The task includes an option with an undefined variable

Use ubuntu 18.04, and role dj-wasabi.zabbix-server
I have a task:
- name: "Debian | Install gpg key"
when: zabbix_repo == "zabbix"
apt_key:
id: "{{ sign_keys[zabbix_short_version][ansible_distribution_release]['sign_key'] }}"
url: http://repo.zabbix.com/zabbix-official-repo.key
register: zabbix_server_repo_files_installed
until: zabbix_server_repo_files_installed is succeeded
become: yes
tags:
- zabbix-server
- init
When i run playbook i get error:
TASK [zabbix-server : Debian | Install gpg key] ****************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute u'cosmic'\n\nThe error appears to have been in '/opt/infra/roles/zabbix-server/tasks/Debian.yml': line 34, 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: \"Debian | Install gpg key\"\n ^ here\n"}
variable sign_keys to vars.yml or default/main.yml is not.
What's my mistake?

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([])

Resources