0

I have a script that reads a text file that has all the nodes listed in there:

node1
node2
node3
.
.
.

This is my script:

#!/bin/bash

while read f; do
   ssh-copy-id myusername@"$f"
   ssh username@master.icinga.test.com
   icinga2 pki ticket --cn '$f' 
done < linux-list.txt

while read f; do
   ssh myusername@"$f" '
        yum install -y epel-release
        wget --no-check-certificate https://packages.icinga.org/epel/7/release/noarch/icinga-rpm-release-7-1.el7.centos.noarch.rpm
        yum install icinga-rpm-release-7-1.el7.centos.noarch.rpm
        yum install -y icinga2  nagios-plugins-all
        chown -R icinga:icinga /etc/icinga2  /var/lib/icinga2 /var/log/icinga2' </dev/null
   ssh myusername@master.icinga.test.com icinga2 pki ticket --cn "$f" |
   ssh myusername@"$f" 'cat >/tmp/pkicode'
   scp ./zones.conf myusername@"$f":/etc/icinga2/zones.conf
done < linux-list.txt

1) The script should log into the Icinga master and run a command icinga2 pki ticket --cn '$f' for each node found on that linux-list.txt file and generate a ticket for each host

2) Then sends this generated code to each node ($f)

After going through all the commands above, then the script needs to run a command icinga2 node wizard which will start a question/answer prompt and needs someone to type the answer as shown below.

Icinga master's FQDN is master.icinga.test.com and it's IP is 10.20.20.1. The PKI ticket is what we generated earlier on the Icinga master and it's different for each host.

Is there any way to automate this?

Y
Enter
master.icinga.test.com
Y
10.20.20.1
N
Y
[PKI Ticket created earlier on the Icinga master]
Enter
Enter
Y
Y
Enter
Enter
N
N

Thanks

Irina I
  • 21
  • 1
  • 4
  • 3
    Wizards are meant for humans, not for computers. Check whether your tool has a scriptable interface. – that other guy Jul 26 '18 at 17:56
  • https://www.icinga.com/docs/icinga2/latest/doc/11-cli-commands/#cli-command-node vaguely suggests you should be using `node setup` instead of `node wizard` in scripts. – tripleee Jul 27 '18 at 07:40
  • `'$f'` in single quotes just produces the literal string `$f`. To interpolate the value of the variable `f`, use double quotes. But this command should not be in the first loop anyway; you have it vaguely correct in the second loop already. – tripleee Jul 27 '18 at 10:10
  • If you do both things in the same script, there is no need for two loops, anyhow; just move `ssh-copy-id` to the beginning of the second loop and get rid of the first. The reason I suggested to do them separately is to be able to run the parts which don't require user interaction unattended, in parallel, from another script, etc. – tripleee Jul 27 '18 at 10:11
  • 1
    (For reference, the previous question in this saga is https://stackoverflow.com/questions/51525313/how-to-ssh-to-remote-hosts-and-run-multiple-commands) – tripleee Jul 27 '18 at 10:12
  • @tripleee Thank you for your advice. How will the icinga2 daemon get that pki ticket we generated on the server? – Irina I Jul 27 '18 at 18:39
  • https://www.icinga.com/docs/icinga2/latest/doc/06-distributed-monitoring/#distributed-monitoring-automation has the specific example `icinga2 node setup --ticket ead2d570e18c78abf285d6b85524970a0f69c22d --cn icinga2-client1.localdomain --endpoint icinga2-master1.localdomain --zone icinga2-client1.localdomain --parent_zone master --parent_host icinga2-master1.localdomain --trustedcert /var/lib/icinga2/certs/trusted-parent.crt --accept-commands --accept-config --disable-confd` – tripleee Jul 27 '18 at 19:20
  • If reading documentation is not a strength of yours, again, maybe posting a separate question about just this specific problem may be a better use of both your own time and mine. – tripleee Jul 27 '18 at 19:21
  • @tripleee, sure, I created a separate question: https://stackoverflow.com/questions/51564830/how-to-use-heredoc-to-answers-prompt-questions Thank you so much for all your help – Irina I Jul 27 '18 at 20:04

1 Answers1

0

in the future, try to create a Minimal, Complete, and Verifiable example, because that was too long to read.

However, it looks like you just want to pipe a series of inputs to a prompt. You can do this with a heredoc, like so

PKI="some text whatever ticket thing blahblahblah"
somecmd << EOF
Y
Enter
master.icinga.test.com
Y
10.20.20.1
N
Y
$PKI
Enter
Enter
Y
Y
Enter
Enter
N
N
EOF

heredocs can do variable expansion. cool.

jeremysprofile
  • 6,648
  • 4
  • 25
  • 41
  • Thanks, so I should create a file (i.e. answers.txt) and then call it after 'icinga2 pki ticket --cn '$f' ' command? Can you elaborate on that more please? Thanks – Irina I Jul 26 '18 at 19:25
  • 1
    No, this is a complete answer. Maybe google [here document.](https://en.wikipedia.org/wiki/Here_document) – tripleee Jul 26 '18 at 20:58
  • @IrinaI, please see tripleee's comment – jeremysprofile Jul 26 '18 at 21:10
  • For some reason my previous comment is not showing up, where will I add the heredoc? I am Googling it but still not clear how the while loop and icinga installation will be connected to the heredoc. – Irina I Jul 27 '18 at 19:38