Hello,
I have two tasks in a playbook. The first task installs software with the command module. The second creates a file. I don’t want the second task to run until the software has finished installing. I’m pretty sure I need to use “when”, but I’m not sure what to evaluate to determine that the software has finished installing.
I’m thinking that I could use the stdout of this yum command? The last line output to the screen is “Complete!” would it be possible to base the “when” on the existence of that text string in stdout?
something like:
file:
path: /home/newdirectory
state: directory
when: [a line of text in stdout] == “Complete!”
Thanks much for any help
The following playbook worked for me, but I just started with Ansible and I’m wondering if I went about this wrong? I feel like I may have missed an easier way to say: do something only after a package has finished installing …
tasks: - name: Install python3 with a specific command and register stdout to a var command: sudo yum -y install python3 python3-pip register: varname - name: Create a directory only after python3 has been installed file: path: /home/ec2-user/newdirectory state: directory when: varname.stdout.find(“Complete!”) != -1
Avoid using the command module when there are specific Ansible modules available for the task. To install packages via yum, there's the yum module.
https://docs.ansible.com/ansible/latest/modules/yum_module.html
And use `become` to escalate privilege.
Your playbook can be rewritten like :
tasks:
- name: Install python3
yum:
name:
- python3
- python3-pip
become: true
- name: Create a directory
file:
path: /home/ec2-user/newdirectory
state: directory
The first task in the playbook will ensure that the packages are installed via yum. If there are any problems, Ansible will report it. The second task, ie, creating directory will run only if the first task succeeds. In general, playbook execution stops if any task fails. There is no need to check for the status of the first task.
Thank you – this worked and it’s a lot cleaner then the way I was doing it - I knew about the yum module but because it did not have any return values I was afraid that it just kicked off the update and moved on - I’m glad that’s not the case