Skip to content

Instantly share code, notes, and snippets.

@zeitounator
Last active August 27, 2023 09:41
Show Gist options
  • Save zeitounator/1561e1ad688e361c5c0532ac3ca35f5e to your computer and use it in GitHub Desktop.
Save zeitounator/1561e1ad688e361c5c0532ac3ca35f5e to your computer and use it in GitHub Desktop.
$ pyenv virtualenv 2.7.18 test_ansible_2.8
created virtual environment CPython2.7.18.final.0-64 in 116ms
creator CPython2Posix(dest=/home/user/.pyenv/versions/2.7.18/envs/test_ansible_2.8, clear=False, no_vcs_ignore=False, global=False)
seeder FromAppData(download=False, pip=bundle, wheel=bundle, setuptools=bundle, via=copy, app_data_dir=/home/user/.local/share/virtualenv)
added seed packages: pip==20.3.4, setuptools==44.1.1, wheel==0.37.1
activators NushellActivator,PythonActivator,FishActivator,CShellActivator,PowerShellActivator,BashActivator
Looking in links: /tmp/tmpzj_Ydz
Requirement already satisfied: setuptools in /home/user/.pyenv/versions/2.7.18/envs/test_ansible_2.8/lib/python2.7/site-packages (44.1.1)
Requirement already satisfied: pip in /home/user/.pyenv/versions/2.7.18/envs/test_ansible_2.8/lib/python2.7/site-packages (20.3.4)
$ pyenv activate test_ansible_2.8
(test_ansible_2.8) $ python --version
Python 2.7.18
(test_ansible_2.8) $ pip install 'ansible<2.9' jmespath
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
Collecting ansible<2.9
Downloading ansible-2.8.20.tar.gz (12.7 MB)
|████████████████████████████████| 12.7 MB 2.7 MB/s
Collecting jmespath
Using cached jmespath-0.10.0-py2.py3-none-any.whl (24 kB)
Collecting jinja2
Using cached Jinja2-2.11.3-py2.py3-none-any.whl (125 kB)
Collecting PyYAML
Using cached PyYAML-5.4.1-cp27-cp27mu-manylinux1_x86_64.whl (574 kB)
Collecting cryptography
Using cached cryptography-3.3.2-cp27-cp27mu-manylinux2010_x86_64.whl (2.6 MB)
Collecting MarkupSafe>=0.23
Using cached MarkupSafe-1.1.1-cp27-cp27mu-manylinux1_x86_64.whl (24 kB)
Collecting ipaddress; python_version < "3"
Using cached ipaddress-1.0.23-py2.py3-none-any.whl (18 kB)
Collecting six>=1.4.1
Using cached six-1.16.0-py2.py3-none-any.whl (11 kB)
Collecting enum34; python_version < "3"
Using cached enum34-1.1.10-py2-none-any.whl (11 kB)
Collecting cffi>=1.12
Using cached cffi-1.15.1-cp27-cp27mu-manylinux1_x86_64.whl (390 kB)
Collecting pycparser
Using cached pycparser-2.21-py2.py3-none-any.whl (118 kB)
Building wheels for collected packages: ansible
Building wheel for ansible (setup.py) ... done
Created wheel for ansible: filename=ansible-2.8.20-py2-none-any.whl size=12657061 sha256=243ce7f80e10e06ea29c384046b54f1ac7f8c512815693924066893eca853fa4
Stored in directory: /home/user/.cache/pip/wheels/0c/aa/59/692bf193d36a798bdca8b9cdc5c2f0f4bbf792bbcf650b73a9
Successfully built ansible
Installing collected packages: MarkupSafe, jinja2, PyYAML, ipaddress, six, enum34, pycparser, cffi, cryptography, ansible, jmespath
Successfully installed MarkupSafe-1.1.1 PyYAML-5.4.1 ansible-2.8.20 cffi-1.15.1 cryptography-3.3.2 enum34-1.1.10 ipaddress-1.0.23 jinja2-2.11.3 jmespath-0.10.0 pycparser-2.21 six-1.16.0
(test_ansible_2.8) $ pip list
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
Package Version
------------ -------
ansible 2.8.20
cffi 1.15.1
cryptography 3.3.2
enum34 1.1.10
ipaddress 1.0.23
Jinja2 2.11.3
jmespath 0.10.0
MarkupSafe 1.1.1
pip 20.3.4
pycparser 2.21
PyYAML 5.4.1
setuptools 44.1.1
six 1.16.0
wheel 0.37.1
(test_ansible_2.8) $ ansible --version
/home/user/.pyenv/versions/2.7.18/envs/test_ansible_2.8/lib/python2.7/site-packages/ansible/parsing/vault/__init__.py:44: CryptographyDeprecationWarning: Python 2 is no longer supported by the Python core team. Support for it is now deprecated in cryptography, and will be removed in the next release.
from cryptography.exceptions import InvalidSignature
ansible 2.8.20
config file = None
configured module search path = [u'/home/user/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /home/user/.pyenv/versions/2.7.18/envs/test_ansible_2.8/lib/python2.7/site-packages/ansible
executable location = /home/user/.pyenv/versions/test_ansible_2.8/bin/ansible
python version = 2.7.18 (default, Apr 5 2023, 15:02:31) [GCC 11.3.0]
(test_ansible_2.8) $ tree
.
└── playbook.yml
0 directories, 1 file
(test_ansible_2.8) $ cat playbook.yml
---
- hosts: localhost
gather_facts: false
vars:
results:
json:
list:
- status: "1"
hostname: "server-bbbv-10"
- status: "3"
hostname: "server-bbbv-14"
- status: "3"
hostname: "server-bbbv-17"
- status: "1"
hostname: "server-bbbv-11"
search_hostname: "server-bbbv-14"
tasks:
- name: Get the result with json_query
vars:
# Warning: single quotes matter here, see jmespath spec
# https://jmespath.org/specification.html#raw-string-literals
query_string: "[?hostname=='{{ search_hostname }}']"
ansible.builtin.debug:
msg: "{{ results.json.list | json_query(query_string) }}"
- name: Alternative syntax for json_query raw string
vars:
query_string: "[?hostname==`{{ search_hostname }}`]"
ansible.builtin.debug:
msg: "{{ results.json.list | json_query(query_string) }}"
- name: Get the same result with stock ansible filters
ansible.builtin.debug:
msg: "{{ results.json.list | selectattr('hostname', '==', search_hostname) | list }}"
(test_ansible_2.8) $ ansible-playbook playbook.yml
/home/user/.pyenv/versions/2.7.18/envs/test_ansible_2.8/lib/python2.7/site-packages/ansible/parsing/vault/__init__.py:44: CryptographyDeprecationWarning: Python 2 is no longer supported by the Python core team. Support for it is now deprecated in cryptography, and will be removed in the next release.
from cryptography.exceptions import InvalidSignature
[WARNING]: No inventory was parsed, only implicit localhost is available
PLAY [localhost] **************************************************************************************************************************************************************************************************
TASK [Get the result with json_query] *****************************************************************************************************************************************************************************
ok: [localhost] => {
"changed": false,
"msg": [
{
"hostname": "server-bbbv-14",
"status": "3"
}
]
}
TASK [Alternative syntax for json_query raw string] ***************************************************************************************************************************************************************
ok: [localhost] => {
"changed": false,
"msg": [
{
"hostname": "server-bbbv-14",
"status": "3"
}
]
}
TASK [Get the same result with stock ansible filters] *************************************************************************************************************************************************************
ok: [localhost] => {
"changed": false,
"msg": [
{
"hostname": "server-bbbv-14",
"status": "3"
}
]
}
PLAY RECAP ********************************************************************************************************************************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
(test_ansible_2.8) $ pyenv deactivate
$ pyenv virtualenv-delete test_ansible_2.8
pyenv-virtualenv: remove /home/user/.pyenv/versions/2.7.18/envs/test_ansible_2.8? (y/N) y
$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment