Showing posts with label Salt command. Show all posts

Wednesday, October 6, 2021

thumbnail

saltstack command for installing packages

Saltstack command for installing a package on the remote server or salt-minion the client



1. The following command is to install, activate and start the qualys-cloud-agent on RedHat and CentOS

# vi qualys-cloud-agent-redhat.sls 
---

{% if grains['os_family'] == "RedHat" -%}

install qualys-cloud-agent:
  pkg.installed:
    - sources:
      - qualys-cloud-agent: salt://_files//qualys-cloud-agent.x86_64.rpm

activate qualys-cloud-agent:
  module.run:
    - name: cmd.run
    - cmd: "/usr/local/qualys/cloud-agent/bin/qualys-cloud-agent.sh ActivationId=c4171172-7e24-47d8-a995-4364923c3b54 CustomerId=aac8d569-c20c-3a9f-e040-10ac130471e6"

enable qualys-cloud-agent service:
  service.running:
    - name: qualys-cloud-agent
    - enable: True
    - reload: True

{% endif %}

2. The following command is to install, activate and start the qualys-cloud-agent on RedHat and CentOS

# vi qualys-cloud-agent-debian.sls 
---

{% if grains['os_family'] == "Debian" -%}

install qualys-cloud-agent:
  pkg.installed:
    - sources:
      - qualys-cloud-agent: salt://_files//qualys-cloud-agent.x86_64.deb

activate qualys-cloud-agent:
  module.run:
    - name: cmd.run
    - cmd: "/usr/local/qualys/cloud-agent/bin/qualys-cloud-agent.sh ActivationId=c4171172-7e24-47d8-a995-4364923c3b54 CustomerId=aac8d569-c20c-3a9f-e040-10ac130471e6"

enable qualys-cloud-agent service:
  service.running:
    - name: qualys-cloud-agent
    - enable: True
    - reload: True

{% endif %}

3. To push / install the package to the single host use the following command and for group of systems you can use nodegroup name so push it.

# salt dev-vm1.acg.com state.apply qualys-cloud-agent-debian

4. Use the following command to create sshusers list and this group will be pushed to /etc/group on the client and only this sshusers will have access to ssh and rest of them will be blocked

# vi sshusers.sls
---
sshusers admin access:
  group.present:
    - name: sshusers
    - members:
      - root
      - dev_user1
      - dev_user2
     

5. Use the following state file to give sudo access to the list of users mentioned in the server_admins list under below mentioned path.

# vi sudoers.sls
setup server_admins sudoers access:
  file.managed:
    - name: /etc/sudoers.d/server_admins
    - source: salt://_files/sudoers/server_admins
    - user: root
    - group: root
    - mode: 440

6. Use the following state file to mount file systems per auto.master configuration in the specified server

# vi automount.sls
---
# RHEL5 & RHEL6
{% if salt['my_helpers.occurrences']('ldap', '/etc/auto.master') > 0 %}
Remove all brocade data:
  file.absent:
    - name: /etc/auto.master
{% endif %}

## RHEL7
{% if salt['my_helpers.occurrences']('sss', '/etc/auto.master') > 0 %}
Remove all brocade data:
  file.absent:
    - name: /etc/auto.master
{% endif %}

copy /etc/auto.master:
  file.managed:
    - name: /etc/auto.master
    - source: salt://_files/asic/_etc_auto.master

reload autofs daemon:
  service.running:
    - name: autofs
    - enable: True
    - reload: True
    - watch:
      - file: /etc/auto.master

7. To restrict other than sshusers for ssh use the following state file

# vi sshd-server.sls
---

include:
  - nisclient

{% if grains['os_family'] == "RedHat" -%}
{% if grains['osmajorrelease'] == 5  %}
{% if grains['osarch'] == "x86_64"  %}

/usr/local/sbin/sshd:
  file.managed:
  - source: salt://_files/el5/_usr_local_sbin_sshd
  - user: root
  - group: root
  - mode: 755

/etc/sysconfig/sshd:
  file.managed:
  - source: salt://_files/el5/_etc_sysconfig_sshd
  - user: root
  - group: root
  - mode: 644

{% if salt['file.file_exists']('/usr/local/sbin/sshd') -%}

