June 8, 2019 Linux, DevOps 1 minute, 46 seconds

ansible-playbook -i, playbook.yml # be aware of the comma
ansible all -i localhost, -m setup -c local

- name: debug
  shell: ls -al /var/www/
  register: out

- name: out
    var: out.stdout_lines

ansible -m setup localhost | sed '1c {' | jq '.ansible_facts | keys'
# alternatively | python -mjson.tool

UPDATE: Since Ansible 2.7 there is a reboot module

Sometimes a reboot of a machine during Ansible operation is required. Reboot results in a lost ssh connection.

Here is a way how to properly tell Ansible to handle a machines reboot in the middle of its operation without failing.

The following playbook snippet reconfigures the network (especially the IP), performs a reboot of the server while wating for it to come back with its new IP address. host_name and ip_address must be provided from a external call.

- name: Configure interface
    src: etc/interfaces.j2
    dest: /etc/network/interfaces
    owner: root
    group: root
    mode: "u=rw,g=r,o=r"

- name: Set hostname
    name: '{{ host_name }}'
  tags: [hostname]

- name: Reboot Machine
  shell: sleep 2 && shutdown -r now "Ansible reboot"
  async: 1
  poll: 0
  ignore_errors: true

- name: Wait for the remote network interface to come back up
    module: wait_for
    host: "{{ ip_address }}"
    port: 22
    delay: 30
    timeout: 300
    state: started
  become: false
  register: wait_result

- name: Change ansible_ssh_host to new ip for further connections
    ansible_ssh_host: "{{ ip_address }}"

- name: Interface configuration finished
    msg: "Interface configuration finished"
  when: wait_result|succeeded

  1. Ansible
  2. Docker
  3. pip install molecule, docker-py, testinfra

graph LR A[Ansible Role] --> B((Molecule)) click A "" "Ansible Homepage" click B "" "Molecule Homepage" B --> C[Docker Container] click B "" "Docker Homepage" B --> D[Play Playbook] B --> E[Testinfra] click B "" "Testinfra Homepage" B --> F[Clean up]