2

I am working on a Vagrant + Ansible configuration for my team that sets up a developer VM, some of the artifacts needed are only available from my corporate network either by being physically on it or connected by VPN.

If we are provisioning on a machine that is not on that corporate network, I'd like to automatically connect to our vpn (using the openconnect client), copy the file and then disconnect, all from Ansible.

In the past, I've detected the network in bash scripts using something like: if nslookup hostname | grep 'can't find'; then ...

My question: is there is clean way to do such a check in an Ansible "when" statement or something like that.

FWIW: I'm fairly new to Ansible so if this is in their documentation and I'm just missing it, please feel free to point me at it and flog me accordingly.

  • If I understand your question correctly, you are looking for the same technique as the one described in serverfault.com/questions/644082/…. Yes, it is in in the docs, but it's quite hard to find and understand, so no flogging :-) – Antonis Christofides Dec 19 '14 at 8:45
  • Looking at that question/answer, it appears they are relying on the command's return val (rc) - but nslookup will return "0" in both of these cases since a "can't find" result is a valid one from the command's POV. ... or am I missing something in that example? – Eric Smalling Dec 19 '14 at 20:46
  • You can use nslookup ... | grep -q "can't find"; or you can use the alternative proposed in the answer by DefionsCode. – Antonis Christofides Dec 20 '14 at 9:07
  • This works well - if you'd like to add it as an answer here, I'll mark it as accepted. – Eric Smalling Dec 30 '14 at 20:14
3

Without getting too fancy, you may just want to use the command module and register the output, like the following:

---
 - name: Register nslookup hostname result
   command: nslookup hostname
   register: ns

 - name: Some other task with conditional
   copy: <params go here>
   when: "'server can\\'t find' in ns.stdout"

If you want to learn a bit more about register variables within Ansible check out the docs here

Additionally, for future reference, if you want to see the JSON available when your register a var, you can do something like the following:

---
 - name: Register nslookup hostname result
   command: nslookup hostname
   register: ns

 - debug: var=ns

And then this should output something like below:

ok: [HOST] => {
    "var": {
        "ns": {
            "changed": true,
            "cmd": [
                "nslookup",
                "hostname"
            ],
            "delta": "0:00:00.054897",
            "end": "2014-12-18 18:51:15.598652",
            "invocation": {
                "module_args": "nslookup hostname",
                "module_name": "command"
            },
            "rc": 0,
            "start": "2014-12-18 18:51:15.543755",
            "stderr": "",
            "stdout": "Server:\t\t192.168.1.1\nAddress:\t192.168.1.1#53\n\n** server can't find hostname: HOSTNAME",
            "stdout_lines": [
                "Server:\t\t192.168.1.1",
                "Address:\t192.168.1.1#53",
                "",
                "** server can't find hostname: HOSTNAME"
            ],
            "warnings": []
        }
    }
}

After being registered you can access any of those attributes with dot notation in later parts of the playbook execution.

Cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.