VGM
VGM

Reputation: 69

Python script to push text file commands over SSH

I am trying to find a solution (if is possible) to create a python script that pushes a configuration text file (batch of commands ~ 1000s of lines) over ssh.

So far, I have a solution to push few commands, but it doesn't apply when there are 1000s of lines.

Upvotes: 0

Views: 122

Answers (1)

Raoslaw Szamszur
Raoslaw Szamszur

Reputation: 1740

Have you heard about Ansible? It does the job you want to do, and it's written mostly in python.

App deployment, configuration management, and orchestration — all from one system. Ansible is simple, agentless automation that anyone can use.

Ansible Documentaion

Long story short you writte playboks (.yaml templates). Simply put, playbooks are the basis for a really simple configuration management and multi-machine deployment system, unlike any that already exist, and one that is very well suited to deploying complex applications.

Example playbook:

---
- hosts: webservers
  vars:
    http_port: 80
    max_clients: 200
  remote_user: root
  tasks:
  - name: ensure apache is at the latest version
    yum:
      name: httpd
      state: latest
  - name: write the apache config file
    template:
      src: /srv/httpd.j2
      dest: /etc/httpd.conf
    notify:
    - restart apache
  - name: ensure apache is running
    service:
      name: httpd
      state: started
  handlers:
    - name: restart apache
      service:
        name: httpd
        state: restarted

When you will create playbook that suits your needs just run it against your machine or list of them. There is a huge list of modules available to ansible ready to use. You can also write your own plugins.

Upvotes: 2

Related Questions