writing an python external_pillar (ext_pillar)

51 views
Skip to first unread message

Cédric Briner

unread,
Jul 26, 2016, 3:06:14 AM7/26/16
to Salt-users
os: Debian Jessie
version:
  salt-common 2015.5.3+ds-1~bpo8+1
  salt-master 2015.5.3+ds-1~bpo8+1

Hi,

I try to create my own external pillar. And to use it. But until now,
I've bee unable to get something working.

I have modified /etc/salt/master as follow:
```
extension_modules: /srv/salt/module

pillar_roots:
  base:
    - /srv/salt/pillar

ext_pillar:
  - my_pillar.py
```

Then I wrote a pillar script in python, that resides in
/srv/salt/module/my_pillar.py.
```
#!/usr/bin/python3

import logging

#### LOG STUFF
logger = logging.getLogger('pillar_ext')
logger.setLevel(logging.DEBUG)
# create console handler with a higher log level
fh = logging.FileHandler('/var/log/salt/pillar_test.log')
fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.ERROR)
# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(fh)
logger.addHandler(ch)
####


logger.info("File pillar.py loaded")
def ext_pillar(id, pillar, *args, **kv):
    logger.info("id: {}".format(id))
    logger.info("pillar: {}".format(pillar))
    logger.info("args: {}".format(args))
    logger.info("kv: {}".format(kv))
    return {"my_key_from_ext_pillar":"my_value_from_ext_pillar"}

if "__main__" == __name__:
    my_dict=ext_pillar("42", "a_pillar", "A", "B", C="C", D="D")
    logger.info("my_dict={}".format(my_dict))
```

I've also tested the script to see if it runs well. It does ! I can see in the log:
/var/log/salt/pillar_test.log
```
2016-07-26 09:02:00,645 - pillar_ext - INFO - File pillar.py loaded
2016-07-26 09:02:00,645 - pillar_ext - INFO - id: 42
2016-07-26 09:02:00,645 - pillar_ext - INFO - pillar: a_pillar
2016-07-26 09:02:00,645 - pillar_ext - INFO - args: ('A', 'B')
2016-07-26 09:02:00,645 - pillar_ext - INFO - kv: {'C': 'C', 'D': 'D'}
2016-07-26 09:02:00,646 - pillar_ext - INFO - my_dict={'my_key_from_ext_pillar': 'my_value_from_ext_pillar'}
```

Now my_pillar.py is the only file in /srv/salt/module directory:
```
ls -la /srv/salt/module/
drwxr-xr-x 8 root root 4096 Jul 25 08:15 .
drwxr-xr-x 6 root root 4096 Jul 22 10:50 ..
-rwxr-xr-x 1 root root 1014 Jul 25 13:44 /srv/salt/module/my_pillar.py
```

Then I restarted the salt-master wishing to see new pillar :
{'my_key_from_ext_pillar': 'my_value_from_ext_pillar'}

But the cmd: "salt a_node pillar.raw | grep my_" show me nothing.

What am I missing ?

Thanks in advance.

cED

Cédric Briner

unread,
Jul 26, 2016, 6:05:10 AM7/26/16
to Salt-users
Hello, I did it !

I'll come back later to tell you what I have actually changed.

Rene Jochum

unread,
Jul 26, 2016, 9:03:12 AM7/26/16
to salt-...@googlegroups.com
Hi,

i have the pleasure to announce that saltstack-lxd-formula(1) went in
public beta.

Its a combination of 3 saltstack technologies:

- An execution Module "lxd"
- 4 States "lxd", "lxd_profile", "lxd_image and "lxd_container"
- The formula

About LXD(2):

LXD is a container "hypervisor" and a new user experience for LXC.
LXD allows you to create images, profiles and containers over a REST API.

Its biggest features are:

- Secure by design (unprivileged containers, resource restrictions and
much more)
- Scalable (from containers on your laptop to thousand of compute nodes)
- Intuitive (simple, clear API and crisp command line experience)
- Image based (no more distribution templates, only good, trusted images)
- Live migration

This is what salstack-lxd-formula allows you to do:

- Initialize LXD with storage, authentication and network settings.
- Create some default settings for containers (profiles).
- Pull an image from various sources.
- Create a container with an image.
- Start/Stop/Restart/Freeze/Unfreeze/Migrate a container.
- And finally undo all of the above.

I hope some people will test it and give feedback,

Thanks,
René Jochum aka pcdummy


1.) https://github.com/pcdummy/saltstack-lxd-formula
2.) https://linuxcontainers.org/lxd/


Reply all
Reply to author
Forward
0 new messages