/etc/init.d/sshd:
  file.replace:
    - name: /etc/init.d/sshd
    - pattern: SSHD=.*
    - repl: SSHD=/usr/local/sbin/sshd
    - append_if_not_found: True
    - backup: master
	service.running:
    - name: sshd
    - watch:
      - file: /etc/init.d/sshd

{% endif %}
{% endif %}

{% endif %}
{% endif %}

Disable GSSAPIAuthentication:
  file.line:
    - name: /etc/ssh/sshd_config
    - match: 'GSSAPIAuthentication yes'
    - mode: delete

Disable GSSAPICleanupCredentials:
  file.line:
    - name: /etc/ssh/sshd_config
    - match: 'GSSAPICleanupCredentials yes'
    - mode: delete

Enable UseDNS:
  file.replace:
    - name: /etc/ssh/sshd_config
    - pattern: ^#UseDNS .*
    - repl: UseDNS no
    - append_if_not_found: True
	- backup: master

Set UseDNS to "no":
  file.replace:
    - name: /etc/ssh/sshd_config
    - pattern: UseDNS .*
    - repl: UseDNS no
    - append_if_not_found: True
    - backup: master
  service.running:
    - name: sshd
    - watch:
      - file: /etc/ssh/sshd_config

sshusers group access:
  group.present:
    - name: sshusers
    - gid: 1000
    - system: True
    - addusers:
      - root

Copy nologin script:
  file.managed:
    - name: /opt/script/nologin
    - source: salt://_files/_nologin
    - mode: 755
    - makedirs: True
	/etc/ssh/sshd_config:
  file.append:
    - name: /etc/ssh/sshd_config
    - text: |

        # Allow access to sshusers group
        Match Group *,!sshusers
            ForceCommand /opt/script/nologin

8. Use the following script to force users not login for everyone other than mentioned Match group

# nologin
#!/bin/sh
echo -ne "\e[31m\e[1m"
cat << EOF
####################################################
#                                                  #
# You are not authorized to log on to this machine #
#                                                  #
####################################################
EOF
echo -ne "\e[0m"

9. Use the following command for sudoers file

# vi ~/_file/sudoers/server_admins
#
# server_admins sudoers file
#
User_Alias DEV=dev_user1,dev_user2,dev_user3
DEV        ALL=(ALL) NOPASSWD: ALL
qa_user1   ALL = (root) ALL

10. To get full inventory of a host

# salt nc-efabuild-01.extremenetworks.com grains.items
# salt nc-efabuild-01.extremenetworks.com grains.items os_faimily


thumbnail

Saltstack server side commands

Saltstack configuration Management command from salt-master 



1. switch to saltadmin and perform salt configuration management changes.

2. state files contains all the configuration file that push to the salt-minion. refer the following command to get that details.

#  pwd
/srv/salt
# ls
etc  _files  _modules  _pillar  RCS  _states  var

3. to create node group then use the following command

# cd /srv/salt/etc/master.d
# vi devservers.conf
---
#
# List of Dev Servers

nodegroups:
  devsrv:
    - dev-vm1.acg.com
    - dev-vm2.acg.com

4. To push any state files to the note group you can use the following command. you need to have the sshusers file under ~/_states folder to push that to the dev servers

# salt -N <node-group-name> state.apply sshusers
# salt -N devsrv state.apply sshusers

5. To check reachability of all the servers in the node group you can use the following command

# salt -N devsrv test.ping

6. To check the ping for single host you can use the following command

# salt dev-vm1.acg.com test.ping
# salt dev-vm1* state.highstate
# salt dev-vm1* state.apply devservers.sshusers (here devservers is the folder under_states and then sshusers exist there)

7. top.sls is the file which all the clients talks to and overwrite the configuration per that. so make sure if you make any changes update that.

# cd /srv/salt/_states/top.sls
---

base:
  # All minions get the following three state files applied
  '*':
    - dnsclient
    - saltstack-patch
    - motd
    # - ntpd
    # - smtp
    - salt-agent
    # - check_mk
    # (not required) - services
    # - post-grains

  devsrv:
    - match: nodegroup
    - nisclient
    - pam
    - root_crontab
    - sshd-server
    - devsrv-sshusers
    - devsrv-automount
