Ansible
Agentless Push-based Configuration Management Tool
- The Control node is where Ansible is installed
- The Ansible Inventory contains all known nodes
- A Managed Node is a Host that Ansible controls
- Ansible Playbooks contain multiple plays that define commands and modules to run the Nodes
- Use Ansible Roles to load related vars, files, tasks, handlers, and other Ansible artifacts based on a known file structure
- Ansible Handlers are tasks that only run when notified.
- Ansible tries to be indempotent, only applying changes if the desired state is achieved
- Customize Ansible with Ansible Modules and Ansible Plugins
- Configuration Example File https://github.com/ansible/ansible/blob/stable-2.9/examples/ansible.cfg
Pros
- Agent less
- YAML is easy to learn
Cons
- Performance speed less then other tools
- YAML not as powerful as Ruby
Getting Started
- Ping all known hosts in group home-lab
ansible home-lab -m ping
- Install Ansible
- Create configuration file and add hosts in
/etc/ansible/ansible.cfg
or a local repo
- Run commands
Ansible CLI
- Command Pattern
ansible [pattern] -m [module] -a "[module options]"
- Run one-off / Ad-hoc Commands with
Ansible Authentication
- Ansible connects to all remote devices with the user name you are using on the control node.
- Ansible assumes you are using SSH keys to connect to remote machines
- use a ssh agent, use the
--private-key
flag to specify a pem file, or add the private key file to an inventory withansible_ssh_private_key_file
.
- use a ssh agent, use the
- Understanding privilege escalation: become — Ansible Documentation
Ansible Variables
- Using Variables — Ansible Documentation
- Cache task output into a registers
- Specify
--extra-vars
on the CLI likekey=value
Ansible Secrets
- Interactive Prompts
- Or use Ansible Vault
Ansible Conditionals
- Specify a Jinja2 Test Expression with
when
Tests — Ansible Documentation - Tasks that pass the test are executud
- Can also use Ansible Facts Conditionals Facts— Ansible Documentation
Ansible Filters
- Ansible Filters are the preferred way to manipulate data in Ansible
- Default variables
{{ some_variable | default(5) }}
- Omit variables
{{ item.mode | default(omit) }}
- Transforming dictionaries into lists with
{{ dict | dict2items }}
- Combining and selecting data
- Default variables
- Ansible can manage Windows machines as long as they have Powershell installed. Ansible uses PowerShell Remoting to execute commands on remote Windows hosts.