I am ansible beginner and I have a problem with ansible playbook that should gather info about system version from multiple servers.
First step was to gather info about server (if it uses Jboss or Tomcat and where it is) - I was able to do it and store it in list like this:
"server_list": [
{
"hostname": "tst-24.ph.koop.cz",
"path": "/srv/ais/test/knz-batch/eap",
"pid": "14660",
"type": "wildfly/jboss",
"version": "",
"version_cmd": "/srv/ais/test/knz-batch/eap/bin/standalone.sh --version"
},
{
"hostname": "tst-24.ph.koop.cz",
"path": "/srv/ais/test/knz-eap/eap",
"pid": "20153",
"type": "wildfly/jboss",
"version": "",
"version_cmd": "/srv/ais/test/knz-eap/eap/bin/standalone.sh --version"
},
{
"hostname": "tst-24.ph.koop.cz",
"path": "/srv/ais/skoleni/knz-ws-int/eap",
"pid": "24861",
"type": "wildfly/jboss",
"version": "",
"version_cmd": "/srv/ais/skoleni/knz-ws-int/eap/bin/standalone.sh --version"
},
{
"hostname": "tst-24.ph.koop.cz",
"path": "/srv/ais/skoleni/knz-ws/wildfly",
"pid": "25195",
"type": "wildfly/jboss",
"version": "",
"version_cmd": "/srv/ais/skoleni/knz-ws/wildfly/bin/standalone.sh --version"
},
{
"hostname": "tst-24.ph.koop.cz",
"path": "/srv/ais/skoleni/knz-ws/undertow",
"pid": "25667",
"type": "tomcat",
"version": "",
"version_cmd": "/srv/ais/skoleni/knz-ws/undertow/bin/version.sh --version"
},
{
"hostname": "tst-24.ph.koop.cz",
"path": "/srv/ais/skoleni/knz/wildfly",
"pid": "26446",
"type": "wildfly/jboss",
"version": "",
"version_cmd": "/srv/ais/skoleni/knz/wildfly/bin/standalone.sh --version"
}
]
Now I need to run other shell command (that will use version_cmd) to get that version (with use of GREP).
But I don't know how to write into list when looping through. And second problem is how to make more conditions (to use other regex for wildfly/jboss)
- name: Get server version
shell: "{{item.version_cmd}}| grep -Po {{ regexp_tomcat_version }}"
loop: "{{ server_list }}"
loop_control:
label: "Check version for {{ item.path }}"
when: item.type == "tomcat"
register: server_list.version
vars:
regexp_tomcat_version: 'Server version:\s*([^(\n)]*)'
ignore_errors: yes
Is this thing even possible in Ansible?
For example, given the list below for testing
server_list:
- hostname: srv1
path: /usr/bin/cc
type: prod
version_cmd: cc --version
version_parse: awk 'FNR == 1 {print $4}'
- hostname: srv1
path: /usr/local/bin/python
type: devel
version_cmd: python --version
version_parse: awk 'FNR == 1 {print $2}'
- hostname: srv2
path: /usr/bin/cc
type: prod
version_cmd: cc --version
version_parse: awk 'FNR == 1 {print $4}'
- hostname: srv2
path: /usr/local/bin/python
type: devel
version_cmd: python --version
version_parse: awk 'FNR == 1 {print $2}'
Create a dynamic group of hostnames in the first play and run it in the second play. In the second play, register the versions into the variable server_list_versions. Then iterate ansible_play_hosts and create the dictionary versions.
- name: Create group server_group
hosts: localhost
tasks:
- add_host:
name: "{{ item }}"
groups: server_group
server_list: "{{ server_list }}"
loop: "{{ server_list|map(attribute='hostname')|unique }}"
- name: Collect versions
hosts: server_group
gather_facts: false
tasks:
- shell: "{{ item.version_cmd }}|{{ item.version_parse }}"
loop: "{{ server_list|selectattr('hostname', 'eq', inventory_hostname) }}"
loop_control:
label: "Check version for {{ item.path }}"
# when: item.type == "devel"
register: server_list_versions
- set_fact:
versions: "{{ versions|d({})|
combine({item: hostvars[item].server_list_versions.results|
json_query('[].{path: item.path,
version: stdout,
stderr: stderr}')}) }}"
loop: "{{ ansible_play_hosts }}"
run_once: true
gives
versions:
srv1:
- path: /usr/bin/cc
stderr: ''
version: 11.0.1
- path: /usr/local/bin/python
stderr: ''
version: 3.8.12
srv2:
- path: /usr/bin/cc
stderr: ''
version: 11.0.1
- path: /usr/local/bin/python
stderr: '/bin/sh: python: not found'
version: ''
If you want to update the attribute version in server_list create a dictionary
- set_fact:
vers_dict: "{{ vers_dict|d({})|
combine({item: hostvars[item].server_list_versions.results|
json_query('[].{path: item.path,
version: stdout}')|
items2dict(key_name='path',
value_name='version')}) }}"
loop: "{{ ansible_play_hosts }}"
run_once: true
gives
vers_dict:
srv1:
/usr/bin/cc: 11.0.1
/usr/local/bin/python: 3.8.12
srv2:
/usr/bin/cc: 11.0.1
/usr/local/bin/python: ''
Then, use this dictionary to update the attribute version
- set_fact:
server_list_new: "{{ server_list_new|d([]) +
[item|combine({'version': version})] }}"
loop: "{{ server_list }}"
vars:
version: "{{ vers_dict[item.hostname][item.path] }}"
run_once: true
gives
server_list_new:
- hostname: srv1
path: /usr/bin/cc
type: prod
version: 11.0.1
version_cmd: cc --version
version_parse: awk 'FNR == 1 {print $4}'
- hostname: srv1
path: /usr/local/bin/python
type: devel
version: 3.8.12
version_cmd: python --version
version_parse: awk 'FNR == 1 {print $2}'
- hostname: srv2
path: /usr/bin/cc
type: prod
version: 11.0.1
version_cmd: cc --version
version_parse: awk 'FNR == 1 {print $4}'
- hostname: srv2
path: /usr/local/bin/python
type: devel
version: ''
version_cmd: python --version
version_parse: awk 'FNR == 1 {print $2}'
Related
I am trying to assign a range of IP to different hosts, but first I am checking if the IP is already attributed. If it is, it gives me an error, so I would like to avoid that too(actually I can assign the IP, but can't pass that error, I am using ignore_errors: yes for now but I would like a better way). Here is part of my script:
---
- hosts: all
gather_facts: yes
become: yes
vars_files:
- client2
tasks:
- set_fact:
intip: "{{ intip | default([]) + [item] }}"
loop: "{{ ansible_interfaces | map('extract',ansible_facts, 'ipv4') | select('defined') | map(attribute='address') | list }}"
ignore_errors: yes
- name: Check IP address
shell:
"ip a a '{{ start_ip|ipmath(my_idx) }}' dev spanbr"
loop_control:
index_var: my_idx
when: (item == inventory_hostname )
loop: "{{ ansible_play_hosts }}"
ignore_errors: yes
I am using this var file, but maybe there is a better way? (I am trying to use something like the first example, but can't get my head around to use it on every hosts)
First vars file (didn't try yet):
client:
- interface:
- local_ip: 10.10.10.10
- name: eth1
- interface:
- local_ip: 10.10.10.11
- name: eth1
Second file:
interface:
- config:
- name: eth0
- config:
- name: eth1
start_ip: 10.10.10.10
I can get only one interface getting the IP and ignoring the address, but as the when conditional is not a loop it only check one interface:
Output:
TASK [Check IP address] ************************************************************************************************************************
skipping: [host2] => (item=host1)
failed: [host1] (item=host1) => {"ansible_index_var": "my_idx", "ansible_loop_var": "item", "changed": true, "cmd": "ip a a '10.10.10.10' dev spanbr", "delta": "0:00:00.005429", "end": "2022-10-20 08:49:43.800954", "item": "host1", "msg": "non-zero return code", "my_idx": 0, "rc": 2, "start": "2022-10-20 08:49:43.795525", "stderr": "RTNETLINK answers: File exists", "stderr_lines": ["RTNETLINK answers: File exists"], "stdout": "", "stdout_lines": []}
skipping: [host1] => (item=host2)
...ignoring
failed: [host2] (item=host2) => {"ansible_index_var": "my_idx", "ansible_loop_var": "item", "changed": true, "cmd": "ip a a '10.10.10.11' dev spanbr", "delta": "0:00:00.002691", "end": "2022-10-20 07:49:43.815422", "item": "host2", "msg": "non-zero return code", "my_idx": 1, "rc": 2, "start": "2022-10-20 07:49:43.812731", "stderr": "RTNETLINK answers: File exists", "stderr_lines": ["RTNETLINK answers: File exists"], "stdout": "", "stdout_lines": []}
...ignoring
I would like to use a notify but I need to use the loop on the task itself so that may be an issue...
Any ideas please??
Here is the output of intip (set_fact) if that helps:
TASK [set_fact] ********************************************************************************************************************************
ok: [host1] => (item=192.168.1.100)
ok: [host1] => (item=127.0.0.1)
ok: [host1] => (item=10.10.10.10)
ok: [host1] => (item=169.254.0.1)
ok: [host2] => (item=127.0.0.1)
ok: [host2] => (item=10.10.10.11)
ok: [host2] => (item=192.168.1.101)
Alright, I found a way to do it, by using host_vars and group_vars, and making my script even easier to write :
---
- hosts: all
gather_facts: yes
become: yes
tasks:
- set_fact:
intip: "{{ hostvars[inventory_hostname].ansible_all_ipv4_addresses }}"
- name: Check IP address
shell:
"ip a a '{{ local_ip }}' dev spanbr"
when: local_ip not in intip
Just by creating 2 files in host_vars.
host1.yaml:
ansible_host: "some_ip"
local_ip: 10.10.10.10
and the same for host2.yaml with different values. All is working great and it's making the script much easier to read too.
Below is my JSON file:
[
{
"?xml": {
"attributes": {
"encoding": "UTF-8",
"version": "1.0"
}
}
},
{
"domain": [
{
"name": "mydom"
},
{
"domain-version": "12.2.1.3.0"
},
{
"server": [
{
"name": "AdminServer"
},
{
"ssl": {
"name": "AdminServer"
}
},
{
"listen_port": "12400"
},
{
"listen_address": "mydom.host1.bank.com"
}
]
},
{
"server": [
{
"name": "myserv1"
},
{
"ssl": [
{
"name": "myserv1"
},
{
"login-timeout-millis": "25000"
}
]
},
{
"listen_port": "22421"
}
]
}
]
}
]
Here is the code to get the listen port number form the json
---
- name: ReadJsonfile
hosts: localhost
tasks:
- name: Display the JSON file content
shell: "cat this.json"
register: result
- name: save the Json data to a Variable as a Fact
set_fact:
jsondata: "{{ result.stdout | from_json }}"
- name: create YML for server name with Listen port
shell: "echo {{ server.0.name }}_httpport: {{ httpport[0].listen_port }}>>{{ playbook_dir }}/wlsdatadump.yml"
loop: "{{ jsondata[1].domain }}"
vars:
server: "{{ item.server | selectattr('name', 'defined') }}"
httpport: "{{ item.server | selectattr('listen_port', 'defined') | list }}"
when: item.server is defined and (item.server | selectattr('listen_port', 'defined')) != []
I get the below error when executing the play
TASK [create YML for server name with Listen port] ************************************************************
skipping: [localhost] => (item={'name': 'mydom'})
skipping: [localhost] => (item={'domain-version': '12.2.1.3.0'})
failed: [localhost] (item={'server': [{'name': 'AdminServer'}, {'ssl': {'name': 'AdminServer'}}, {'listen_port': '12400'}, {'listen_address': 'mydom.host1.bank.com'}]}) => {"ansible_loop_var": "item", "changed": true, "cmd": "echo AdminServer_httpport: 12400>>/web/aes/admin/playbooks/dump.yml", "delta": "0:00:00.007706", "end": "2022-03-17 04:43:24.665832", "item": {"server": [{"name": "AdminServer"}, {"ssl": {"name": "AdminServer"}}, {"listen_port": "12400"}, {"listen_address": "mydom.host1.bank.com"}]}, "msg": "non-zero return code", "rc": 1, "start": "2022-03-17 04:43:24.658126", "stderr": "/bin/sh: 12400: Bad file descriptor", "stderr_lines": ["/bin/sh: 12400: Bad file descriptor"], "stdout": "", "stdout_lines": []}
If i change the port number from numeric to non-numeric say change "12400" to "portfirst" the playbook works fine.
This issue may have to do with the data type.
Can you please suggest how can i overcome this error?
you create a file listenport.j2 in folder templates:
{% for item in jsondata[1].domain if item.server is defined and (item.server | selectattr('listen_port', 'defined')) != [] %}
{{ item.server.0.name }}_httpport: {{ (item.server | selectattr('listen_port', 'defined')| list).0.listen_port }}
{% endfor %}
playbook:
- name: ReadJsonfile
hosts: localhost
tasks:
- name: Display the JSON file content
shell: "cat ./file2.json"
register: result
- name: save the Json data to a Variable as a Fact
set_fact:
jsondata: "{{ result.stdout | from_json }}"
- name: template
template:
src: listenport.j2
dest: "{{ playbook_dir }}/wlsdatadump.yml"
result in file wlsdatadump.yml:
AdminServer_httpport: 12400
myserv1_httpport: 22421
if you are working on lot of hosts (not only on localhost) and just want to create the file on localhost, use delegate_to: localhost inside the task
Considering the output of ios_facts module (or any similar gathering fact module), I wrote the following Ansible playbook:
name: checking interface status
gather_facts: no
hosts: iosxe
tasks:
name: get cisco config
cisco.ios.ios_facts:
gather_subset: all
register: cisco_output
name: task001
debug:
var: cisco_output['ansible_facts']['ansible_net_interfaces']
loop: "{{ ansible_facts['ansible_net_interfaces'] | dict2items }}"
when: item.lineprotocol == "up"
For the sake of practicing Ansible, I wanted the playbook to show list of interfaces in "up" state. The relative part in the output of the "ios_fact" shows that it is a "dictionary" not "list".
"ansible_net_interfaces": {
"GigabitEthernet1": {
"bandwidth": 1000000,
"description": null,
"duplex": "Full",
"ipv4": [
{
"address": "10.106.31.229",
"subnet": "24"
}
],
"lineprotocol": "up",
"macaddress": "000c.29c3.a8f3",
"mediatype": "Virtual",
"mtu": 1500,
"operstatus": "up",
"type": "CSR vNIC"
},
"GigabitEthernet2": {
"bandwidth": 1000000,
"description": "CSR2",
"duplex": "Full",
"ipv4": [
{
"address": "10.171.73.33",
"subnet": "27"
}
],
"lineprotocol": "up",
"macaddress": "000c.29c3.a8fd",
"mediatype": "Virtual",
"mtu": 1500,
"operstatus": "up",
"type": "CSR vNIC"
}, ...
I got several different errors and tried to workaround by changing the playbook each time as these:
loop: "{{ ansible_facts['ansible_net_interfaces'] | dict2items }}"
loop: "{{ cisco_output['ansible_facts']['ansible_net_interfaces'] }}"
loop: "{{ cisco_output['ansible_facts']['ansible_net_interfaces'] | dict2items }}"
But was unsuccessful as I got different errors again. What would be the right way of this?
The dict2items filter will convert the dict to an array of items, where the keys will be GigabitEthernet1, GigabitEthernet2, and the values of these keys will sub-dicts.
"item.key": "GigabitEthernet1"
"item.key": "GigabitEthernet2"
So, the lineprotocol key will be in the item.value, i.e. item.value.lineprotocol. A simple task such as below can demonstrate the same:
- debug:
msg: "{{ item.key }} is up"
loop: "{{ cisco_output['ansible_facts']['ansible_net_interfaces | dict2items }}"
when: item.value.lineprotocol == "up"
The attribute ipv4 is a list. Use with_subelements if you want to take into account the fact that there might be more IP addresses configured, e.g.
- debug:
msg: "{{ item.0.key }} {{ item.1.address }}"
with_subelements:
- "{{ ansible_net_interfaces|dict2items|
selectattr('value.lineprotocol', 'eq', 'up') }}"
- value.ipv4
gives
msg: GigabitEthernet1 10.106.31.229
msg: GigabitEthernet2 10.171.73.33
I'm struggling to loop over hostvars of the registered output of all the hosts in dynamic inventory.
Here is the code.
$cat collect.yaml
---
- hosts: "{{ env }}"
become: True
tasks:
- name: Get dockerinfo
docker_host_info:
containers: yes
register: result
- name: Debug dockerInfo
debug:
var: result.containers
- name: dynamic grouping
add_host:
name: "{{ item[0] }}"
groups: "{{ item[1].Image | regex_replace('.*?/(.*?):.*', '\\1') }}"
loops:
- "{{ ansible_play_batch }}"
- "{{ hostvars[item].result.containers }}"
Error i get is item not defined. I would want the hosts refer to their respective result.containers. Not sure on how to use hostvars for the host to refer their respective result.containers.
Here is result.container output.
TASK [Debug dockerInfo]
ok: [vm1.nodekite.com] => {
"result.containers": [
{
"Image": "ca.docker/webproxy:1.0.0",
},
{
"Image": "docker.local/egacustomer:1.0.1",
},
]}
ok: [vm2.nodekite.com ] => {
"result.containers": [
{
"Image": "ca.docker/webproxyui:1.0.0",
},
{
"Image": "cna-docker-local/lega-customer:1.0.1",
},
]}
Here is the what i'm trying to achieve
changed: [vm1.nodekite.com] => {
"add_host": {
"groups": [
"webproxy"
],
"host_name": "vm1.nodekite.com",
},
changed: [vm1.nodekite.com] => {
"add_host": {
"groups": [
"egacustomer"
],
"host_name": "vm1.nodekite.com",
},
changed: [vm2.nodekite.com] => {
"add_host": {
"groups": [
"webproxy" >> this should be webproxyui
],
"host_name": "vm2.nodekite.com",
},
changed: [vm2.nodekite.com] => {
"add_host": {
"groups": [
"egacustomer" >> this should be lega-customer
],
"host_name": "vm2.nodekite.com",
},
Any help would be greatly appreciated.
I would run this task using Images Names
- hosts: "{{ group }}"
gather_facts: false
become: true
become_method: sudo
tasks:
- name: stop or restart docker containers
command: "docker {{ state }} {{ container_name }}"
How about just group_by paired with the loop?
- hosts: "{{ env }}"
become: True
tasks:
- name: Get dockerinfo
docker_host_info:
containers: yes
register: result
- debug:
var: result.containers
- group_by:
key: "container_{{ item.image | regex_replace('.*?/(.*?):.*', '\\1') }}"
loop: "{{ result.containers }}"
- debug:
var: group_names
You don't need to add the prefix. But this would add each host to groups with their container image prefixes. You should be able to use the group later in the play or playbook.
With ansible, looping over a list of items that return a dict(?) with listed values.
Basically I want to check a dict of packages to see if they are installed or not (and later on return a message for any missing packages).
Any loop/with_dict combo tried so far return various errors that the specific variable can't be found.
When querying the status itself (yum module) it does put the full output into the {{ pkg }} var, per package.
---
- name: Ansible tests playbook
hosts: vms
remote_user: root
vars:
pkgs:
- yum-utils
- mariadb-libs
tasks:
- name: Check packages
yum:
list: "{{ item }}"
disablerepo: '*'
register: pkg
loop: "{{ pkgs }}"
If I then simply output the contents of {{ pkg.results }} with :
- name: list
debug:
msg: "{{ item }}"
loop: "{{ pkg.results }}"
I get :
root#vm011:~/ovirt# ansible-playbook check.yml
PLAY [Ansible tests playbook] **************************************************************************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************************************************************************************
ok: [vm017.warp]
TASK [Check paclages] **********************************************************************************************************************************************************************
ok: [vm017.warp] => (item=yum-utils)
ok: [vm017.warp] => (item=mariadb-libs)
TASK [list] ********************************************************************************************************************************************************************************
ok: [vm017.warp] => (item=None) =>
msg:
changed: false
failed: false
invocation:
module_args:
allow_downgrade: false
conf_file: null
disable_gpg_check: false
disable_plugin: []
disablerepo: '*'
enable_plugin: []
enablerepo: null
exclude: null
install_repoquery: true
installroot: /
list: yum-utils
name: null
security: false
skip_broken: false
state: installed
update_cache: false
update_only: false
validate_certs: true
item: yum-utils
results:
- arch: noarch
envra: 0:yum-utils-1.1.31-50.el7.noarch
epoch: '0'
name: yum-utils
release: 50.el7
repo: installed
version: 1.1.31
yumstate: installed
ok: [vm017.warp] => (item=None) =>
msg:
changed: false
failed: false
invocation:
module_args:
allow_downgrade: false
conf_file: null
disable_gpg_check: false
disable_plugin: []
disablerepo: '*'
enable_plugin: []
enablerepo: null
exclude: null
install_repoquery: true
installroot: /
list: mariadb-libs
name: null
security: false
skip_broken: false
state: installed
update_cache: false
update_only: false
validate_certs: true
item: mariadb-libs
results:
- arch: x86_64
envra: 1:mariadb-libs-5.5.60-1.el7_5.x86_64
epoch: '1'
name: mariadb-libs
release: 1.el7_5
repo: installed
version: 5.5.60
yumstate: installed
How can I get just the yumstate value (installed, or otherwise), per package, into a separate variable ?
If, for instance, I try to debug the msg with
- name: list
debug:
msg: "{{ item.yumstate }}"
loop: "{{ pkg.results }}"
I get:
fatal: [vm017.warp]: FAILED! =>
msg: |-
The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'yumstate'
you are looping correctly through the pkg.results, but in each item the yumstate is under another results key. you should use:
- name: list
debug:
msg: "{{ item.results.yumstate }}"
loop: "{{ pkg.results }}"
i tried your code on my fedora, and the results for one of the 2 packages has as value a list, so the item.results.yumstate would not work: Fedora response for yum utils:
"item.results": [
{
"arch": "noarch",
"epoch": "0",
"name": "yum-utils",
"nevra": "0:yum-utils-1.1.31-517.fc29.noarch",
"release": "517.fc29",
"repo": "fedora",
"version": "1.1.31",
"yumstate": "available"
},
{
"arch": "noarch",
"epoch": "0",
"name": "yum-utils",
"nevra": "0:yum-utils-1.1.31-518.fc29.noarch",
"release": "518.fc29",
"repo": "updates",
"version": "1.1.31",
"yumstate": "available"
}
]
for mariadb-libs, no packages found, response was:
"item.results": []
hope it helps
You did indeed point me in the correct way.
- name: list
debug:
msg: "Package {{ item.results[0].name }} is {{ item.results[0].yumstate }}"
loop: "{{ pkg.results }}"
Did the trick, with just "{{ item.results.yumstate }}" it still returned an error
TASK [list] ********************************************************************************************************************************************************************************
fatal: [vm017.warp]: FAILED! =>
msg: |-
The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'yumstate'
Adding the [0] got me:
TASK [list] ********************************************************************************************************************************************************************************
ok: [vm017.warp] => (item=None) =>
msg: Package yum-utils is installed
ok: [vm017.warp] => (item=None) =>
msg: Package mariadb-libs is installed
You might want to consider nested loops , also known aa "loops and includes" if you're attempting fancy things.
https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#defining-inner-and-outer-variable-names-with-loop-var