Technotes

Technotes for future me

Ansible course notes

Git add submodule git submodule add <remote_url> <destination_folder> git submodule add git@github.com:adelerhof/ansible-role-postfix.git maintenance/roles/ansible-role-postfix

Init task

ansible-galaxy init

adhoc

ansible app -b -m group -a “name=admin state=present” ansible app -b -m user -a “name=johndoe group=admin createhome=yes” ansible app -b -m package -a “name=git state=present”

files ansible multi -m stat -a “path=/etc/environment”

Copy a file to the servers ansible multi -m copy -a “src=/etc/hosts dest=/tmp/hosts”

The src can be a file or a directory. If you include a trailing slash, only the contents of the directory will be copied into the dest. If you omit the trailing slash, the contents and the directory itself will be copied into the dest.

Retrieve a file from the servers ansible multi -b -m fetch -a “src=/etc/hosts dest=/tmp”

Fetch will, by default, put the /etc/hosts file from each server into a folder in the destination with the name of the host (in our case, the three IP addresses), then in the location defined by src. So, the db server’s hosts file will end up in /tmp/192.168.60.6/etc/hosts. ls /tmp/192.168.60.4/etc/hosts

You can add the parameter flat=yes, and set the dest to dest=/tmp/ (add a trailing slash), to make Ansible fetch the files directly into the /tmp directory. However, filenames must be unique for this to work, so it’s not as useful when copying down files from multiple hosts. Only use flat=yes if you’re copying files from a single host.

Create directories and files ansible multi -m file -a “dest=/tmp/test mode=644 state=directory”

Here’s how to create a symlink (set state=link):Deploy a version-controlled application

$ ansible multi -m file -a “src=/src/file dest=/dest/symlink
state=link”

The src is the symlink’s target file, and the dest is the path where the symlink itself should be.

Delete directories and files

You can set the state to absent to delete a file, directory, or symlink.

ansible multi -m file -a “dest=/tmp/test state=absent”

Manage cron jobs ansible multi -b -m cron -a “name=‘daily-cron-all-servers’
hour=4 job=’/path/to/daily-script.sh’”

ansible multi -b -m cron -a “name=‘daily-cron-all-servers’
state=absent”

Deploy a version-controlled application ansible app -b -m git -a “repo=git://example.com/path/to/repo.git
dest=/opt/myapp update=yes version=1.2.4” ansible app -b -a “/opt/myapp/update.sh”

Chapter 4 - Ansible Playbooks

---
- hosts: all
  become: yes

  tasks:
    - name: Install Apache.
      yum:
        name:
          - httpd
          - httpd-devel
        state: present

    - name: Copy configuration files.
      copy:
        src: "{{ item.src }}"
        dest: "{{ item.dest }}"
        owner: root
        group: root
        mode: 0644
      with_items:
        - src: httpd.conf
          dest: /etc/httpd/conf/httpd.conf
        - src: httpd-vhosts.conf
          dest: /etc/httpd/conf/httpd-vhosts.conf

    - name: Make sure Apache is started now and at boot.
      service:
        name: httpd
        state: started
        enabled: yes

Real-world playbook: CentOS Node.js app server

Last updated on 27 Oct 2021
Published on 16 Dec 2019
Edit on GitHub