ansible-2.x

Ansible playbook condition fails when variable has a default value

末鹿安然 提交于 2019-12-10 18:43:58
问题 Given the following playbook ( deployment.yml ): --- - name: Debug hosts: applicationservers tasks: - debug: msg="{{add_host_entries | default('false')}}" - debug: msg="{{add_host_entries | default('false') == 'true'}}" - debug: msg="Add host entries = {{add_host_entries | default('false') == 'true'}}" - include: add_host_entries.yml when: add_host_entries | default('false') == 'true' The condition to include add_host_entries.yml always fails, even if all of the above debug messages print

Ansible run always role

♀尐吖头ヾ 提交于 2019-12-10 10:28:16
问题 Is there any way to always run a role? I am creating lock file before starting any deployment to prevent parallel deployment. In case of any failure/success I want to delete the lock file. - { role: lock-deployment, tags: always } - { role: fetch-artifactory, tags: always } - { role: unlock-deployment, tags: always } I want to run unlock-deployment role irrespective of failure/success. 回答1: problem is I don't want to do block, rescue for every task. I just want to delete lock file in case of

How to use omit with Ansible and avoid any errors?

守給你的承諾、 提交于 2019-12-09 00:43:04
问题 I tried to use omit with an expression like this: id: "{{ openstack_networks.id | default(omit) }}" But it seems that it keeps failing with an exception when openstack_networks variable is not defined. What is the correct way to write this jinja2 filter? I want to omit the parameter in case openstack_networks.id does not exists. 回答1: Not super elegant, but 100% working solution to handle keys of possibly undefined parent dicts: id: "{{ (openstack_networks | default({})).id | default(omit) }}"

Register a variable output with Ansible CLI / Ad-Hoc

假如想象 提交于 2019-12-08 14:50:43
问题 Can I register the output of a task? Is there an argument with ansible command for that ? This is my command: ansible all -m ios_command -a"commands='show run'" -i Resources/Inventory/hosts I need this, because the output is a dictionary and I only need the value for one key. If this is not possible, is there a way to save the value of that key to a file? 回答1: I have found that you can convert ansible output to json when executing playbooks with " ANSIBLE_STDOUT_CALLBACK=json " preceding the

Ansible writing output from multiple task to a single file

若如初见. 提交于 2019-12-08 07:37:41
问题 In Ansible, I have written an Yaml playbook that takes list of host name and the executes command for each host. I have registered a variable for these task and at the end of executing a task I append output of each command to a single file. But every time I try to append to my output file, only the last record is getting persisted. --- - hosts: list_of_hosts become_user: some user vars: output: [] tasks: - name: some name command: some command register: output failed_when: "'FAILED' in

Ansible playbook fails to lock apt

☆樱花仙子☆ 提交于 2019-12-07 07:33:37
问题 I took over a project that is running on Ansible for server provisioning and management. I'm fairly new to Ansible but thanks to the good documentation I'm getting my head around it. Still I'm having an error which has the following output: failed: [build] (item=[u'software-properties-common', u'python-pycurl', u'openssh-server', u'ufw', u'unattended-upgrades', u'vim', u'curl', u'git', u'ntp']) => {"failed": true, "item": ["software-properties-common", "python-pycurl", "openssh-server", "ufw"

Ansible - Check if string exists in file

大城市里の小女人 提交于 2019-12-06 19:54:45
问题 I'm very new to Ansible Is it possible to check if a string exists in a file using Ansible. I want to check is a user has access to a server. this can be done on the server using cat /etc/passwd | grep username but I want Ansible to stop if the user is not there. I have tried to use the lineinfile but can't seem to get it to return. code - name: find lineinfile: dest=/etc/passwd regexp=[user] state=present line="user" The code above adds user to the file if he is not there. All i want to do

how to read json file using ansible

女生的网名这么多〃 提交于 2019-12-06 17:20:12
问题 I have a json file in the same directory where my ansible script is. Following is the content of json file: { "resources":[ {"name":"package1", "downloadURL":"path-to-file1" }, {"name":"package2", "downloadURL": "path-to-file2"} ] } I am trying to to download these packages using get_url. Following is the approach: --- - hosts: localhost vars: package_dir: "/var/opt/" version_file: "{{lookup('file','/home/shasha/devOps/tests/packageFile.json')}}" tasks: - name: Printing the file. debug: msg="

Ansible writing output from multiple task to a single file

心不动则不痛 提交于 2019-12-06 09:54:23
In Ansible, I have written an Yaml playbook that takes list of host name and the executes command for each host. I have registered a variable for these task and at the end of executing a task I append output of each command to a single file. But every time I try to append to my output file, only the last record is getting persisted. --- - hosts: list_of_hosts become_user: some user vars: output: [] tasks: - name: some name command: some command register: output failed_when: "'FAILED' in output" - debug: msg="{{output | to_nice_json}}" - local_action: copy content='{{output | to_nice_json}}'

Ansible: Can we run an include playbook Asynchronously?

你。 提交于 2019-12-06 08:15:46
问题 I'm interested to learn if Ansible can run an included playbook asynchronously? Basically what I'm trying to do is run a task "Fire and forget, check on it later." When I check on it later I also want to send a slack notification with the result. However I've notice the included playbook for slack notification takes a little longer than expected to complete and hence it holds up the rest of the playbook. What I want is to async the included playbook for slack notification so that the current