0

I have a remote server with a file on it. One line in that file is the following:

authorizationToken=<hash or empty string>

I'd like to set a variable in a role to the value of whatever's after the equals sign on that line, if anything (it might be a hash, or it might be an empty string).

What's the least awful way to do this?

iLikeDirt
  • 606
  • 5
  • 17

2 Answers2

1

Konstantin linked you to a solution using custom facts, but you can also get at the same value using a simple task like:

- name: get authorizationToken
  command: >
    awk -F= '$1 == "authorizationToken" {print $2}' /path/to/configfile
  register: token

Now the value is available in subsequent tasks as token.stdout.

larsks
  • 277,717
  • 41
  • 399
  • 399
0

After trying for a while to do this the "right" way, in the end the path of least resistance was using a shell script:

- name: Record autorization Token
  shell: "cat {{ buildagent_dir }}/conf/buildAgent.properties 2>/dev/null | grep authorizationToken | cut -d '=' -f 2"
  register: token
iLikeDirt
  • 606
  • 5
  • 17
  • Note that you don't need `cat` in any case, and by using `awk` you can replace *both* the `grep` and `cut`, and since you're now down to a single command you can replace `shell` with `command`. – larsks Oct 13 '17 at 21:40
  • I know it's not the most character-efficient way of doing it, but I generally prefer explicitness and readability to efficiency when writing shell commands, and I find awk syntax to be tougher to parse at a glance. Maybe I'm weird. – iLikeDirt Oct 14 '17 at 16:01