Notes

Ansible and Infrastructure Management

Many developers and system administrators manage servers by logging into them via SSH, making changes, and logging off. Some of these changes would be documented, some would not. If an admin needed to make the same change to many servers (for example, changing one value in a config file), the admin would manually log into each server and repeatedly make this change.

Tags

If you have a large playbook, it may become useful to be able to run only a specific part of it rather than running everything in the playbook.

tasks:
- yum:
name:
- httpd
- memcached
state: present
tags:
- packages
- template:
src: templates/src.j2
dest: /etc/foo.conf
tags:
- configuration
$ ansible-playbook example.yml --tags "configuration,packages"
$ ansible-playbook example.yml --skip-tags "packages"

Copy a file to a server

$ ansible host-group -m copy -a "src=/etc/hosts dest=/tmp/hosts"

Retrive a file from a server

$ ansible host-group -s -m fetch -a "src=/etc/hosts dest=/tmp"
  • -s: become super user
  • -m: use module
  • -a: arguments

Accelerated mode

Accelerated mode can be anywhere from 2-6x faster than SSH with ControlPersist enabled, and 10x faster than paramiko.

---
- hosts: all
accelerate: true

Async and polling

caution

Not all modules support asynchronous

---
- hosts: all
remote_user: root
tasks:
- name: simulate long running op (15 sec), wait for up to 45 sec, poll every 5 sec
command: /bin/sleep 15
async: 45
poll: 5
register: sleep_job_result
- name: Check status of tasks
async_status: jid={{ sleep_job_result.ansible_job_id }}
register: job_result
until: job_result.finished
retries: 30

Strategy

Strategy defines how a playbook is executed in ansible

  • Linear strategy: Run each task across all servers in parallel at the same time. It wait for the task to finish on all servers before proceeding to the next task

Ansible notes

  • Free strategy: Each server run each task independent of other severs and do not wait for the taks on other servers to finish each servers can go right to the end as fast as it can without bothering any of other servers

  • Batch strategy: Ansible run tasks on three servers and when the tasks finishes, ansible run tasks on the next batch

Forks

Suppose you want to run ansible playbook on 100 hosts. Ansible will only run 5 fork at a time You can change the forks value as much as you like in ansible.cfg. But make sure you have sufficient CPU resource and network bandwith for this operation

forks = 5

Task failure

Set value

any_errors_fatal: true

If any tasks failed on one server. Ansible stop the execution of the play on all servers and exit

Ignore errors, failed_when

  • ignore_errors: Ignore errors
  • failed_when: fail on a condition
- command: cat /var/log/server.log
register: command_output
failed_when: "'ERROR' in command_output.stdout"

Lookups

Import k8s manifest with Lookup
tasks:
- name: Create hello-k8s resources and wait until they are Ready.
k8s:
state: present
# We’re loading the contents of the file into the definition parameter of this task, and
# we’re doing that twice, once for the deployment, and once for the service.
definition: "{{ lookup('file', 'files/' + item) }}"
# We also added wait: true to make sure Ansible waits until all the pods in the
# deployment are ‘Ready’, before continuing on to test that the Deployment works
# correctly.
wait: true
with_items:
- hello-k8s-deployment.yml
- hello-k8s-service.yml

Dynamic inventory

Last updated on