I'm trying to register my minion in a central hosts file and then push this hosts file to all my connected minions.
Here is what I have in mind:
Minion send an event 'register' with its ip and hostname to be registered on the master's central hosts file
Master listening to the event 'register' and react with the reactor /srv/reactor/register.sls
Reactor calls the state /srv/salt/register.sls on the minion installed on the master's host to modify the central file and send an event 'hosts_modified' after the modification is complete
Master listening to the event 'hosts_modified' and react with the reactor /srv/reactor/deploy_hosts.sls which applies the state /srv/salt/hosts.sls on all connected minions to push the new modified hosts file
The first 3 steps are working fine but the master is not reacting to the last event 'hosts_modified'.
Command to initiate the register on the minion:
salt-call event.send register minion_host=somehostname minion_ip=1.1.1.1
Master reactor config (/etc/salt/master.d/reactor.conf):
reactor:
- salt/beacon/*/inotify//etc/hosts:
- /srv/reactor/revert.sls
- 'deployment':
- /srv/reactor/deployment.sls
- 'register':
- /srv/reactor/register.sls
- 'hosts_modified':
- /srv/reactor/deploy_hosts.sls
/srv/reactor/register.sls
{% set forwarded_data = data.data %}
test:
local.state.sls:
- tgt: 'master'
- args:
- mods: register
- pillar:
forwarded_data: {{ forwarded_data | json() }}
/srv/salt/register.sls
{% set data = salt.pillar.get('forwarded_data') %}
add_host:
cmd.run:
- name: /srv/scripts/hosts-manage.sh {{ data.minion_ip }} {{ data.minion_host }}
event_host_modified:
event.send:
- name: hosts_modified
- require:
- cmd: add_host
/srv/reactor/deploy_hosts.sls
deploy_hosts:
local.state.sls:
- tgt: '*'
- name: hosts
/srv/salt/hosts.sls
# Hosts file management
/etc/hosts:
file.managed:
- source: salt://repo/conf/hosts
Am I doing it wrong?
Is it not possible to handle events sent while applying states?
EDIT
I finally did it with an Orchestrate Runner.
/srv/reactor/register.sls:
{% set forwarded_data = data.data %}
register:
runner.state.orch:
- args:
- mods: orch.register
- pillar:
forwarded_data: {{ forwarded_data | json() }}
/srv/salt/orch/register.sls:
{% set data = salt.pillar.get('forwarded_data') %}
add_host:
cmd.run:
- name: /srv/scripts/hosts-manage.sh {{ data.minion_ip }} {{ data.minion_host }}
- stateful: True
refresh hosts on minions:
salt.state:
- tgt: '*'
- sls: hosts
- watch:
- cmd: add_host
/srv/salt/hosts.sls:
# Hosts file management
/etc/hosts:
file.managed:
- source: salt://repo/conf/hosts
It seems to be working this way.
I finally did it with an Orchestrate Runner.
/srv/reactor/register.sls:
{% set forwarded_data = data.data %}
register:
runner.state.orch:
- args:
- mods: orch.register
- pillar:
forwarded_data: {{ forwarded_data | json() }}
/srv/salt/orch/register.sls:
{% set data = salt.pillar.get('forwarded_data') %}
add_host:
cmd.run:
- name: /srv/scripts/hosts-manage.sh {{ data.minion_ip }} {{ data.minion_host }}
- stateful: True
refresh hosts on minions:
salt.state:
- tgt: '*'
- sls: hosts
- watch:
- cmd: add_host
/srv/salt/hosts.sls:
# Hosts file management
/etc/hosts:
file.managed:
- source: salt://repo/conf/hosts
It seems to be working this way.
Related
Context: I am noob to salt stack open and working on evaluating as an option for state management. I am getting my hands on it. Currently I am running into not been able to run my mods.sls file.
Environment: Three AWS linux systems (t2) One master and two nodes/minions.
/srv/salt/apache/ contains init.sls, map.sls, mods.sls, and welcome.sls.
# mods.sls
{% for conf in ['status', 'info'] %}
mod_{{ conf }}:
file.managed:
- name: /etc/apache2/conf-available/mod_{{ conf }}.conf
- contents: |
<Location "/{{ conf }}">
SetHandler server-{{ conf }}
</Location>
{% if salt.grains.get('os_family') == 'Debian' %}
cmd.run:
- name: a2enmod {{ conf }} && a2enconf mod_{{ conf }}
- creates: /etc/apache2/conf-enabled/mod_{{ conf }}.conf
{% endif %}
{% endfor %}
# salt '*' state.show_sls mods`
node2:
- No matching sls found for 'mods' in env 'base'
node:
- No matching sls found for 'mods' in env 'base'
ERROR: Minions returned with non-zero exit code
Any advice from the community would be appreciated or anything I can do to make the question easier to understand.
The sls path is apache.mods, not mods.
How do I use requisites in reactor files?
I only want to run the second state if the first completes successfully.
{% if ('id' in data) and ('act' in data) and ('pub' in data) and
((data['act'] == 'pend') and (data['pub'] != '')) %}
check_minion_domain_joined:
local.cmd.run:
- tgt: 'MINIONNAME'
- arg:
- powershell.exe -ExecutionPolicy Bypass -File "C:/salt/auth-minion.ps1" {{ data['id'] }}
accept_key_domain_joined:
wheel.key.accept:
- match: {{ data['id'] }}
- require:
- cmd: check_minion_domain_joined
{% endif %}
I can see that the first state is being run by the master via the output of salt-run state.event, but "accept_key_domain_joined" never runs.
As per the official documentation, reactor SLS files do not support requisites.
Reactor SLS files, by design, do not support requisites, ordering, onlyif/unless conditionals and most other powerful constructs from Salt's State system.
The better option is to trigger orchestration from the reactor. So instead of having the two actions performed from reactor, just call for an orchestration run:
Example reactor/file.sls:
create-file-with-content:
runner.state.orch:
- args:
- mods: orch.create_file
- pillar:
min_id: {{ data['id'] }}
min_act: {{ data['act'] }}
Then in orch/create_file.sls, we can use the pillar data and accomplish the tasks.
Example:
{% set minion_id = salt.pillar.get('min_id') %}
{% set minion_act = salt.pillar.get('min_act') %}
touch-a-tmp-file:
salt.function:
- name: file.touch
- tgt: {{ minion_id }}
- arg:
- /tmp/dummy.file
update-tmp-file:
salt.function:
- name: file.append
tgt: {{ minion_id }}
arg:
- /tmp/dummy.file
- minion_act is {{ minion_act }}
- require:
- salt: touch-a-tmp-file
There is some more information on troubleshooting pillar information in reactor-orchestration states here.
In Ansible I've used register to save the results of a task in the variable services.
It has this structure:
"stdout_lines": [
"arp-ethers.service \u001b[1;31mdisabled\u001b[0m",
"auditd.service \u001b[1;32menabled \u001b[0m",
"autovt#.service \u001b[1;31mdisabled\u001b[0m",
"blk-availability.service \u001b[1;31mdisabled\u001b[0m"]
and I would like to receive this:
{
"arp-ethers.service": "disabled",
"auditd.service": "enabled",
"autovt#.service": "disabled",
"blk-availability.service":"disabled"
}
I'd like to use a subsequent set_fact task to generate a new variable with a dictionary, but I'm going round in circles with no luck so far.
- name: Collect all services for SYSTEMD
raw: systemctl list-unit-files --type=service --no-pager -l --no-legend`
register: services
changed_when: false
- debug:
var: services
- debug:
msg: "{{ item.split()[0]|to_json }} : {{ item.split()[1]|to_json }}"
with_items:
- "{{ services.stdout_lines }}"
- name: Populate fact list_services for SYSTEMD
set_fact:
cacheable: yes
list_services: "{{ list_services|default({}) | combine ( {item.split()[0]|to_json: item.split()[1]|to_json} ) }}"
with_items: "{{ services.stdout_lines }}"
This return :
FAILED! => {"msg": "|combine expects dictionaries, got u'arp-ethers.service \\x1b[1;31mdisabled\\x1b[0m\\r\\nauditd.service \\x1b[1;32menabled \\x1b[0m\\r\\nautovt#.service \\x1b[1;31mdisabled\\x1b[0m\\r\\nblk-availability.service \\x1b[1;31mdisabled\\x1b[0m\\r\\n'"}
What you want is to switch list-unit-files into json output using --output=json (yes, that's a link to the journalctl man page, because the systemctl one links there)
roughly like this, although I didn't test it:
- name: Collect all services for SYSTEMD
raw: systemctl --output=json list-unit-files --type=service
register: services_json
changed_when: false
- set_fact:
services: '{{ services_json.stdout | from_json }}'
Use service_facts. For example
- service_facts:
- set_fact:
dict_services: "{{ dict(ansible_facts.services|
dict2items|
json_query('[].[key, value.status]')) }}"
I try to create the following state, but I don't know how to write the if clause? Maybe someone can help me with it. What I try to accomplish is that salt takes a configuration file if a file with the target hostname exists and else take the default config.
example:
{% if ??? test -f ??? salt://ntpd/ntp.conf_{{ salt['grains.get']('host') }} %}
ntpd-config:
file.managed:
- name: /etc/ntp.conf
- source: salt://ntpd/ntp.conf_{{ salt['grains.get']('host') }}
- user: root
- group: root
- file_mode: 644
- require:
- ntpd-pkgs
{% else %}
ntpd-config:
file.managed:
- name: /etc/ntp.conf
- source: salt://ntpd/ntp.conf
- user: root
- group: root
- file_mode: 644
- require:
- ntpd-pkgs
{% endif %}
Hope, someone could help me.
Thanks in advance!
Matthias
I just found the answer by myself.
Found out in the documentation that I can define multiple sources. The last one is then the default one if none of the others bevore exists.
This now works:
ntpd-config:
file.managed:
- name: /etc/ntp.conf
- source:
- salt://ntpd/ntp.conf_{{ salt['grains.get']('host') }}
- salt://ntpd/ntp.conf
I have set up pillar data for websites, e.g. web_root, virtualhost and mysql:
web_root:
config_file: salt://some/path.conf
key: some data
directory_name: directoryA
virtualhost:
config_file: salt://some/path.conf
name: websiteA
mysql:
database:
- websiteA_db
These map to states for web_root, virtualhost and mysql (using formula).
I'd like to use have a minion run these states multiple times, using separate pillar data, e.g.
include:
- apache
- php
{% for instance in [instanceA, instanceB] -%}
{% load pillar data /pillar/{{ instance }} -%}
- web_root #run the state
- virtualhost #run the state
- mysql #run the state
{% endfor -%}
Is this possible? I know I can set up pillar data like so:
web_root:
instanceA:
config_file: salt://some/pathA.conf
key: some data
directory_name: directoryA
instanceB:
config_file: salt://some/pathB.conf
key: some data
directory_name: directoryB
virtualhost:
instanceA:
config_file: salt://some/pathA.conf
name: websiteA
instanceB:
config_file: salt://some/pathB.conf
name: websiteB
mysql:
database:
- websiteA_db
- websiteB_db
But it means I have to add loops to each state file, making it less readable as well as use different syntax, e.g. for mysql which is a formula with set syntax requirements.
You'll want to do something like this:
Pillar Data
web_root:
instances:
A:
- name: A
- key: key_A_data
B:
- name: B
- key: key_B_data
State file
{% set names = salt['pillar.get']('web_root:instances') %}
apache:
pkg.installed: []
{% for name in names %}
instance{{ name }}:
- config_file: salt://some/path{{ name }}.conf
- key: {{ key }}
- directory_name: directory{{ name }}
{% endfor %}
Then just do the same thing for the rest of your objects. This way you don't have to change your state file when you add objects to the pillar.