Home > other >  How i can define conditinal value for a parameter in task defintion
How i can define conditinal value for a parameter in task defintion

Time:01-17

I'm a newbe in Ansible ant need a help for task definition.

My current task:

- name: Add the user
  user:
    name:  "{{ user_name }}"
    comment:  "{{ user_name }} -User"
    groups:  "{{ user_groups | default([users]) | join(',') }}"
    append: False # not required. If C(yes), add the user to the groups specified in C(groups). If C(no), user will only be added to the groups specified in C(groups), removing them from all other groups. Mutually exclusive with C(local)
    password:  "{{ lookup('file', '{{ user_data_folder }}/{{ user_name }}/user_password.txt') | password_hash('sha512')  }}" # not required. Optionally set the user's password to this crypted value. On macOS systems, this value has to be cleartext. Beware of security issues. To create a disabled account on Linux systems, set this to C('!') or C('*'). To create a disabled account on OpenBSD, set this to C('*************'). See U(https://docs.ansible.com/ansible/faq.html#how-do-i-generate-encrypted-passwords-for-the-user-module) for details on various ways to generate these password values.
    state: present # not required. choices: absent;present. Whether the account should exist or not, taking action if the state is different from what is stated.
    update_password: on_create # not required. choices: always;on_create. C(always) will upde passwords if they differ. C(on_create) will only set the password for newly created users.
    shell: "{{ user_shell | default('/bin/bash') }}"

Works very well.

Now, I'd like to change the "groups"-Item definition.

The expectation is:

  • if exists the file "{{ lookup('file', '{{ user_data_folder }}/{{ user_name }}/user_groups.txt') take the definition
  • if the variable user_groups is set, so overwrite the file content else: use default([users])

I can describe this in other way INIT: USER_GROUPS_FOR_TASK=[users]

IF Fileexists THEN get the content USER_GROUPS_FOR_TASK = content of the file ENDIF IF VAR(user_groups) exists USER_GROUPS_FOR_TASK = VAR(user_groups) ENDIF

I tried with:

a) groups:  "{{ lookup('file', '{{ user_data_folder }}/{{ user_name }}/user_groups.txt', errors='ignore' ) | user_groups | default([users]) | join(',')   }}"
b) groups:  "{{ lookup('file', '{{ user_data_folder }}/{{ user_name }}/user_groups.txt', errors='ignore' ) | default([users]) | join(',')   }}"

but without any luck if the file '{{ user_data_folder }}/{{ user_name }}/user_groups.txt' does not exist.

Can somebody help me?

i tried with: a) groups: "{{ lookup('file', '{{ user_data_folder }}/{{ user_name }}/user_groups.txt', errors='ignore' ) | user_groups | default([users]) | join(',') }}" b) groups: "{{ lookup('file', '{{ user_data_folder }}/{{ user_name }}/user_groups.txt', errors='ignore' ) | default([users]) | join(',') }}"

The expectation is:

  • if exists the file "{{ lookup('file', '{{ user_data_folder }}/{{ user_name }}/user_groups.txt') take the definition
  • if the variable user_groups is set, so overwrite the file content else: use default([users])

I can describe this in other way INIT: USER_GROUPS_FOR_TASK=[users]

IF Fileexists THEN get the content USER_GROUPS_FOR_TASK = content of the file ENDIF IF VAR(user_groups) exists USER_GROUPS_FOR_TASK = VAR(user_groups) ENDIF

CodePudding user response:

I'm almost positive you'll have to either use a set_fact: (so you can tell ansible you're ok with the missing file) or first use a lookup("fileglob" in order to check for the presence of the file and skip trying to read it if it's missing. The |default is exclusively for unset or empty vars, and not a general try: except: mechanism in ansible. The choice of which to use is mostly whether any other process in your playbook would benefit from knowing that file exists or not

The set_fact: approach also has two "styles" in it, with and without the explicit ignore_errors:, depending on the style of your team

- name: maybe get groups from file
  set_fact:
    file_groups: '{{ lookup("file", ...) }}'
  ignore_errors: true

- name: or, the implicit ignore version
  set_fact:
    file_groups: '{{ item }}'
  # this one will still whine a warning,
  # but has an implicit ignore_errors: true on it
  with_file: '{{ user_data_folder }}/{{ user_name }}/user_groups.txt'

- debug:
    msg: groups is '{{ file_groups | default([users]) | join(",") }}'
  • Related