Skip to content
Snippets Groups Projects
Commit 193b95f2 authored by nimrod's avatar nimrod
Browse files

- Better YAML formatting in ansible-python.

- Use code blocks instead of literal blocks.
parent 94ec9caa
No related branches found
No related tags found
No related merge requests found
...@@ -26,7 +26,9 @@ there's no restriction on the hierarchy depth. In your playbook directory create ...@@ -26,7 +26,9 @@ there's no restriction on the hierarchy depth. In your playbook directory create
a ``library`` directory (the Ansible default, so you can change this in a ``library`` directory (the Ansible default, so you can change this in
``ansible.cfg``) and create an empty ``__init__.py`` file inside that directory. ``ansible.cfg``) and create an empty ``__init__.py`` file inside that directory.
Add a git submodule inside that directory and you're done. Let's see an Add a git submodule inside that directory and you're done. Let's see an
example:: example
.. code:: shell
git init ansible-modules git init ansible-modules
cd ansible-modules cd ansible-modules
......
...@@ -7,13 +7,17 @@ Using Ansible as a Python module ...@@ -7,13 +7,17 @@ Using Ansible as a Python module
At my current employer we have several servers in production with various At my current employer we have several servers in production with various
providers, some of them with multiple ip addresses. When configuring the providers, some of them with multiple ip addresses. When configuring the
firewall to allow traffic from other servers I reached for Ansible. The firewall to allow traffic from other servers I reached for Ansible. The
obvious solution was to use a nested loop, something like this: :: obvious solution was to use a nested loop, something like this:
.. code:: yaml
- name: Allow other servers - name: Allow other servers
ufw: rule=allow from_ip={{ item[1] }} ufw:
rule: allow
from_ip: '{{ item[1] }}'
with_nested: with_nested:
- all_hosts - all_hosts
- {{ item.ansible_all_ipv4_addresses }} - '{{ item.ansible_all_ipv4_addresses }}'
However, this syntax is invalid (and other variations I tried). Using 'include' 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 with 'with_items' is deprecated and I didn't manage to get it to work with
...@@ -25,7 +29,9 @@ Incorperating Ansible in Python ...@@ -25,7 +29,9 @@ Incorperating Ansible in Python
------------------------------- -------------------------------
To retrieve all of the ip addresses I'd ran the setup module to gather the To retrieve all of the ip addresses I'd ran the setup module to gather the
information :: information
.. code:: python
from ansible.runner import Runner from ansible.runner import Runner
struct = Runner (module_name='setup', pattern='all_hosts').run() struct = Runner (module_name='setup', pattern='all_hosts').run()
...@@ -33,7 +39,9 @@ information :: ...@@ -33,7 +39,9 @@ information ::
Now we have a complex data structure that is the output of Ansible's fact 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 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 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. :: a list of all of our server's ip addresses.
.. code:: python
ipaddresses = [] ipaddresses = []
for host in struct['contacted']: for host in struct['contacted']:
...@@ -45,14 +53,18 @@ Putting that information to good use ...@@ -45,14 +53,18 @@ Putting that information to good use
Now that we have a list of the ip addresses, we can start running Ansible 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 commands right from with Python (just like we did) or build a playbook by
outputing a YAML file. I chose the latter. :: outputing a YAML file. I chose the latter.
.. code:: python
from yaml import safe_dump from yaml import safe_dump
doc = {'all_ipv4': ipaddresses} doc = {'all_ipv4': ipaddresses}
print (safe_dump (doc), file='vars.yml') print (safe_dump (doc), file='vars.yml')
This will create a vars.yml file with the all_ipv4 variable already defined 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: :: there to be imported to any playbook and run. For example:
.. code:: yaml
--- ---
- hosts: all_hosts - hosts: all_hosts
...@@ -60,8 +72,10 @@ there to be imported to any playbook and run. For example: :: ...@@ -60,8 +72,10 @@ there to be imported to any playbook and run. For example: ::
- vars.yml - vars.yml
tasks: tasks:
- name: Allow other servers - name: Allow other servers
ufw: rule=allow from_ip={{ item }}
with_items: all_ipv4 with_items: all_ipv4
ufw:
rule: allow
from_ip: '{{ item }}'
With this much little code we were able to query all of our hosts, extract the 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 needed information and output it back to Ansible for further use. I see this as
......
...@@ -12,13 +12,17 @@ It's a pythonic tool that's easy to use and was a breeze to setup. ...@@ -12,13 +12,17 @@ It's a pythonic tool that's easy to use and was a breeze to setup.
Installing Pelican Installing Pelican
------------------ ------------------
As Pelican is a static blog/ website generator, all we're doing is in your workstation. All you need to have server-wise is a bog-standard web server (like Apache or Nginx). Everything else is done on your local machine. I installed Pelican from Debian (it's currently available in testing) :: As Pelican is a static blog/ website generator, all we're doing is in your workstation. All you need to have server-wise is a bog-standard web server (like Apache or Nginx). Everything else is done on your local machine. I installed Pelican from Debian (it's currently available in testing)
$ apt-get install python-pelican fabric .. code:: shell
Alternatively, you can use pip :: apt-get install python-pelican fabric
$ pip install pelican fabric Alternatively, you can use pip
.. code:: shell
pip install pelican fabric
Creating a blog Creating a blog
--------------- ---------------
...@@ -81,9 +85,11 @@ I've set the timezone to mine (so that the time of published articles is correct ...@@ -81,9 +85,11 @@ I've set the timezone to mine (so that the time of published articles is correct
Themes Themes
------ ------
Pelican comes with a default theme (the same as used by Pelican's website) but I wanted something more understated so I took at look at `https://github.com/getpelican/pelican-themes <https://github.com/getpelican/pelican-themes>`_ and chose pelican-mockingbird. Cloned it :: Pelican comes with a default theme (the same as used by Pelican's website) but I wanted something more understated so I took at look at `https://github.com/getpelican/pelican-themes <https://github.com/getpelican/pelican-themes>`_ and chose pelican-mockingbird. Cloned it
$ git clone https://github.com/wrl/pelican-mockingbird.git .. code:: shell
git clone https://github.com/wrl/pelican-mockingbird.git
and set the theme to that by adding the following to pelicanconf.py: :: and set the theme to that by adding the following to pelicanconf.py: ::
...@@ -101,9 +107,11 @@ Create a ReStructedText file inside of contents. The filename is for personal us ...@@ -101,9 +107,11 @@ Create a ReStructedText file inside of contents. The filename is for personal us
:author: <Insert your name here> :author: <Insert your name here>
:summary: <Insert summary here> :summary: <Insert summary here>
After we added the content we want to upload it to our web server (I use fabric) :: After we added the content we want to upload it to our web server (I use fabric)
.. code:: shell
$ fab publish fab publish
If you don't have keys set for the server it will ask you for your password to the server. If you don't have keys set for the server it will ask you for your password to the server.
Last thing, you can create pages, create a pages directory inside contents and save the files there. Their format is the same as articles but they'll have a somewhat template applied and they will be shown in the menu. A good example will an 'About Me' page. Last thing, you can create pages, create a pages directory inside contents and save the files there. Their format is the same as articles but they'll have a somewhat template applied and they will be shown in the menu. A good example will an 'About Me' page.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment