Skip to content

Instantly share code, notes, and snippets.

@meshula
Last active September 29, 2021 18:03
Show Gist options
  • Save meshula/e5f1c277129b7426b9a3d5bcd7dc40dd to your computer and use it in GitHub Desktop.
Save meshula/e5f1c277129b7426b9a3d5bcd7dc40dd to your computer and use it in GitHub Desktop.

Definitions

Ansible server - where ansible is installed Module - a set of commands meant to run on the client Task - a section in a playbook with a single procedure to complete Role - independent yaml files that can be invoked from playbooks, like functions in a C program. The directory structure is very particular. A role is created by ansible-galaxy init role1 and results in:

[root@ansible-server test2]# tree
.
`-- role1
    |-- defaults     # default variables for playbook
    |   `-- main.yml
    |-- handlers
    |   `-- main.yml
    |-- meta
    |   `-- main.yml
    |-- README.md
    |-- tasks
    |   `-- main.yml
    |-- tests
    |   |-- inventory
    |   `-- test.yml
    `-- vars         # all variables used by role1
        `-- main.yml

7 directories, 8 files

Fact - client system information from gather-facts ansible -i hosts all -m setup will gather facts from the system variables

Inventory - yaml file with ansible client servers. The groups in this yaml file are refered to by a playbook

[group1]
host1 ansible_host=192.168.100.2 ansible_ssh_port=22
[group2]
host2 ansible_host=192.168.100.3 ansible_ssh_port=22

Playbook - scripts in yaml files that send commands to remote systems specific roles can be assigned to individual hosts. A playbook can be dry-run using ansible-playbook -i hosts p4.yml --check and run by ansible-playbook -i hosts p4.yml -k Play - execution of a playbook Handler - task called if notifier is present Notifier - section attributed to a task which calls a handler on changes Tag - name that can refer to specific task or task group

Notes

where are our ansible hosts?

ansible -i hosts all -m ping returns

[WARNING]: Unable to parse /home/nporcino/hosts as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'

How to reference our inventory?

Note: ping a specific server: ansible -i hosts all -m ping --limit host2

copy a file to all hosts

ansible -i hosts all -m copy -a "src=/root/test_ansible/testfile dest=/tmp/testfile"

install a package on all hosts via yum

ansible -i hosts all -m yum -a 'name=ncdu state=present'

uninstall that package on all hosts

ansible -i hosts all -m yum -a 'name=ncdu state=absent'

install lldpad on host group1

Install lldpad, and then interact with systemd to ensure that lldpad is started as part of systemd config:

- hosts: group1
  tasks:
  - name: Install lldpad package
    yum:
      name: lldpad
      state: latest
  - name: check lldpad service status
    service:
      name: lldpad
      state: started

handle configuration file changes

This example restarts the sshd service if sshd_config's port entry is modified:

- hosts: group2
  tasks:
  - name: sshd config file modify port
    lineinfile:
     path: /etc/ssh/sshd_config
     regexp: 'Port 28675'
     line: '#Port 22'
    notify:
       - restart sshd
handlers
    - name: restart sshd
      service: sshd
        name: sshd
        state: restarted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment