Difference between revisions of "Ansible"

From UVOO Tech Wiki
Jump to navigation Jump to search
Line 129: Line 129:
  
 
https://www.vgemba.net/ansible/Ansible-WinRM-Workgroup/
 
https://www.vgemba.net/ansible/Ansible-WinRM-Workgroup/
 +
 +
 +
# No set inventory file to run script on remote
 +
```
 +
#!/usr/bin/env bash
 +
set -e
 +
# Create .env file with ANSIBLE_USER/PASSWORD using export then . .env
 +
 +
if [ "$#" -ne 3 ]; then
 +
  echo "Usage $0 <os type linux/windows> <hostname> <script>"
 +
  echo "Example $0 windows myhost.example.com ./test.ps1"
 +
  exit
 +
fi
 +
os=$1
 +
host=$2
 +
script=$3
 +
inventory=$(mktemp tmp.inventory.XXXXX.yaml)
 +
ansible_user=${ANSIBLE_USER}
 +
ansible_password=${ANSIBLE_PASSWORD}
 +
 +
if [[ "$os"=="windows" ]]; then
 +
    windowshost="$host:"
 +
elif [[ "$os"=="linux" ]]; then
 +
    linuxhost="$host:"
 +
else
 +
    echo E: Unsupported os. Must be windows or linux.
 +
    exit
 +
fi
 +
 +
text="
 +
all:
 +
  vars:
 +
    ansible_user: ${ansible_user}
 +
    ansible_password: ${ansible_password}
 +
windows:
 +
  hosts:
 +
    ${windowshost}
 +
  vars:
 +
    ansible_connection: winrm
 +
    ansible_port: 5985
 +
    ansible_winrm_scheme: http
 +
    ansible_winrm_transport: ntlm
 +
    ansible_winrm_server_cert_validation: ignore
 +
linux:
 +
  hosts:
 +
    $linuxhost
 +
  vars:
 +
    ansible_connection: ssh
 +
"
 +
echo "$text" > $inventory
 +
ansible "$host" -m "script $script" -i $inventory
 +
rm $inventory
 +
```

Revision as of 00:45, 20 July 2021

Install and Use

Using apt and older version

sudo apt install ansible sshpass
python3 -m venv venv
source ansible/bin/activate
# pip install -U pip
pip install ansible pywinrm

inventory.yaml

all:
  vars:
    ansible_user: <myusername>
    ansible_password: <mypass>
windows:
  hosts:
    winhost.example.com:
  vars:
    ansible_connection: winrm
    ansible_port: 5985
    ansible_winrm_scheme: http  # Recommend https if possible
    ansible_winrm_transport: ntlm
    ansible_winrm_server_cert_validation: ignore  # Not recommended
linux:
  hosts:
    linuxhost.example.com:
  vars:
    ansible_connection: ssh

https://www.vgemba.net/ansible/Ansible-WinRM-Workgroup/

ansible 'Windows' -m win_shell -i inventory.yaml -a 'pwd'
ansible 'Linux' -i inventory.yaml -a 'pwd'

Run local script on remotes

ansible 'Windows' -m "script ./a.ps1" -i inventory.yaml
ansible myhost.example.com -a "pwd" --ask-pass




Use latest Python and Pip source

Or lets just use Python Source of latest, pip

#!/usr/bin/env bash
set -e pipefail

version='3.9.6'

get() {
sudo apt install -y build-essential checkinstall
sudo apt install -y libreadline-gplv2-dev libncursesw5-dev libssl-dev \
  libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffi-dev zlib1g-dev

curl -LO https://www.python.org/ftp/python/$version/Python-$version.tgz
tar xzf Python-$version.tgz
}

install() {
  cd Python-$version
  ./configure --prefix=/usr/local
  #./configure --prefix=/usr/local --enable-optimizations
  sudo make altinstall
}

get
install

Install ansible in virtual env

python3.9 -m venv venv
source ansible/bin/activate
# pip install -U pip
pip install ansible pywinrm

inventory.toml

[Windows]
winhost.example.com

[Windows:vars]
ansible_user=<myuser>
ansible_password=<mypass>
ansible_connection=winrm
# ansible_port=5985
# ansible_winrm_scheme=http
ansible_port=5986
ansible_winrm_scheme=https
ansible_winrm_server_cert_validation: ignore


[Linux]
linuxhost.example.com


[Linux:vars]
ansible_user=<my user>
ansible_password=<my pass>
ansible_connection=ssh

Presentations

https://www.vgemba.net/ansible/Ansible-WinRM-Workgroup/

No set inventory file to run script on remote

#!/usr/bin/env bash
set -e
# Create .env file with ANSIBLE_USER/PASSWORD using export then . .env

if [ "$#" -ne 3 ]; then
  echo "Usage $0 <os type linux/windows> <hostname> <script>"
  echo "Example $0 windows myhost.example.com ./test.ps1"
  exit
fi
os=$1
host=$2
script=$3
inventory=$(mktemp tmp.inventory.XXXXX.yaml)
ansible_user=${ANSIBLE_USER}
ansible_password=${ANSIBLE_PASSWORD}

if [[ "$os"=="windows" ]]; then
    windowshost="$host:"
elif [[ "$os"=="linux" ]]; then
    linuxhost="$host:"
else
    echo E: Unsupported os. Must be windows or linux.
    exit
fi

text="
all:
  vars:
    ansible_user: ${ansible_user}
    ansible_password: ${ansible_password}
windows:
  hosts:
    ${windowshost}
  vars:
    ansible_connection: winrm
    ansible_port: 5985
    ansible_winrm_scheme: http
    ansible_winrm_transport: ntlm
    ansible_winrm_server_cert_validation: ignore
linux:
  hosts:
    $linuxhost
  vars:
    ansible_connection: ssh
"
echo "$text" > $inventory
ansible "$host" -m "script $script" -i $inventory
rm $inventory