#su - ocp
#export KUBECONFIG=/home/ocp/cluster_mgnt_roles/kubeconfig.ostest
#oc exec -it -n openstack openstackclient bash

cd ~cloud-admin 

cat >ansible_ha.env<<EOF
export ANSIBLE_INVENTORY="/home/cloud-admin/hosts"
export ANSIBLE_SSH_ARGS="-F /home/cloud-admin/ssh.config.ansible"
export ANSIBLE_CONFIG="/home/cloud-admin/ansible.cfg"

EOF

cat >/home/cloud-admin/ansible.cfg<<EOF
[defaults]
export ANSIBLE_STDOUT_CALLBACK=debug
stdout_callback = debug
deprecation_warnings=False
EOF


export n_cntrl=$( grep controller /etc/hosts|grep ctlplane|wc -l)
export n_cntrl=$(($n_cntrl -1))

export n_comp=$( grep computeleaf /etc/hosts|grep ctlplane|wc -l)
#export n_comp=$(($n_comp -1))


cat >/home/cloud-admin/hosts<<EOF
$(for i in $(eval echo {0..$n_cntrl});do echo "controller-${i} ansible_host=controller-${i} ansible_user=cloud-admin ansible_private_key_file=/home/cloud-admin/.ssh/id_rsa";done)
$(for i in $(eval echo {0..$n_comp});do echo "computeleaf-${i} ansible_host=computeleaf${i} ansible_user=heat-admin ansible_private_key_file=/home/stack/.ssh/id_rsa";done)


[controller]
$(for i in $(eval echo {0..$n_cntrl});do echo "controller-${i}";done)

[compute]
$(for i in $(eval echo {1..$n_comp});do echo "computeleaf${i}";done)


[overcloud_nodes]
$(for i in $(eval echo {1..$n_comp});do echo "computeleaf${i}";done)
$(for i in $(eval echo {0..$n_cntrl});do echo "controller-${i}";done)
$(if [ $n_ceph -ne 0 ];then for i in $(eval echo {0..$n_ceph});do echo "ceph-${i}";done;fi)
$(if [ $n_messaging -ne 0 ];then for i in $(eval echo {0..$n_messaging});do echo "messaging-${i}";done;fi)
$(if [ $n_database -ne 0 ];then for i in $(eval echo {0..$n_database});do echo "database-${i}";done;fi)

EOF

rm ssh.config.ansible
grep controller /etc/hosts|grep ctlplane|while read node; do node_name=$(echo $node | cut -f 3 -d " "|cut -f 1 -d".") ; node_ip=$(echo $node | cut -f 1 -d " ");echo -e "Host $node_name\n Hostname $node_ip\n IdentityFile /home/cloud-admin/.ssh/id_rsa\n User cloud-admin\n StrictHostKeyChecking no\n UserKnownHostsFile=/dev/null\n"; done >> ssh.config.ansible

grep computeleaf /etc/hosts|grep ctlplane|while read node; do node_name=$(echo $node | cut -f 3 -d " "|cut -f 1 -d"-") ; node_ip=$(echo $node | cut -f 1 -d " ");echo -e "Host $node_name\n Hostname $node_ip\n IdentityFile /home/cloud-admin/.ssh/id_rsa\n User cloud-admin\n StrictHostKeyChecking no\n UserKnownHostsFile=/dev/null\n"; done >> ssh.config.ansible

#test ansible
source ./ansible_ha.env
ansible overcloud_nodes -b -mshell -a'hostname -s'

