Ansible Cheatsheet
installation
remote machine should have 'python' - 'gather_facts: False' or 'gather_facts: no' otherwise
uninstall
rm -rf $HOME/.ansible
rm -rf $HOME/.ansible.cfg
sudo rm -rf /usr/local/lib/python2.7/dist-packages/ansible
sudo rm -rf /usr/local/lib/python2.7/dist-packages/ansible-2.5.4.dist-info
sudo rm -rf /usr/local/bin/ansible
sudo rm -rf /usr/local/bin/ansible-config
sudo rm -rf /usr/local/bin/ansible-connection
sudo rm -rf /usr/local/bin/ansible-console
sudo rm -rf /usr/local/bin/ansible-doc
sudo rm -rf /usr/local/bin/ansible-galaxy
sudo rm -rf /usr/local/bin/ansible-inventory
sudo rm -rf /usr/local/bin/ansible-playbook
sudo rm -rf /usr/local/bin/ansible-pull
sudo rm -rf /usr/local/bin/ansible-vault
sudo rm -rf /usr/lib/python2.7/dist-packages/ansible
sudo rm -rf /usr/local/lib/python2.7/dist-packages/ansible
ansible configuration places
- path variable $Ansible_Config
- ~/.ansible.cfg
- /etc/ansible/ansible.cfg
configuration for external roles
filename: ~/.ansible.cfg
check configuration
inventory
without inventory inline host ip
without inventory with pem ssh private ssh key
generate PEM file
ssh-keygen -t rsa -b 4096 -m PEM -f my_ssh_key.pem
ll my_ssh_key.pem
ansible all -i desp000111.vantage.zur, --user=vitalii.cherkashyn -e ansible_ssh_private_key_file=my_ssh_key.pem -m "ping" -vvv
ini file
# example cfg file
[web]
host1
host2 ansible_port=222 # defined inline, interpreted as an integer
[web:vars]
http_port=8080 # all members of 'web' will inherit these
myvar=23 # defined in a :vars section, interpreted as a string
execute with specific remote python version, remote python, rewrite default variables, rewrite variables, override variable
execute ansible for one host only, one host, one remove server, verbosity
ansible-playbook -i "ubs000015.vantage.org , " mkdir.yaml
ansible-playbook welcome-message.yaml -i airflow-test-account-01.ini --limit worker --extra-vars="ACCOUNT_ID=QA01" --user=ubuntu --ssh-extra-args="-i $EC2_KEY" -vvv
ansible all -i airflow-test-account-01.ini --user=ubuntu --ssh-extra-args="-i $EC2_KEY" -m "ping" -vvv
ansible main,worker -i airflow-test-account-01.ini --user=ubuntu --ssh-extra-args="-i $EC2_KEY" -m "ping"
simple file for creating one folder
- hosts: all
tasks:
- name: Creates directory
file:
path: ~/spark-submit/trafficsigns
state: directory
mode: 0775
- name: copy all files from folder
copy:
src: "/home/projects/ubs/current-task/nodes/ansible/files"
dest: ~/spark-submit/trafficsigns
mode: 0775
- debug: msg='folder was amazoncreated for host {{ ansible_host }}'
execute ansible locally, local execution
# --extra-vars="mapr_stream_path={{ some_variable_from_previous_files }}/some-argument" \
ansible localhost \
--extra-vars="deploy_application=1" \
--extra-vars=@group_vars/all/vars/all.yml \
--extra-vars=@group_vars/ubs-staging/vars/ubs-staging.yml \
-m include_role \
-a name="roles/labeler"
execute ansible-playbook with external paramters, bash script ansible-playbook with parameters, extra variables, external variables, env var
with path to file for external parameters, additional variables from external file
ansible-playbook -i inventory.ini playbook.yml --extra-vars @/path/to/var.properties
ansible-playbook playbook.yml --extra-vars=@/path/to/var.properties
external variables inline
ansible-playbook playbook.yml --extra-vars="oc_project=scenario-test mapr_stream_path=/mapr/prod.zurich/vantage/scenario-test"
check is it working, ad-hoc command
Playbooks
ansible-playbook <YAML> # Run on all hosts defined
ansible-playbook <YAML> -f 10 # Run 10 hosts parallel
ansible-playbook <YAML> --verbose # Verbose on successful tasks
ansible-playbook <YAML> -C # Test run
ansible-playbook <YAML> -C -D # Dry run
ansible-playbook <YAML> -l <host> # Run on single host
Run Infos
Syntax Check
Execute arbitrary commands
ansible <hostgroup> -a <command>
ansible all -a "ifconfig -a"
## Debugging
### List facts and state of a host
```sh
ansible <host> -m setup # All facts for one host
ansible <host> -m setup -a 'filter=ansible_eth*' # Only ansible fact for one host
ansible all -m setup -a 'filter=facter_*' # Only facter facts but for all hosts
Save facts to per-host files in /tmp/facts
Ansible Modules
Ansible modules are standalone scripts that can be used inside an Ansible playbook. You can use these modules to run whatever commands it needs to get its job done. Ansible modules are categorized into various groups based on their functionality. There are hundreds of Ansible modules are available.
Format
Place your modules inside
tasks
.
Task formats
One-line
Map
Foldable scalar
Modules
Aptitude
Packages
Deb files
Repositories
Repository keys
git
See: git module
git_config
See: git_config module
user
- user:
state: present
name: git
system: yes
shell: /bin/sh
groups: admin
comment: "Git Version Control"
See: user module
service
See: service module
Shell
shell
Extra options
- shell: echo hello
args:
creates: /path/file # skip if this exists
removes: /path/file # skip if this is missing
chdir: /path # cd here before running
Multiline example
See: shell module
script
- script: /x/y/script.sh
args:
creates: /path/file # skip if this exists
removes: /path/file # skip if this is missing
chdir: /path # cd here before running
See: script module
Files
file
- file:
path: /etc/dir
state: directory # file | link | hard | touch | absent
# Optional:
owner: bin
group: wheel
mode: 0644
recurse: yes # mkdir -p
force: yes # ln -nfs
See: file module
copy
- copy:
src: /app/config/nginx.conf
dest: /etc/nginx/nginx.conf
# Optional:
owner: user
group: user
mode: 0644
backup: yes
See: copy module
template
- template:
src: config/redis.j2
dest: /etc/redis.conf
# Optional:
owner: user
group: user
mode: 0644
backup: yes
See: template module
Playbook snippets
Using templating
Capture shell output
tasks:
- name: some shell
register: sh_out
ignore_errors: yes
become_user: root
shell: |
find /
- name: "Print stdout"
debug:
msg: "{{ sh_out.stdout.split('\n') }}"
- name: "Print stderr"
debug:
msg: "{{ sh_out.stderr.split('\n') }}"
Handling files
tasks:
- name: file operation
file:
path: <file path>
state: file
# optional attributes examples
mode: '0755'
owner: <owner>
group: <group>
modification_time: now
access_time: '{{ "%Y%m%d%H%M.%S" | strftime(stat_var.stat.atime) }}'
Handling directories
tasks:
- name: Change directory
file:
path: <dir path>
state: directory
# optional attributes
recurse: yes # apply owner, group, mtime, atime, mode... recursively to all childs too
Deleting files & directories
Operate on multiple files
For example multi file fetching