run playbook with sudo
ansible-playbook -v config-users.yaml --sudo --sudo-user=joe --ask-sudo-pass
use different Hosts file
ansible-playbook -v -i /path/to/hosts
run playbook but only a specific task (tag)
ansible-playbook playbooks/restore_bitbucket.yaml -i hosts --tags rsync
or to skip: (--skip-tags tag1, tag2)
store output of a command as a variable
shell: cat /etc/network | grep eth0
register: address
debug: msg="address is {{ address.stdout }}"
configure multiple items with one task
- name: more complex items to add several users
user:
name: "{{ item.name }}"
uid: "{{ item.uid }}"
groups: "{{ item.groups }}"
state: present
with_items:
- { name: testuser1, uid: 1002, groups: "wheel, staff" }
- { name: testuser2, uid: 1003, groups: staff }
get path location of current Playbook (pwd)
{{ playbook_dir }}
Set playbook to be verbose by default
- hosts: blah
strategy: debug
run playbook with verbose traceback
ansible-playbook -i hosts myPlaybook.yaml -vvv
run playbook on multiple Host groups
- hosts: "search_head, deployer"
Run playbook locally on host
hosts: 127.0.0.1
connection: local
Prompt for password during Playbook run
# Playbook to change user password
- name: pw change
hosts: target
become: true
become_user: root
vars_prompt:
- name: username
prompt: "enter username for which to change the pw"
- name: password
prompt: "enter new password"
private: yes
tasks:
- name: change pw
user: "name={{ username }} password={{ password }} update_password=always"
run playbook with "dry run" / NOOP / simulate
ansible-playbook foo.yml --check
Run task on different target,
- name: run something on some other server
debug: msg="running stuff"
delegate_to: someserver
Delegate task to a host group
- name: restart web servers
service: name=memcached state=restarted
delegate_to: "{{ item }}"
with_items: "{{ groups['webservers'] }}"
Get IP or facter of a remote host
- name: get IP
debug: msg="{{ hostvars['nycweb01']['ansible_default_ipv4']['address'] }}"
or
debug: msg="{{ hostvars[item]['ansible_ssh_host'] }}"
with_items: "{{ groups['webservers'] }}"
synchronize file (copy file from Ansible host to target)
- synchronize:
src: "{{ playbook_dir }}/files/vscode.repo"
dest: /etc/yum.repos.d/
synchronize from server A to server B with a wildcard
- name: copy Splunk Apps
synchronize:
src: "/opt/splunk/etc/apps/{{ item }}" (server A)
dest: "/opt/splunk/etc/shcluster/apps/" (server B)
with_items:
- item1
- item2
delegate_to: server A
wget a file to a location
- get_url:
url: 'https://dl.google.com/go/go1.10.linux-amd64.tar.gz'
dest: '/tmp'
force: no # dont download if file already exists
untar tar.gz
No comments:
Post a Comment