ansible-playbook

Abort execution of remaining task if certain condition is failed

◇◆丶佛笑我妖孽 提交于 2019-12-02 18:50:31
I want to abort execution of remaining task if certain condition is failed. and display proper error message. So instead of skipping remaining task I want to show error message and stop execution of ansible playbook. Lets say I am running below command $ ansible-playbook playbook.yml -e "param1=value1 param2=value" My playbook look like this:- playbook.yml:- --- - hosts: local user: roop gather_facts: no vars: {param1: "", param2: ""} tasks: #check whether param1 defined - name: 'Check for valid param1' shell: echo {{ param1 }} register: isValidParam1 when: param1 !="" #check if param1 is null

Ansible condition when string not matching

删除回忆录丶 提交于 2019-12-02 18:01:37
I am trying to write an Ansible playbook that only compiles Nginx if it's not already present and at the current version. However it compiles every time which is undesirable. This is what I have: - shell: /usr/local/nginx/sbin/nginx -v 2>&1 register: nginxVersion - debug: var=nginxVersion - name: install nginx shell: /var/local/ansible/nginx/makenginx.sh when: "not nginxVersion == 'nginx version: nginx/1.8.0'" become: yes The script all works apart from the fact that it runs the shell script every time to compile Nginx. The debug output for nginxVersion is: ok: [server] => { "var": {

How to continue execution on failed task after fixing error in playbook?

不想你离开。 提交于 2019-12-02 17:51:11
When writing and debugging Ansible playbooks, typical workflow is as follows: ansible-playbook ./main.yaml Playbook fails on some task Fix this task and repeat line 1, waiting for all previous tasks to execute again. Which takes a lot of time Ideally, i'd like to resume execution on failed task, having inventory and all facts collected by previous tasks. Is it even possible? How to make playbook writing/debugging faster? Mxx Take a look at http://docs.ansible.com/playbooks_startnstep.html . If you want to start executing your playbook at a particular task, you can do so with the --start-at

ansible register with loop debug print not working

浪子不回头ぞ 提交于 2019-12-02 17:28:36
问题 i have a simple playbook that is supposed to display my services status. and i want to view the output from the machine to see if the status is active or not. so i used a debug print, like so: - name: name_of_services shell: systemctl status {{item}} with_items: - service1 - service2 register: out - debug: var=item.stdout_lines with_items: out.results when i execute this i get a lot of info i don't want plus the item.stdout_lines info that i do want in the end of it. how is it possible to

include tasks from another role in ansible playbook

試著忘記壹切 提交于 2019-12-02 17:24:10
I'm designing a kind of playbook lib with individual tasks so in the usual roles repo, I have something like: roles ├── common │ └── tasks │ ├── A.yml │ ├── B.yml │ ├── C.yml │ ├── D.yml │ ├── login.yml │ ├── logout.yml │ └── save.yml ├── custom_stuff_workflow │ └── tasks │ └── main.yml └── other_stuff_workflow └── tasks └── main.yml my main.yml in custom_stuff_workflow then contain something like: --- - include: login.yml - include: A.yml - include: C.yml - include: save.yml - include: logout.yml and this one in the other workflow: --- - include: login.yml - include: B.yml - include: A.yml -

docker extra_host parameter expects a dictionary value for hostname, how can I use a variable?

Deadly 提交于 2019-12-02 17:12:51
问题 In ansible playbook docker parameter extra_host takes two parts host: ip_address. I am trying to pass the host and ipaddress in as variables. They are from prompt vars. The end result in my hosts file is: 1.2.3.4 {{server_hostname}}. Here is the code: vars_prompt: - name: "server_ip" prompt: "Please enter the server IP address" private: no - name: "server_hostname" prompt: "Please enter the server hostname" private: no tasks: - name: Install Tomcat docker: image: tomcat:8.0 pull: missing name

Ansible Handler notify vs register

我怕爱的太早我们不能终老 提交于 2019-12-02 16:57:39
So after reading Ansible docs, I found out that Handlers are only fired when tasks report changes, so for example: some tasks ... notify: nginx_restart # our handler - name: nginx_restart vs some tasks ... register: nginx_restart # do this after nginx_restart changes when: nginx_restart|changed Is there any difference between these 2 methods? When should I use each of them? For me, register seems to have more functionality here, unless I am missing something... There are some differences and which is better depends on the situation. Handlers will only be visible in the output if they have

Ansible: Set variable to file content

爷,独闯天下 提交于 2019-12-02 16:55:14
I'm using the ec2 module with ansible-playbook I want to set a variable to the contents of a file. Here's how I'm currently doing it. Var with the filename shell task to cat the file use the result of the cat to pass to the ec2 module. Example contents of my playbook. vars: amazon_linux_ami: "ami-fb8e9292" user_data_file: "base-ami-userdata.sh" tasks: - name: user_data_contents shell: cat {{ user_data_file }} register: user_data_action - name: launch ec2-instance local_action: ... user_data: "{{ user_data_action.stdout }}" I assume there's a much easier way to do this, but I couldn't find it

How to execute a shell script on a remote server using Ansible?

三世轮回 提交于 2019-12-02 16:08:24
I am planning to execute a shell script on a remote server using Ansible playbook. test.sh: touch test.txt Playbook: --- - name: Transfer and execute a script. hosts: server user: test_user sudo: yes tasks: - name: Transfer the script copy: src=test.sh dest=/home/test_user mode=0777 - name: Execute the script local_action: command sudo sh /home/test_user/test.sh When I run the playbook, the transfer successfully occurs but the script is not executed. Pasi H local_action runs the command on the local server, not on the servers you specify in hosts parameter. Change your "Execute the script"

How to write dynamic variable in Ansible playbook

穿精又带淫゛_ 提交于 2019-12-02 15:48:32
Based on extra vars parameter I Need to write variable value in ansible playbook ansible-playbook playbook.yml -e "param1=value1 param2=value2 param3=value3" If only param1 passed myvariable: 'param1' If only param1,param2 passed myvariable: 'param1,param2' If param1,param2,param3 are passed then variable value will be myvariable: 'param1,param2,param3' When I try to create variable dynamically through template then my playbook always takes previous variable value. But inside dest=roles/myrole/vars/main.yml its writing correct value. What I make a try here - hosts: local user: roop gather