-1

In my machine I have all envs in /etc/environment. I need to load one env to run a systemd service with a config file with that env.

Here my ansible code

- name: Enable filebeat
  become: true
  systemd:
    name: filebeat
    enabled: true
  environment:
    HELK_ID

I also tried this one:


- name: Start filebeat
  become: true
  shell: |
    source /etc/environment
    systemctl import-environment {{ansible_env.HELK_ID}} && systemctl start filebeat

I always get an the error AnsibleUndefinedVariable: 'HELK_ID' is undefined"

What am I wrong?

Daniele
  • 538
  • 1
  • 5
  • 17
  • Does this answer your question? [Replace string with custom environment value](https://stackoverflow.com/questions/76288180/replace-string-with-custom-environment-value) – U880D May 20 '23 at 06:56

1 Answers1

0

My fault. Ansible can't read environment variable during "machine build". So I have to replace a specifica string on reboot.

- template:
    src: filebeat.yml
    dest: /etc/myfile.yml
    mode: '0755'

- cron:
    name: "config myfile"
    reboot: yes
    job: "sed -i \"s/HELK_ID//\" /etc/myfile.yml && systemctl import-environment HELK_ID && systemctl start myfile"
Daniele
  • 538
  • 1
  • 5
  • 17