Ansible

Ansible

Install from ppa for Ubuntu

sudo apt install -y software-properties-common
sudo add-apt-repository --yes --update ppa:ansible/ansible
sudo apt install ansible

General Commands

Printing out all of the Ansible Facts

ansible -i [inventory file name] [host name] -m setup > [file name]

Playbook

Running a playbook limiting to certain sections

ansible-playbook [playbook name] -i [inventory host file] --limit [host section name]

Scenarios

Restarting a server

  - name: Set Host name
    ...
    register: task_result

  - name: Reboot immediately if there was a change
    reboot:
    when: task_result is changed

  - name: Wait for the reboot to complete if there was a change.
    wait_for_connection:
      connect_timeout: 20
      sleep: 5
      delay: 5
      timeout: 300
    when: task_result is changed

Setup Users for Ansible

pip install passlib

python -c "from passlib.hash import sha512_crypt; import getpass; print(sha512_crypt.using(rounds=5000).hash(getpass.getpass()))"

Useful Ansible Facts

Fact NamePurposeExampleansible_distributionOS Distribution NameUbuntuansible_distribution_versionVersion number of OS18.04ansible_distribution_releaseRelease name of OSbionic