Ansible Commands - keshavbaweja-git/guides GitHub Wiki
- ansible.cfg
[defaults]
inventory = hosts
remote_user = <remote-username>
private_key_file = <path-to-private-key>
- hosts
- site.yml
- A Playbook is an ordered list of plays.
- A Play is an ordered list of tasks associated with an unordered set of hosts.
- A task is defined in terms of a single module.
Description | Command |
---|---|
Ping servers | ansible <host-group-name> -m ping |
Check server uptime | ansible <host-group-name> -m command -a uptime |
Pass command line args to playbook | ansible-playbook <pb.yml> --extra-vars "arg1=value1 arg2=value2" |
Pass command line args to playbook | ansible-playbook <pb.yml> -e "arg1=value1 arg2=value2" |
Pass command line args from a file to playbook | ansible-playbook <pb.yml> -e @vars.yml |
Run tasks associated with a tag | ansible-playbook <pb.yml> --tags "tag1" |
Run tasks not associated with a tag | ansible-playbook <pb.yml> --skip-tags "tag1" |
View values of a variable | - debug: var=<var_name> |
Host specification - union of two groups | hosts: group1:group2 |
Host specification - intersection of two groups | hosts: group1:&group2 |
Host specification - exclusion | hosts: group1:!group2 |
Host specification - command line | ansible-playbook -l 'group1:&group2' pb.yml |
Viewing all facts | ansible <server> -m setup |
Viewing a subset of facts | ansible <server/host_group> -m setup -a 'filter=ansible_eth*' |
Place /etc/ansible/facts.d
on the host. Access these facts as variable ansible_local
in playbook.
A dictionary that contains all of the variables defined on all the hosts, keyed by the hostname as known to ansible.
Hostname of the current host as known to ansible.
- name: capture output
command: whoami
register: result
- debug: var=result
Value of a registered variable is always a dictionary.
- name: capture output of id command
command: id -un
register: login
- debug: "Logged in as users {{ login.stdout }}"
- name: Copy and unarchive installation
unarchive:
src: "{{ installation_file }}"
dest: "{{ destination_folder}}"
To avoid overwrites on reruns, add a parameter "creates" with value set to path of one of the file created by unarchive action. If that file exists, unarchive won't overwrite.
file:
path: "{{ destination_folder }}"
state: "directory"
- name: Create symbolic link
file:
src: "<path_to>"
path: "<symbolic_link>"
state: link
debug:
msg: "{{ansible_fqdn}}"
- name: Stop spark slave
when: inventory_hostname in groups['spark-slave']
command: "{{ spark_home }}/sbin/stop-slave.sh"
tags:
- stop-slave
- name: Create "current" symbolic link
file:
src: "{{ base_folder }}/{{ version }}"
path: "{{ base_folder }}/current"
state: link
when: do_not_update_current is undefined
If the above is executed as ansible-playbook <pb.yml> -e "do_not_update_current=''"
, the task above will not be executed
tasks:
- name: Display current host name
debug:
msg: "{{ ansible_fqdn }}"
- name: Folders older than 30 days
find:
paths: "/a/b/c"
age: 30d
file_type: directory
register: "folders_to_delete"
- name: Display folders
debug:
var: "folders_to_delete"
- name: Remove folders
file:
path: "{{ item.path }}"
state: absent
with_items: "{{ folders_to_delete.files }}"
- Linear (default) - Ansible completes execution of a task on all hosts before it proceeds with execution of next task on any host.
- Free - Ansible initiates execution of next task on a host as soon as the previous task completes on that host.