Skip to content

Latest commit

 

History

History
232 lines (189 loc) · 9.07 KB

ansible.org

File metadata and controls

232 lines (189 loc) · 9.07 KB

Ansible

- hosts: web
  tasks:
    - shell: |
        ls -ld /proc/"$(docker top ssh-guest-room | awk '/s6-svscan/ { print $2 }')"/root/tmp
      register: output
    - debug: var=output
  • Skip host key check (example in packer template syntax)
"ansible_env_vars": [
    "ANSIBLE_HOST_KEY_CHECKING=False"
]
  • ip address
- tags: docker
  name: Generate /etc/default/docker file
  copy:
    dest: '/etc/default/docker'
    content: |
      DOCKER_OPTS="-H tcp://{{ ansible_all_ipv4_addresses | ipaddr('172.16.103.0/24') | first }}:2375 -H unix:///var/run/docker.sock"
  • ubuntu 14.04 interfaces
- hosts: web_ubuntu
  tasks:
    - set_fact: interfaces="{{ ansible_interfaces | select('match', '^(em|p|eth|br)[0-9]+') | map('regex_replace', '^(.*)$', 'ansible_\\1') | list | unique }}"
    - debug: var=interfaces
    - debug: msg="{{ interfaces | map('extract', hostvars[inventory_hostname], 'macaddress') | select('defined') | list | unique }}"
    - debug: msg="{{ interfaces | map('extract', hostvars[inventory_hostname], 'ipv4') | select('defined') | list }}"
    - debug: msg="{{ interfaces | map('extract', hostvars[inventory_hostname], 'ipv4_secondaries') | select('defined') | list }}"
  • netplan
- hosts: kubernetes
  tasks:
    - copy:
        dest: /etc/netplan/01-netcfg.yaml
        content: |
          network:
            version: 2
            renderer: networkd
            ethernets:
              {{ ansible_default_ipv4.alias }}:
               dhcp4: no
               addresses: [{{ ansible_all_ipv4_addresses | first }}/23]
               gateway4: {{ ansible_default_ipv4.gateway }}
               nameservers:
                 addresses: [8.8.8.8,8.8.4.4]
  • docker memory
#!/usr/bin/env ansible-playbook

- hosts: web30.intr
  # vars:
  #   ansible_become_pass: "{{ lookup('passwordstore', 'majordomo/ssh/eng') }}"
  #   slack_token: "{{ lookup('passwordstore', 'majordomo/mjru.slack.com/tokens/ansible') }}"
  tasks:
    - shell: "{% raw %}docker stats --format '{{ .Name }} {{ .MemUsage }}' --no-stream | awk '/mariadb/ { print $2 }' | cut -d. -f 1{% endraw %}"
      become: yes
      register: docker

    - debug: var=docker.stdout

    - slack:
        token: "{{ slack_token }}"
        msg: "MySQL container memory took more than 22GB on web30.intr"
        channel: "#test"
      when: docker.stdout|int > 22

Cheat sheet

- name: send a message with a report link using Slack markup
  slack:
    token: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    msg: |
      goaccess report is available at <https://malscan.intr/reports/access-{{ site }}.html> and you could generate it again by invoking:
      ```curl "http://malscan:8052/api/v2/job_templates/10/launch/" -H "Authorization: Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" -H "Content-Type: application/json" --data '{"extra_vars":{"host":"{{ host }}","home":"{{ home }}","site":"{{ site }}"}}'```
  delegate_to: localhost

Learning

Roles

Security

ansible localhost -m user -a ‘password=”!” name=root’ -bK

Tools

Misc

- hosts: web
  tasks:
    - shell: |
        cat > /etc/ntp.conf <<'EOF'
        restrict default ignore
        restrict localhost
        restrict 127.0.0.1
        restrict 192.168.0.0 mask 255.255.0.0
        restrict 172.16.0.0 mask 255.255.0.0
        restrict 127.127.1.0

        server 172.16.102.1 iburst burst
        server 172.16.103.1 iburst burst

        server  127.127.1.0
        fudge   127.127.1.0 stratum 10

        driftfile /var/lib/ntp/drift
        EOF

        nix-shell -p ntp --run 'ntpd -qg'

---
- hosts: web33.intr
  become: yes
  tasks:
    - shell: |
        kill -SIGHUP "$(docker top taskexecutor | awk '/python/ { print $2 }')"

[ssh_connection]

# ssh arguments to use
# Leaving off ControlPersist will result in poor performance, so use 
# paramiko on older platforms rather than removing it
#ssh_args = -o ControlMaster=auto -o ControlPersist=60s
ssh_args = -4 -o ControlMaster=auto -o ControlPersist=60s -o PreferredAuthentications=publickey

# The path to use for the ControlPath sockets. This defaults to
# "%(directory)s/ansible-ssh-%%h-%%p-%%r", however on some systems with
# very long hostnames or very long path names (caused by long user names or 
# deeply nested home directories) this can exceed the character limit on
# file socket names (108 characters for most platforms). In that case, you 
# may wish to shorten the string below.
# 
# Example: 
# control_path = %(directory)s/%%h-%%r
control_path = %(directory)s/ansible-ssh-%%h-%%p-%%r
#control_path = /run/shm/%r@%h-%p
#ControlPath  /run/shm/%r@%h-%p

# Enabling pipelining reduces the number of SSH operations required to 
# execute a module on the remote server. This can result in a significant 
# performance improvement when enabled, however when using "sudo:" you must 
# first disable 'requiretty' in /etc/sudoers
#
# By default, this option is disabled to preserve compatibility with
# sudoers configurations that have requiretty (the default on many distros).
# 
#pipelining = False
pipelining = true

# if True, make ansible use scp if the connection type is ssh 
# (default is sftp)
#scp_if_ssh = True

[accelerate]
accelerate_port = 5099
accelerate_timeout = 30
accelerate_connect_timeout = 5.0