Using chown command in ansible?

Warum Debian und/oder eine seiner Spielarten? Was muss ich vorher wissen? Wo geht es nach der Installation weiter?
Antworten
balasahu
Beiträge: 1
Registriert: 01.05.2021 08:21:03
Kontaktdaten:

Using chown command in ansible?

Beitrag von balasahu » 01.05.2021 08:28:12

I have a command as

Code: Alles auswählen

sudo chown $(id -u):$(id -g) $HOME/.kube/config
I want to convert into ansible script. I have tried below

Code: Alles auswählen

- name: Changing ownership
      command: chown $(id -u):$(id -g) $HOME/.kube/config
      become: true 
but I am getting error as below

Code: Alles auswählen

fatal: FAILED! => {"changed": t> fatal: FAILED! => {"changed": true, "cmd": ["chown", "$(id", "-u):$(id", "-g)", "$HOME/.kube/config"], "delta": "0:00:00.003948", "end": "2019-07-17 07:22:31.798773", "msg": "non-zero return code", "rc": 1, "start": "2019-07-17 07:22:31.794825", "stderr": "chown: invalid option -- 'u'\nTry 'chown --help' for more information.", "stderr_lines": ["chown: invalid option -- 'u'", "Try 'chown --help' for more information."], "stdout": "", "stdout_lines": []}rue, "cmd": ["chown", "$(id", "-u):$(id", "-g)", "$HOME/.kube/config"], "delta": "0:00:00.003948", "end": "2019-07-17 07:22:31.798773", "msg": "non-zero return code", "rc": 1, "start": "2019-07-17 07:22:31.794825", "stderr": "chown: invalid option -- 'u'\nTry 'chown --help' for more information.", "stderr_lines": ["chown: invalid option -- 'u'", "Try 'chown --help' for more information."], "stdout": "", "stdout_lines": []}
EDIT: File module also did not work.

Code: Alles auswählen

- name: Create a symbolic link
    file:
      path: $HOME/.kube
      owner: $(id -u)
      group: $(id -g)

Benutzeravatar
hikaru
Moderator
Beiträge: 13559
Registriert: 09.04.2008 12:48:59

Re: Using chown command in ansible?

Beitrag von hikaru » 01.05.2021 09:59:18

I'm not familiar with ansible, but it seems to me you need to quote the id variables in some form because they aren't expanded properly.

JTH
Moderator
Beiträge: 3014
Registriert: 13.08.2008 17:01:41
Wohnort: Berlin

Re: Using chown command in ansible?

Beitrag von JTH » 01.05.2021 10:23:47

(Also not familiar with Ansible.)

Looking at the error message, I would guess the command is not executed in a shell but with exec (or similar), as usual.

This could work then:

Code: Alles auswählen

- name: Changing ownership
      command: sh -c "chown $(id -u):$(id -g) $HOME/.kube/config"
      become: true 

Edit:
Or try shell:

Code: Alles auswählen

- name: Changing ownership
      shell: chown $(id -u):$(id -g) $HOME/.kube/config
      become: true 
Manchmal bekannt als Just (another) Terminal Hacker.

eggy
Beiträge: 3331
Registriert: 10.05.2008 11:23:50

Re: Using chown command in ansible?

Beitrag von eggy » 01.05.2021 10:29:25

Use the file functions provided by ansible :
https://docs.ansible.com/ansible/2.4/file_module.html
Using $HOME isn't the right thing to do there, likely.

Antworten