Ansible Part 2

Plays

A play generally includes hosts otherwise it's simply a task

Ubuntu Ansible Install
sudo apt-add-repository ppa:ansible/ansible
sudo apt-get update
sudo apt-get install ansible
Create a New role for Docker & Setup Folder Structure
mkdir -p docker-role/roles && cd docker-role
ansible-galaxy init --offline --init-path=roles docker
Debugging - Run a single play in a playbook
  • Within the playbook attach a tag (tags: test-auth) to a play
- name: validate-kube-config-auth
  hosts: localhost
  tags: test-auth
  tasks:
    - name: check-system-admin
      shell: oc whoami | grep system:admin
      register: whoami
  • Run the Playbook targeting a specific play based on the tag defined above
[root@bastion openshift-homework]# ansible-playbook ocp-deploy-main.yaml -i inventory -t test-auth
Gather facts and process with jq
$ ansible localhost -m setup | sed '1c {' | jq .ansible_facts.ansible_product_version
"ThinkPad X1 Carbon 4th"
Ansible Roles

Roles

Ansible Variables

Variables and Templates
Working with Ansible variables in conditionals
Ansible lookups: vars vs. facts
pre_tasks & set_facts

Modules

Introduction to Shell and Command Modules in Ansible

Good Practice

Project Directory Setup
More Project Directory Setup
15 things you should know about Ansible
10 Good Practice Anisble Play Techniques

Bad Practice ? ;)

Need to accept Keys for Multiple hosts because you can't use ssh-copy-id (e.g. ESXi hosts) and need to use Password in your Ansible Host Vars?

[esxihosts]
172.22.21.161
172.22.21.162

[all:vars]
ansible_connection=ssh
ansible_user=root
ansible_ssh_pass=<password>
  1. Install sshpass
sudo apt install -y sshpass
  1. Loop through your hosts logging in, accepting key and logging out
for host in {161..168}; do sshpass -p <password> ssh -oStrictHostKeyChecking=no root@172.22.21.$host exit; done
  1. Run your playbook
ansible-playbook -i inventory/ esxi-ansible.yaml --tags=solidfire

Test - Remove Keys from your local known_hosts

for host in {161..168}; do ssh-keygen -f "/home/as/.ssh/known_hosts" -R "172.22.21.$host"; done

Optional

for host in {2..8}; do sshpass -p redhat123 ssh-copy-id asoul@server-00$host.server.com; done
for H in {1..8}; do ssh-keygen -f "/home/as/.ssh/known_hosts" -R "rhev-host-00$H.server.com"; done