Ansible

Ansible

%toc

Befehl ausfuehren

ansible production -m command -a uptime
ansible testserver -m fetch -a 'src=filepath dest=filename flat=yes'

Playbook

Playbook nur auf einem Host ausfuehren:

ansible-playbook -l hosts playbook.yml
ansible-playbook --limit hosts playbook.yml
ansible-playbook --syntax-check playbook.yml

Roles

Use su

    become: yes
    become_user: username
    #su: yes
    #su_user: username

Vault

Local ausfuehren

ansible-playbook -i "localhost," -c local workstation.yml

User sudo with a password

ansible-playbook site.yml --ask-sudo-pass

Loops

Playbook Examples

Variablen

- debug:
  args:
    msg: 'System {{ ansible_distribution_release }}'

ansible host -m setup

Debug Messages:

      - name: Ansible check directory exists
        stat:
          path: /etc/pihole
        register: folderstat

      - debug:
          msg: "{{ folderstat }}"

Conditionals:

      - set_fact: test=false
        when: folderstat.stat.exists == false

Commandline

ansible group -m ping
ansible group -m command -a "sudo touch /tmp/ansible.txt" --ask-sudo-pass

Variables

    - name: Include vm variables
      include_vars:
        file: vars/vms.yml
        name: vms
    - set_fact:
        vm: "{{ vms.all.hosts[fqdn] }}"