On Thursday, 11 October 2018 22.30.13 CEST 'Chris Bidwell - NOAA Federal' via Ansible Project wrote:
> cat check_for_reboot.yml
> ---
> - name: Check for reboot
> hosts: testserver
> become: yes
>
> vars_files:
> - passwd.yml
> - vars.yml
>
> vars:
> script_dir: "/home/tsg/scripts"
>
> tasks:
> - name: Does script directory exist?
> stat:
> path: "{{ script_dir }}"
> register: dir_exists
You don't need to do this, the file module will check if the directory exist and create if it doesn't exist.
>
> - name: Does script exist?
> stat:
> path: "{{ script_dir }}/needs-restarting.py"
> register: fic
You don't need to do this, the copy module will check if the file exist and create/copy if it doesn't exist.
If you are using Ansible 2.7 I recommend using the reboot module.
https://docs.ansible.com/ansible/2.7/modules/reboot_module.html#reboot-module
If not you should change it to this to avoid errors.
- name: "Rebooting {{ ansible_hostname }}"
shell: sleep 2 && reboot
async: 1
poll: 0
when: reboot_reqd.rc == 1
> - name: Wait for ssh to come back available
> wait_for:
> host: "{{
> (ansible_ssh_host|default(ansible_host))|default(inventory_hostname) }}"
> port: 22
> search_regex: OpenSSH
> delay: 10
> timeout: 240
> vars:
> ansible_connection: local
> when: reboot_reqd.rc == 1
Instead of wait_for I highly recommend using wait_for_connection instead because that module will check the remote host is ready to execute Ansible modules before it continues.
(With the reboot module this is not needed)
> Output:
> TASK [Create scripts dir]
> *************************************************************************************************************************************************
> changed: [testserver]
>
> TASK [Copy script]
> ********************************************************************************************************************************************************
> An exception occurred during task execution. To see the full traceback, use
> -vvv. The error was: If you are using a module and expect the file to exist
> on the remote, see the remote_src option
> fatal: [testserver]: FAILED! => {"changed": false, "msg": "Could not find
> or access '/home/tsg/scripts/needs-restarting.py' on the Ansible
> Controller.\nIf you are using a module and expect the file to exist on the
> remote, see the remote_src option"}
>
> Okay, so I know that file is on the src server (ansible server).
>
> [tsg@server]$ pwd
> /home/tsg/scripts
> [tsg@server scripts]$ll
> -rwxr-x---. 1 root tsg 8432 Aug 23 17:57 needs-restarting.py
The dot after the permission indicate that your file has a SELinux ACL, so you should check your SELinux log to see if SELinux is blocking Ansible to access the file.
--
Kai Stian Olstad