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
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -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
``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
example::
example

.. code:: shell

    git init ansible-modules
    cd ansible-modules
+22 −8
Original line number Diff line number Diff line
@@ -7,13 +7,17 @@ Using Ansible as a Python module
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: ::
obvious solution was to use a nested loop, something like this:

.. code:: yaml

    - name: Allow other servers
      ufw: rule=allow from_ip={{ item[1] }}
      ufw:
        rule: allow
        from_ip: '{{ item[1] }}'
      with_nested:
        - all_hosts
        - {{ item.ansible_all_ipv4_addresses }}
        - '{{ 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
@@ -25,7 +29,9 @@ Incorperating Ansible in Python
-------------------------------

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
    struct = Runner (module_name='setup', pattern='all_hosts').run()
@@ -33,7 +39,9 @@ information ::
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. ::
a list of all of our server's ip addresses.

.. code:: python

    ipaddresses = []
    for host in struct['contacted']:
@@ -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
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
    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: ::
there to be imported to any playbook and run. For example:

.. code:: yaml

    ---
    - hosts: all_hosts
@@ -60,8 +72,10 @@ there to be imported to any playbook and run. For example: ::
        - vars.yml
      tasks:
        - name: Allow other servers
          ufw: rule=allow from_ip={{ item }}
          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
needed information and output it back to Ansible for further use. I see this as
+25 −17
Original line number Diff line number Diff line
@@ -12,13 +12,17 @@ It's a pythonic tool that's easy to use and was a breeze to setup.

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
---------------
@@ -81,9 +85,11 @@ I've set the timezone to mine (so that the time of published articles is correct

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: ::

@@ -101,9 +107,11 @@ Create a ReStructedText file inside of contents. The filename is for personal us
    :author: <Insert your name 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.
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.