Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for windows control machine using cygwin. #34

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

mitchd
Copy link

@mitchd mitchd commented Jun 7, 2016

I had to make some changes to node-ansible to be compatible with our windows control machine (we're stuck with Windows for reasons beyond this scope...) I thought this might be useful for other users.

The ansible.js module detects a windows platform with:
var isWin = /^win/.test(process.platform)

This var is used to decide whether to substitute cygwinCommand in place of 'ansible'. Additionally, support is added for specifying an ansiblePath to point to an alternate location.

Ansible is invoked in cygwin with a structure like:

console.log('Node-Ansbile Test');

var Ansible = require('node-ansible');
var command = new Ansible.AdHoc()
                        .hosts('cisco')
                        .module('ping')
                        .cygwinCommand('c:/cygwin64/bin/bash.exe')
                        .ansiblePath('/usr/bin/');
var promise = command.exec();
promise.then(function(result) {
        console.log('Got good output');
        console.log(result.output);
        console.log(result.code);
}, function(err){
        console.log('Got Error');
        console.error(err);
})

This is equivalent to running the following command on a windows commandline instance:
c:\cygwin64\bin\bash.exe -c "/usr/bin/ansible cisco -m ping"

The path to ansible must be explicit in the bash -c call.

@mitchd
Copy link
Author

mitchd commented Jun 9, 2016

This was a little premature it seems. Playbook functionality isn't quite where it needs to be. Adhoc seems to be OK, though.

PS C:\TFGROOT\Temp\NodeTest> node .\test.js
Node-Ansbile Test
Got good output
Using /etc/ansible/ansible.cfg as config file
Loaded callback minimal of type stdout, v2.0
Using module file /usr/lib/python2.7/site-packages/ansible-2.2.0-py2.7.egg/ansible/modules/core/system/ping.py
<192.168.0.119> ESTABLISH CONNECTION FOR USER: mitchd on PORT 22 TO 192.168.0.119
<192.168.0.119> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1465500524.3-668112364718
14 `" && echo ansible-tmp-1465500524.3-66811236471814="` echo $HOME/.ansible/tmp/ansible-tmp-1465500524.3-66811236471814
 `" ) && sleep 0'
<192.168.0.119> PUT /cygdrive/c/Users/MITCH~1.DAV/AppData/Local/Temp/tmpZiKTyU TO /home/mitchd/.ansible/tmp/ansible-tmp-
1465500524.3-66811236471814/ping.py
<192.168.0.119> EXEC /bin/sh -c 'chmod -R u+x /home/mitchd/.ansible/tmp/ansible-tmp-1465500524.3-66811236471814/ && slee
p 0'
<192.168.0.119> EXEC /bin/sh -c 'LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /home/mitch
d/.ansible/tmp/ansible-tmp-1465500524.3-66811236471814/ping.py; rm -rf "/home/mitchd/.ansible/tmp/ansible-tmp-1465500524
.3-66811236471814/" > /dev/null 2>&1 && sleep 0'
192.168.0.119 | SUCCESS => {
    "changed": false,
    "invocation": {
        "module_args": {
            "data": null
        },
        "module_name": "ping"
    },
    "ping": "pong"
}

0
PS C:\TFGROOT\Temp\NodeTest>

@mitchd
Copy link
Author

mitchd commented Jun 10, 2016

Now with a playbook such as:

---
- hosts: cisco
  gather_facts: no
  connection: local
  tasks:

  - debug: msg="Testing Playbook Output"

  - name: RUN 'SHOW VERSION'
    ios_command:
      host: 192.168.168.1
      username: cisco
      password: testPW1
      commands:
        - show version
        - show ip interface brief
    register: version

  - debug: var=version.stdout_lines

And:


console.log('Node-Ansbile Test');

var Ansible = require('node-ansible');
var command = new Ansible.Playbook()
                        .cygwinCommand('c:/cygwin64/bin/bash.exe')
                        .ansiblePath('/usr/bin/')
                        .playbook('/home/Mitch.Davis/main');
var promise = command.exec();
promise.then(function(result) {
        console.log('Got good output');
        console.log(result.output);
        console.log(result.code);
}, function(err){
        console.log('Got Error');
        console.error(err);
})

Yields:

PS C:\TFGROOT\Temp\NodeTest> node .\test.js
Node-Ansbile Test
Got good output

PLAY [cisco] *******************************************************************

TASK [debug] *******************************************************************
ok: [intrlink] => {
    "msg": "Testing Playbook Output"
}

TASK [RUN 'SHOW VERSION'] ******************************************************
ok: [intrlink]

TASK [debug] *******************************************************************
ok: [intrlink] => {
    "version.stdout_lines": [
        [
            "Cisco IOS Software, C890 Software (C890-UNIVERSALK9-M), Version 15.1(4)M4, RELEASE SOFTWARE (fc1)"
            "Technical Support: http://www.cisco.com/techsupport",
            "Copyright (c) 1986-2012 by Cisco Systems, Inc.",
            "Compiled Wed 21-Mar-12 01:40 by prod_rel_team",
            "",
            "ROM: System Bootstrap, Version 12.4(22r)YB3, RELEASE SOFTWARE (fc1)",
            "",
            "DevRouter uptime is 1 day, 3 hours, 18 minutes",
            "System returned to ROM by reload at 13:01:06 UTC Thu Jun 9 2016",
            "System restarted at 13:01:44 UTC Thu Jun 9 2016",
            "System image file is \"flash:c890-universalk9-mz.151-4.M4.bin\"",
            "Last reload type: Normal Reload",
            "Last reload reason: Reload Command",
            "",
            "",
            "",
            "This product contains cryptographic features and is subject to United",
            "States and local country laws governing import, export, transfer and",
            "use. Delivery of Cisco cryptographic products does not imply",
            "third-party authority to import, export, distribute or use encryption.",
            "Importers, exporters, distributors and users are responsible for",
            "compliance with U.S. and local country laws. By using this product you",
            "agree to comply with applicable laws and regulations. If you are unable",
            "to comply with U.S. and local laws, return this product immediately.",
            "",
            "A summary of U.S. laws governing Cisco cryptographic products may be found at:",
            "http://www.cisco.com/wwl/export/crypto/tool/stqrg.html",
            "",
            "If you require further assistance please contact us by sending email to",
            "[email protected].",
            "",
            "Cisco 891 (MPC8300) processor (revision 1.0) with 498688K/25600K bytes of memory.",
            "Processor board ID FGL17032316",
            "",
            "9 FastEthernet interfaces",
            "1 Gigabit Ethernet interface",
            "1 Serial interface",
            "1 terminal line",
            "1 Virtual Private Network (VPN) Module",
            "256K bytes of non-volatile configuration memory.",
            "250880K bytes of ATA CompactFlash (Read/Write)",
            "",
            "",
            "License Info:",
            "",
            "License UDI:",
            "",
            "-------------------------------------------------",
            "Device#\t  PID\t\t\tSN",
            "-------------------------------------------------",
            "*0  \t  CISCO891-K9           FGL17032316     ",
            "",
            "",
            "",
            "License Information for 'c890'",
            "    License Level: advipservices   Type: Permanent",
            "    Next reboot license Level: advipservices",
            "",
            "",
            "Configuration register is 0x2102",
            ""
        ],
        [
            "Interface                  IP-Address      OK? Method Status                Protocol",
            "Async1                     unassigned      YES unset  down                  down    ",
            "FastEthernet0              unassigned      YES unset  up                    up      ",
            "FastEthernet1              unassigned      YES unset  administratively down down    ",
            "FastEthernet2              unassigned      YES unset  administratively down down    ",
            "FastEthernet3              unassigned      YES unset  administratively down down    ",
            "FastEthernet4              unassigned      YES unset  administratively down down    ",
            "FastEthernet5              unassigned      YES unset  administratively down down    ",
            "FastEthernet6              unassigned      YES unset  administratively down down    ",
            "FastEthernet7              unassigned      YES unset  administratively down down    ",
            "FastEthernet8              unassigned      YES NVRAM  administratively down down    ",
            "GigabitEthernet0           unassigned      YES NVRAM  administratively down down    ",
            "Vlan1                      192.168.168.1   YES NVRAM  down                  down    ",
            "Vlan3                      192.168.168.1   YES NVRAM  up                    up      "
        ]
    ]
}

PLAY RECAP *********************************************************************
intrlink                   : ok=3    changed=0    unreachable=0    failed=0


0
PS C:\TFGROOT\Temp\NodeTest>

When using -e the JSON needs to be formatted like:

-e "{ "KEY" : "VALUE" }"

JSON.stringify yields:

{"KEY":"VALUE"}

The whitespace is critical to ansible parsing the variables.  Doublequotes on the outside of the JSON object are required for reading the command off the cli properly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants