Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
B
blog
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
nimrod
blog
Commits
29a10962
Commit
29a10962
authored
10 years ago
by
nimrod
Browse files
Options
Downloads
Patches
Plain Diff
- Add new entry (Ansible module in Python).
parent
df4a9b2d
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
content/ansible-python.rst
+70
-0
70 additions, 0 deletions
content/ansible-python.rst
with
70 additions
and
0 deletions
content/ansible-python.rst
0 → 100644
+
70
−
0
View file @
29a10962
Using Ansible as a Python module
================================
:date: 2015-01-01
:summary: Using Ansible as a Python module when playbooks are not enough.
At my current employer we have several servers in production with various
providers, some of them with multiple ip addresses. When configuring the
firewall to allow traffic from other servers I reached for Ansible. The
obvious solution was to use a nested loop, something like this: ::
- name: Allow other servers
ufw: rule=allow from_ip={{ item[1] }}
with_nested:
- all_hosts
- {{ item.ansible_all_ipv4_addresses }}
However, this syntax is invalid (and other variations I tried). Using 'include'
with 'with_items' is deprecated and I didn't manage to get it to work with
registering variables as well. What I had left was programaticaly generating
a playbook, but investigating further I found that Ansible can be imported as
a Python module.
Incorperating Ansible in Python
-------------------------------
To retrieve all of the ip addresses I'd ran the setup module to gather the
information ::
from ansible.runner import Runner
struct = Runner (module_name='setup', pattern='all_hosts').run()
Now we have a complex data structure that is the output of Ansible's fact
gathering module. Running it in the interpeter and examining the structure is
not hard at all and that is how I managed to write the following code to extract
a list of all of our server's ip addresses. ::
ipaddresses = []
for host in struct['contacted']:
for ip in struct['contacted'][host]['ansible_facts']['ansible_all_ipv4_addresses']:
ipaddresses.append (ip)
Putting that information to good use
------------------------------------
Now that we have a list of the ip addresses, we can start running Ansible
commands right from with Python (just like we did) or build a playbook by
outputing a YAML file. I chose the latter. ::
from yaml import safe_dump
doc = {'all_ipv4': ipaddresses}
print (safe_dump (doc), file='vars.yml')
This will create a vars.yml file with the all_ipv4 variable already defined
there to be imported to any playbook and run. For example: ::
---
- hosts: all_hosts
vars_files:
- vars.yml
tasks:
- name: Allow other servers
ufw: rule=allow from_ip={{ item }}
with_items: all_ipv4
With this much little code we were able to query all of our hosts, extract the
needed information and output it back to Ansible for further use. I see this as
a product of the good decisions the Ansible developers choose early on (YAML,
Python, SSH). As always, for any feedback you may have, `email me <mailto:
nimrod@shore.co.il>`_.
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment