I want to write a playbook for following scenario: reads a text file which has linux commands written it, executes them one by one, aborts if any command fails to execute, and if I correct the command and run the playbook again it should execute from where it was aborted (instead of executing from beginning)

sample file: sample.txt

echo "hello world"  
df -h  
free -m  
mkdir /tmp/`hostname`_bkp  
touch /tmp/`hostname`_bkp/file{1..5}  
mvn -version  
echo "directory and files created"  
echo "Bye.!"  

So for example if mvn -version fails to execute then ansible should abort.

How can achieve scenario through ansible?

  • Is there any particular reason to execute the commands from a file, and not just create task for each command? – 13dimitar Mar 1 '17 at 8:11
  • As there are lot of commands to be executed so I thought putting them consolidated in a text file and reading them line by line would be an idea. – hruday Mar 1 '17 at 11:52
  • 1
    actually it is better to use a tasks file for that - every command is a task, which will be executed. You can set conditions, loops, actions on success/unsuccessful completion of tasks and so on. I will draft an example with your commands and post them in an answer. Meanwhile, if you want a more useful/meaningful commands to be executed, please edit your questions - I'm not sure what exactly is the benefit of echo "Hello World!" for you :) – 13dimitar Mar 1 '17 at 11:58
  • I just gave an example that my text file looks like this. Actual commands may be tested once I get any working code. :) – hruday Mar 1 '17 at 13:21

Bellow is an example playbook, which executes a number of simple tasks.

 - hosts: localhost
    - name: say hi
      shell: echo "Hello, World!"

    - name: do df -h
      shell: df -h
      register: space

    - name: show the output of df -h
      debug: var=space

    - name: do free -m
      shell: free -m
      register: memory
      ignore_errors: yes

    - name: show memory stats
      debug: var=memory

    - name: create /tmp/"hostname"_bkp
      file: dest=/tmp/{{ ansible_nodename }}_bkp state=directory

    - name: create files
      file: dest=/tmp/{{ ansible_nodename }}_bkp/file{{ item }} state=touch
       - 1
       - 2
       - 3
       - 4
       - 5

It creates a directory and files at the desired location. You can also set ownership, permissions, which fit better your requirements.

ansible_nodename is an ansible fact (a variable), which gets collected at the beginning of a play.

You can see more information about the ansible file module here. Please have a look at the other ansible modules - they are plenty, easy to learn and powerful.

  • Thanks for your playbook, Here we have only a sample 4 or 5 commands. what if I have more than 20/25/50 commands then what would be the best approach through ansible.? – hruday Mar 1 '17 at 13:24
  • You can create roles (e.g. role apache, which will have few tasks file - one main which includes other tasks files (install nginx, deploy configuration files, harden apache), or role for ssh - tasks file which installs and hardens sshd. It is a common and a good practice to follow such structure of your playbooks. I really advise you to go through docs.ansible.com/ansible/intro_getting_started.html – 13dimitar Mar 1 '17 at 13:30

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.