Ansible - /etc/profile.d scripts

Viewed 5179

I defined a envs.sh script inside the /etc/profile.d/ folder. When executing a ansible-playbook, I'm trying to get the value of this env var but it instead throws me an error:

Ansible test:

debug: msg="{{ ansible_env.NGN_VAL }} is an environment variable"

Error:

fatal: [xxx.yyy.zzz.kkk] => One or more undefined variables: 'dict object' has no attribute 'NGN_VAL'

FATAL: all hosts have already failed -- aborting

Why it doesn't execute the scripts inside that folder? When I connect through ssh, I echo it and it displays its value. How do I set remote environment variables and obtain them during ansible execution?

Thanks

2 Answers

In my point of view you can accomplish using a mix of ansible vault and system environment variables.

Create and encrypt a file with ansible vault (this is where you put the content of your remote env variable):

$ ansible-vault create vars_environment.yml

Put your password to encrypt this file and write content of your variable, for example:

ngn_val: supersecret

I use to load my permanent environment variables in /etc/profile.d directory but they are more paths:

  • /etc/profile.d
  • /etc/profile
  • /etc/environment
  • ~/.profile

It is a good way to write a template in your playbook, although your can use the copy module:

---
# environment.yml
- name: Get enviroment variable server
  hosts: debian.siccamdb.sm
  vars: 
    dest_profile_environment: /etc/profile.d/environment.sh
  vars_files:
    - vars_environment.yml
  tasks:
    - name: "Template {{ dest_profile_enviroment }}"
      template:
        src: environment.sh.j2
        dest: "{{ dest_profile_environment }}"
        mode: '0644'
    - name: Load env variable
      shell: ". {{ dest_profile_environment }} && echo $NGN_VAL"
      register: env_variable
    - name: Debug env variable
      debug:
        var: env_variable.stdout_lines[0]

The important thing here is in the shell task because you have to source your environment variable and the use it by the register keyword which can be used to capture the output of a command in a variable.

This is the template's content in this example the file environment.sh.j2:

export NGN_VAL={{ ngn_val }}

Type your password o the vars_environment.yml file after run the playbook:

ansible-playbook --vault-id @prompt environment.yml

The output is the following:

PLAY [Get enviroment variable server] *************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************************************************************************************************
ok: [debian.siccamdb.sm]

TASK [Template {{ dest_profile_enviroment }}] *****************************************************************************************************************************************************************************************
changed: [debian.siccamdb.sm]

TASK [Load env variable] **************************************************************************************************************************************************************************************************************
changed: [debian.siccamdb.sm]

TASK [Debug env variable] *************************************************************************************************************************************************************************************************************
ok: [debian.siccamdb.sm] => {
    "env_variable.stdout_lines[0]": "supersecret"
}

PLAY RECAP ****************************************************************************************************************************************************************************************************************************
debian.siccamdb.sm         : ok=4    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Another way to accomplish is using custom facts in your remote server. This is a good practice if you want to leave the content of your variable in the remote server and using in playbooks without the needs of ansible vault.

Related