- salt-minion
(similarly add for other nodegroups)

8. If you have two salt-master (meaning master and slave) then if you make any update to states and files then to sync that to the another salt-master (or slave) use the following command

# sync-states
# sync-files



thumbnail

How to re-register salt-key on salt-master

Re-register salt-key on salt-master when you are having issue for the key to be registered in salt-master



Perform the following steps on the salt-minion side to fix the hostname issue on the salt-master.

1. Update the /etc/hosts file with the correct hostname that you want to be

2. Then on salt-minion stop salt-minion service

# systemctl stop salt-minion
OR
# service salt-minion stop

3. Delete the salt-minion keys. you need to find the salt-minion key location and then remote minion.pem and minion.pub keys.

# rm -rf /opt/salt/etc/pki/minion/minion.p*
/opt/salt/etc/pki/minion
    - minion.pem
    - minion.pub

Perform the following steps on the salt-master side to fix the hostname issue

1. Delete the salt-key for the respective server

# salt-key -d acg-vm1.acg.com

2. After that restart the salt-minion service on the client side.

# systemctl restart salt-minion

3. Then you can verify on salt-master with the following command to find if the key is registered.

# salt-key -L | grep acg-vm1

4. once the key shows in salt-master then you should be able to push the configuration changes to the client which is salt-minion

5. To verify the salt-minion service on the client side use the following command

# systemctl status salt-minion
# service salt-minion status
Redirecting to /bin/systemctl status salt-minion.service
\u25cf salt-minion.service - The Salt Minion
   Loaded: loaded (/usr/lib/systemd/system/salt-minion.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2021-08-06 23:35:55 EDT; 1 months 29 days ago
     Docs: man:salt-minion(1)
           file:///usr/share/doc/salt/html/contents.html
           https://docs.saltstack.com/en/latest/contents.html
 Main PID: 1008 (python2.7)
   CGroup: /system.slice/salt-minion.service
           \u251c\u25001008 /opt/salt/bin/python2.7 /opt/salt/bin/salt-minion --config-dir=/opt/salt/etc
           \u251c\u25001769 /opt/salt/bin/python2.7 /opt/salt/bin/salt-minion --config-dir=/opt/salt/etc
           \u2514\u25001776 /opt/salt/bin/python2.7 /opt/salt/bin/salt-minion --config-dir=/opt/salt/etc

Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.


thumbnail

How to delete salt-key from salt master

If you working on cleaning up the retired or decommissioned server or if you are deploying new VM with same name then you might want to cleanup the salt-key for those servers. so for that you need to login to the salt-master and then switch to saltadmin and then perform the following steps.



1. Switch to saltadmin using the following command

# sudo su - saltadmin

2. To delete the salt-key for the respective host use the following command

# salt-key -d <FQDN hostname?
# salt-key -d acg-vm1.acg.com

3. To check the salt version use the following command

# salt --versions-report
Salt Version:
           Salt: 2017.7.2
 
Dependency Versions:
           cffi: 1.11.2
       cherrypy: Not Installed
       dateutil: Not Installed
      docker-py: Not Installed
          gitdb: 2.0.3
      gitpython: 2.1.7
          ioflo: Not Installed
         Jinja2: 2.10
        libgit2: Not Installed
        libnacl: Not Installed
       M2Crypto: Not Installed
           Mako: Not Installed
   msgpack-pure: Not Installed
 msgpack-python: 0.4.8
   mysql-python: Not Installed
      pycparser: 2.18
       pycrypto: 2.6.1
   pycryptodome: Not Installed
         pygit2: Not Installed
         Python: 2.7.14 (default, Dec  4 2017, 20:10:15)
   python-gnupg: Not Installed
         PyYAML: 3.12
          PyZMQ: 16.0.3
           RAET: Not Installed
          smmap: 2.0.3
        timelib: Not Installed
        Tornado: 4.5.2
            ZMQ: 4.1.6
 
System Versions:
           dist: centos 7.8.2003 Core
         locale: UTF-8
        machine: x86_64
        release: 3.10.0-1127.8.2.el7.x86_64
         system: Linux
        version: CentOS Linux 7.8.2003 Core


4. To list all the salt-minion keys registered with salt-master you can run the following command.

# salt-key -L


Powered by Blogger.