Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make an idempontent shell in Ansible

Tags:

ansible

I am using Ansible and I have a little problem trying to make idempotent a shell execution. The first I do is install python-apt package because I need it to use the apt module for install others packages. But every time that I run my playbook the shell task always runs and I want to make it idempotent. Here is my shell task:

- name: install pyton-apt
  shell: apt-get install -y python-apt

And here is the output, always running the above task:

$ ansible-playbook -i hosts site.yml 

PLAY [docker] ***************************************************************** 

GATHERING FACTS *************************************************************** 
ok: [10.0.3.240]

TASK: [docker | install pyton-apt] ******************************************** 
changed: [10.0.3.240]

TASK: [docker | install unzip] ************************************************ 
ok: [10.0.3.240]

PLAY RECAP ******************************************************************** 
10.0.3.240                 : ok=3    changed=1    unreachable=0    failed=0 
like image 417
Robert Avatar asked Dec 19 '22 13:12

Robert


1 Answers

You should use the ansible apt module to install python-apt, which will be idempotent out of the box: http://docs.ansible.com/apt_module.html

E.g.

- name: install python-apt
  apt: name=python-apt state=present

(Note using the apt module should automatically install python-apt on the remote host so I'm not sure why you would need to manually install it, see https://github.com/ansible/ansible/issues/4079)

Edit: If for some reason you can't use the inbuilt apt module to install python apt, the shell module provides the creates parameter to help make it idempotent.

- name: install python-apt
  shell: apt-get install -y python-apt >> /home/x/output.log creates=/home/x/output.log

What this means is that the shell module will not run if /home/x/output.log already exists.

like image 92
Woodham Avatar answered Jan 08 '23 02:01

Woodham