Ansible
Ansible in general
I introduced Ansible at work as well as in my home and came to appreciate it a lot. If hosts are set up with ansible it is easy to replicate and keep track of all instances. At the same time I consider it documented, if set up purely with ansible.
To get started I can recommend this book. It is a perfect starting point and if you want to go deeper just use the official ansible documentation.
Run ansible tasks in parallel
I am scheduling some lxc container with awx and ansible on my proxmox node. Now I added a new K8s development cluster, which consumes a lot of power, while running, so I also want to make sure, that this is shutdown most of the time.
With the containers it was easy with simple tasks, because they are started and stopped quickly.
Following my playbook for containers, which is fast enough:
- name: Start/stop containers
hosts: proxmox
become: yes
vars:
ids:
- '202'
- '203'
- '204'
- '206'
- '207'
state: started
tasks:
- name: " LXC's"
proxmox:
api_host: 10.0.0.3
api_user: root@pam
api_password: !vault |
$ANSIBLE_VAULT;1.1;AES256
62623164336537346264373264386431356534636162343439393734303233386437656365623161
3035376339316363353764626566343832653834656638650a353166326630366563363362396633
NOT A REAL HASH, EVEN WITH SECRET OF NO USE
3735663235646638360a653961366538383036663035666134303231346562323732306334373965
30323435616630306639396335386133326431663066333539616636393466653764
node: pve
vmid: ""
state: ""
loop: ""
The K8s cluster is composed of virtual machines, which also mounts nfs-shares, and they take some time shutting down. That is why I want to run the commands in parallel.
- name: Start/stop K8s related vms
hosts: proxmox
become: yes
vars:
ids:
- '301'
- '310'
- '311'
- '312'
state: started
tasks:
- name: " vm's"
proxmox_kvm:
api_host: 10.0.0.3
api_user: root@pam
api_password: !vault |
$ANSIBLE_VAULT;1.1;AES256
62623164336537346264373264386431356534636162343439393734303233386437656365623161
3035376339316363353764626566343832653834656638650a353166326630366563363362396633
NOT A REAL HASH, EVEN WITH SECRET OF NO USE
3735663235646638360a653961366538383036663035666134303231346562323732306334373965
30323435616630306639396335386133326431663066333539616636393466653764
node: pve
vmid: ""
state: ""
timeout: 30
force: true
async: 175 # proxmox forcefully terminates a vm after 120 secondsi
poll: 0 # moves on to the next task immediately without checking back (concurency)
loop: ""
register: result
- name: Check async tasks status
async_status:
jid: ""
loop: ""
loop_control:
loop_var: "async_result_item"
register: async_poll_results
until: async_poll_results.finished
retries: 200
The option poll: 0
makes sure to not check if a task was successful and ansible goes immediately to
the next task. The task with the module async_status allows me to check back on them in this case.
Like that one has a nice mechanic to parallelize long-running tasks.
Snapshots on proxmox
For some experiments, with multiple vm’s (4 for the k8s cluster), I had to quickly take snapshots at the same time. That is how I do it over ansible, respectively with awx:
https://docs.ansible.com/ansible/latest/collections/community/general/proxmox_snap_module.html
AWX
I use an older version of AWX because I decided to not run it on a K8s cluster, because I only start the cluster on certain occasions to save energy. There is an easy install possibility with docker-compose on a docker instance see here.
If you have a properly set up ansible project it is super simple to automated it in AWX. As soon as multiple users deploy scripts on servers it gets a lot easier to track, in what state the servers are.
More to come…