qwert-poiuy
qwert-poiuy

Reputation: 23

How to run an ansible playbook as a cron job?

I'm a new ansible user. I want to use ansible playbook as a cron job i.e. run ansible-playbook site.yml command on a specific day/date. I set up the command using crontab -e which seems to be working fine.

Unfortunately, I get the following error:

fatal: [localhost]: FAILED! => {
  "ansible_facts": {}, 
  "changed": false, 
  "failed_modules": {
    "ansible.legacy.setup": {
      "cmd": "sysctl hw.model", 
      "failed": true, 
      "invocation": {
        "module_args": {
          "fact_path": "/etc/ansible/facts.d", 
          "filter": "*", 
          "gather_subset": ["all"], 
          "gather_timeout": 10}}, 
          "msg": "[Errno 2] No such file or directory: b'sysctl'", "rc": 2
        }
      }, 
      "msg": "The following modules failed to execute: ansible.legacy.setup"
    }

The extended error stack:

PLAY [First Playbook] ansible.cfg inventory site.yml TASK [Gathering Facts] fatal: [localhost]: FAILED! => {"ansible_facts": {}, "changed": false, "failed_modules": {"ansible.legacy.setup": {"cmd": "sysctl hw.model", "failed": true, "invocation": {"module_args": {"fact_path": "/etc/ansible/facts.d", "filter": "*", "gather_subset": ["all"], "gather_timeout": 10}}, "msg": "[Errno 2] No such file or directory: b'sysctl'", "rc": 2}}, "msg": "The following modules failed to execute: ansible.legacy.setup
"}  
localhost : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0

site.yaml file content:

- name: First Playbook
  hosts: all
  tasks:
    - name: Debug
      debug:
        msg: "1 2"

Inventory file content:

---
all:
  hosts:
    localhost:
      ansible_connection: local
      ansible_python_interpreter: "/usr/local/bin/python3"

ansible.cfg file content:

[defaults]
inventory=inventory

I am using macOS. I installed ansible using brew (not pip), and I'm targeting localhost.

If my understanding of the error message is correct, ansible is looking for fact.d in the default location /etc/ansible/facts.d. I tried creating ansible.cfg in the project directory, but I am not sure if the fact file also needs to be created and/or specified.

Upvotes: 0

Views: 2900

Answers (1)

Telinov Dmitri
Telinov Dmitri

Reputation: 441

This is because the $PATH is not updated with the path of sysctl when running from cron.

Either specify full path of sysctl binary when invoke it or pass the $PATH variable before the crontab invokation.

Upvotes: 1

Related Questions