-1

I am trying to automate the installation of Icinga2 on many remote clients. The PKI token will get generated on the Icinga server and it's different for each client - Then it should be sent to each client. As part of the client installation, icinga2 node wizard would run and I would like to pipe a series of inputs to a prompt as below. Can you please check to see if I use the heredoc correctly?

#!/bin/bash

while read f; do
   ssh-copy-id myusername"$f"
   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'

   PKI= echo $/tmp/pkicode
   icinga2 node wizard << EOF
   Y
   Enter
   master.icinga.test.com
   Y
   10.20.20.1
   N
   Y
   $PKI
   Enter
   Enter
   Y
   Y
   Enter
   Enter
   N
   N
   EOF
   scp ./zones.conf myusername@"$f":/etc/icinga2/zones.conf
done < linux-list.txt

Thank you

Irina I
  • 31
  • 1
  • 5
  • BTW, you might want to read your `linux-list.txt` on a FD other than stdin, to avoid the need to ` – Charles Duffy Jul 27 '18 at 20:06
  • Beyond that, this question is a bit overbroad -- an ideal StackOverflow question focuses on only a single, specific problem, with the shortest possible [mcve] that reproduces it; "here's my code, what looks like it could be wrong?", without a specific misbehavior or efforts to trim to the shortest reproducer, is hard to build a canonical answer for. – Charles Duffy Jul 27 '18 at 20:08
  • 1
    FWIW the previous question in this long thread is https://stackoverflow.com/questions/51544500/automate-feeding-the-output-of-a-command-to-a-prompt – tripleee Jul 27 '18 at 21:47
  • 1
    Please take a look: http://www.shellcheck.net/ – Cyrus Jul 27 '18 at 21:48

1 Answers1

0

You have a few errors in the code.

#!/bin/bash

while read f; do
   ssh-copy-id myusername"$f"
   ssh myusername@"$f" '
     :
    ' </dev/null

   ssh myusername@master.icinga.test.com icinga2 pki ticket --cn "$f" </dev/null |
   ssh myusername@"$f" '
      PKI=$(cat)
      icinga2 node wizard <<________EOF
Y

master.icinga.test.com
Y
10.20.20.1
N
Y
$PKI


Y
Y


N
N
________EOF
        '
   scp ./zones.conf myusername@"$f":/etc/icinga2/zones.conf
done < linux-list.txt

You'll notice in particular how the last ssh needs to have all the code which should run there inside the single quotes. It receives the ticket on its standard input, and uses $(cat) to directly capture it in a variable. Also, the empty lines in the here document produce an etrer keystroke with nothing else before it in the input to icinga2.

As already suggested in previous questions of yours, you should probably be using icinga2 node setup instead of node wizard in scripts.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Thank you, I am reading the docs for the icinga node setup command, not sure how the satellite node can access the server and get a pki ticket. – Irina I Jul 30 '18 at 19:01
  • Still the same, `ssh root@master icinga2 blah blah get ticket for "$f" | ssh user@"$f" 'ticket=$(cat); icinga2 node setup --ticket "$ticket" --etc --etc'` – tripleee Jul 30 '18 at 19:14