Tag: playbook

Learning – Ansible 101 – Episode 3 – Introduction to Playbooks

Learning - Ansible 101 - Episode 3 - Introduction to Playbooks

Playbooks

Default module

The ansible command default module is command. So following commands are the same

ansible -i inventory multi -m command -a "date"
ansible -i inventory multi -a "date"

update_cache task

For idempotence, can update_cache task

Molecule

Testing Ansible roles using Molecule.

Background

Timeout

Timeout in 3600 seconds.

ansible -i inventory multi -b -B 3600 -P 0 -a "yum -y update"

Pulling time

Using -P for seconds

ansible -i inventory multi -b -B 3600 -P 0 -a "yum -y update"

Job ID

The result shows ansible job ID, which can be used to query in the server.

192.168.60.6 | CHANGED => {
    ...
    "ansible_job_id": "991487770448.3711",
    ...
    "results_file": "/root/.ansible_async/991487770448.3711",
    ...
}

Query by job id

ansible -i inventory db -b -m async_status -a "jid=991487770448.3711"

*Note: This will not show log or error of job"

Check log

ansible -i inventory multi -b -a "tail /var/log/messages"
ansible -i inventory multi -b -m shell -a "tail /var/log/messages | grep ansible-command | wc -l"

Cron

ansible -i inventory multi -b -m cron -a "name=something hour=4 job=/path/to/script.sh"
ansible -i inventory multi -b -m cron -a "name=something hour=4 job=/path/to/script.sh state=absent"

Git

ansible -i inventory multi -b -m git -a "repo=github_url_goes_here dest=/opt/app update=yes version=1.2.4"

Reuse SSH connection

Ansible configuration file ansible.cfg

[ssh_connection]
pipelining = True

Clean up

Destroy VMs

vagrant destroy -f

Playbooks

mkdir playbooks
cd playbooks

Inventory

[ec2]
35.175.148.144

[ec2:vars]
ansible_user=centos
ansible_ssh_private_key_file=~/.ssh/jeffgeerling_aws.pem

Create a shell script

shell-script.sh

# Install Apache.
yum install --quiet -y httpd httpd-devel
# Copy configuration files.
cp httpd.conf /etc/httpd/conf/httpd.conf
cp httpd-vhosts /etc/httpd/conf/httpd-vhosts.conf
# Start Apache and configure it to run at boot.
service httpd start
chconfig httpd on

Create playbook.yml

---
- name: Install Apache.
  hosts: all

  tasks:
    - name: Install Apache
      command: yum install --quiet -y httpd httpd-devel
    - name: Copy configuration files.
      command: >
        cp httpd.conf /etc/httpd/conf/httpd.conf
    - command: >
        cp httpd-vhosts /etc/httpd/conf/httpd-vhosts.conf
    - name: Start Apache and configure it to run at boot.
      command: service httpd start
    - command: chkconfig httpd on

or

---
- name: Install Apache.
  hosts: all

  tasks:
    - name: Install Apache
      shell: |
        yum install --quiet -y httpd httpd-devel
        cp httpd.conf /etc/httpd/conf/httpd.conf
        cp httpd-vhosts /etc/httpd/conf/httpd-vhosts.conf

    - name: Start Apache and configure it to run at boot.
      command: service httpd start
    - command: chkconfig httpd on

or

---
- name: Install Apache.
  hosts: all
  become: true

  tasks:
    - name: Install Apache
      yum:
        name:
          - httpd
          - httpd-devel
        state: present
      become: true
    - 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/config/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: true

    - command: >
        cp httpd-vhosts /etc/httpd/conf/httpd-vhosts.conf
    - name: Start Apache and configure it to run at boot.
      command: service httpd start
    - command: chkconfig httpd on

run on all nodes except one

ansible-playbook -i inventory multi --limit db
ansible-playbook -i inventory multi --limit=192.168.60.6
ansible-playbook -i inventory multi --limit="!:db"

List inventory

ansible-inventory --list i inventory

References

Ansible 101 - Episode 3 - Introduction to Playbooks