Ansible cmd function

From UVOO Tech Wiki
Jump to navigation Jump to search

Bash function wrapper

run_cmd(){
  host=$1
  cmd=$2
  conn=${3:-linux}
  port=${4:-22}
  if [[ $conn == "winrm" ]]; then
    ansible all --inventory $host, --module-name=win_shell --args="$cmd" -e ansible_password=$ANSIBLE_PASSWORD -e ansible_user=$ANSIBLE_USER -e ansible_winrm_transport=ntlm -e ansible_connection=winrm -e ansible_port=$port
  elif [[ $conn == "ssh" ]]; then
    ansible all --inventory $host, --module-name=shell --args="$cmd" -e ansible_password=$ANSIBLE_PASSWORD -e ansible_user=$ANSIBLE_USER  --become -e ansible_become_password=$ANSIBLE_PASSWORD -e ansible_port=$port
  else
    echo "Connection type $conn not supported."
  fi
}

run_cmd host.example.com "whoami" winrm 5986