0

I have servers without python2 installed by default (the target servers are mainly under Alpine Linux) and i use the playbook below to fix that.

- hosts: all
  become: yes
  remote_user: root
  gather_facts: no
  pre_tasks:
      # Python is not installed by default
      - name: 'Install python'
        raw: apk --no-cache add python

  tasks:
      - name: 'Upgrade Operating System'
        apk:
                available: yes
                upgrade: yes

      - name: 'Install base software'
        apk:
                name: '{{ item }}'
                state: latest
        with_items:
                - tmux
                - vim
                - git

I use raw module because without python package installed i got this output :

TASK [Upgrade Operating System] ******************************************************************************************************************************************
fatal: [172.16.160.254]: FAILED! => {"changed": false, "failed": true, "module_stderr": "Shared connection to 172.16.160.254 closed.\r\n", "module_stdout": "/bin/sh: /usr/bin/python: not found\r\n", "msg": "MODULE FAILURE", "rc": 0}

Actually the playbook works fine but the state is 'changed=1' for every time 'Install python' tasks run.

Is it possible to make it idempotent (with play recap 'changed=0' and everything are 'ok=x') ? If yes can you help me to do this ?

0

I'm not 100% certain, but I think it might already be almost idempotent because of how APK works. Based on what I can tell, APK will return success without doing anything if you tell it to add a package that is already installed (at least, that's what it's doing when trying to install python2 on a system I've already got it installed on). The only potential caveat is that it will upgrade the package if there is a newer version, but I don't think there is any way without embedding a shell script into the playbook that you can work around that (you would have to parse the output of apk version to figure out if it would update it or not).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.