How Do You Leverage Ansible to Deploy Salt Minion to an Ansible Managed Node?

Problem scenario
You have two configuration management servers: one is an Ansible control server and another is a Salt Master server.  You want Ansible to deploy Salt Minion to the managed nodes.  You want the managed nodes of Ansible to receive configurations from the Salt Master server.  This way one team can use SaltStack and another team can use Ansible.  How do you do this?

Solution

Prerequisites
This assumes you have deployed Salt Master and Ansible.  (If you are running Linux SUSE and need to install Ansible, see this posting.)  This assumes that a third server is configured to be a managed node and receives configurations from the Ansible control server with playbook runs.

This solution assumes you have the same username with sudoer privileges on each managed node.  For the example below we use "cooluser".  This username needs to be able to use sudo without being prompted for a password.  If you need assistance with this, see this posting.

Procedures
1.  Get a file that will be the template and source of the /etc/salt/minion file for the Salt Minion client servers.  Place this copy on the Ansible control server.  (If you want more information about this file, see "How do you install and configure Salt..." step #7 of this posting.)

2.  Place this file where you have other source files on the Ansible control server (and you can name it sminstaller.txt or whatever you want):

distro=$(cat /etc/*-release | grep NAME)

debflag=$(echo $distro | grep -i "ubuntu")
if [ -z "$debflag" ]
then   # If it is not Ubuntu, test if it is Debian.
  debflag=$(echo $distro | grep -i "debian")
  echo "determining Linux distribution..."
else
   echo "You have Ubuntu Linux!"
fi

rhflag=$(echo $distro | grep -i "red*hat")
if [ -z "$rhflag" ]
then   #If it is not RedHat, see if it is CentOS or Fedora.
  rhflag=$(echo $distro | grep -i "centos")
  if [ -z "$rhflag" ]
    then    #If it is neither RedHat nor CentOS, see if it is Fedora.
    echo "It does not appear to be CentOS or RHEL..."
    rhflag=$(echo $distro | grep -i "fedora")
    fi
fi

if [ -z "$rhflag" ]
  then
  echo "...still determining Linux distribution..."
else
  echo "You have a RedHat distribution (e.g., CentOS, RHEL, or Fedora)"
  yum -y install salt-minion   # install nc for initial testing only.
fi

if [ -z "$debflag" ]
then
  echo "...still determining Linux distribution..."
else
   echo "You are using either Ubuntu Linux or Debian Linux."
   apt-get -y install salt-minion
fi

suseflag=$(echo $distro | grep -i "suse")
if [ -z "$suseflag" ]
then
  if [ -z "$debflag" ]
  then
    if [ -z "$rhflag" ]
      then
      echo "*******************************************"
      echo "Could not determine the Linux distribution!"
      echo "Installation aborted. Nothing was done."
      echo "******************************************"
      exit
    fi
  fi
else
   zypper -n install salt-minion
fi

3.  On the Ansible control server write an Ansible playbook (e.g., a .yaml file) to transfer the files in step #1 and step #2. This playbook will execute the file in step #2 (above) on the managed node after it is transferred.  Create a .yaml file (e.g., contint.yaml) with the following lines (excluding the last "#**" lines):

- name: something
  hosts: all
  remote_user: cooluser
  become: yes
  tasks:
  - file:
      path: /etc/salt
      state: directory
      owner: cooluser
      mode: 0744

- name: Transfer the script file down.
  hosts: all
  tasks:
  - copy:
      src: /home/cooluser/sminstaller.txt # File in step #2; rename if whatever
      dest: /tmp/smin.sh
      owner: cooluser
      mode: 0644

- name: Install Salt Minion on any distribution of Linux.
  hosts: all
  remote_user: cooluser
  become: yes
  tasks:
     - name: Run the installer
       command: bash /tmp/smin.sh
     - name: Run the salt minion service for the first time.
       command: salt-minion
       async: 10
       poll: 0

- name: Transfer the minion file down.
  hosts: all
  tasks:
  - copy:
      src: /home/cooluser/minion.txt # File in step #1.
      dest: /etc/salt/minion
      owner: cooluser
      mode: 0644

#** Change the "cooluser" to a username that has sudoer privileges.
#** Change the src file names and directory paths above to match your configuration.

4.  Run the playbook with this command:  ansible-playbook contint.yaml

5.a.  Go to the Salt Master server to configure the new Salt Minions to work with the Salt Master server itself.  Run this command:  sudo salt-key -L

5.b.  Run this command but substitute "<nameOfSaltMinion>" with the hostname of the Salt Minion client based on the results of the above command:  sudo salt-key -a <nameOfSaltMinion>

Leave a comment

Your email address will not be published. Required fields are marked *