Posts

Showing posts with the label ansible

Ansible playbook when usage

Image
Clash Royale CLAN TAG #URR8PPP Ansible playbook when usage - name: restart dcache if mem low hosts: test tasks: - name: getMem shell: /bin/bash /etc/zabbix/shell/MonitorMem.sh register: memNum - name: restart dcache if mem low shell: killall -9 dcache when: memNum < 3 MonitorMem.sh returns a num(an integer) that represents free memory,I want to use when to decide whether execute restart action. but every time I run the playbook.it will skip restart action. please give me a hand, thanks in advance. 1 Answer 1 The shell documentation modules provides the structure of the return values, stdout contains the output of the executed command (and so is stdout_lines ). stdout stdout_lines I added also a filter to convert the value to an integer. - name: restart dcache if mem low hosts: test tasks: - name: getMem shell: /bin/bash /etc/zabbix/shell/MonitorMem.sh regist...

Use Ansible ec2_remote_facts to add_host, and connect to newly added host

Image
Clash Royale CLAN TAG #URR8PPP Use Ansible ec2_remote_facts to add_host, and connect to newly added host I have a newly created ec2 instance. I want to query AWS using Ansible's ec2_remote_facts to find that host's ip address, add it as a host, and connect to it in the next step of the playbook. ec2_remote_facts Here's what I have so far - - hosts: localhost tasks: - ec2_remote_facts: region: us-east-1 filters: image_id: ami-8b070ff4 register: ec2 - add_host: hostname: new_instance name: "{{ ec2.instances[0].private_ip_address }}" ansible_ssh_user: ubuntu ansible_ssh_private_key_file: ~/.ssh/my_key - hosts: new_instance tasks: - shell: 'echo $HOSTNAME' register: results - debug: var=results.stdout But I'm getting this error - "msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname new_instance: nodename nor servname provided,...

Locate string/integer not in list

Image
Clash Royale CLAN TAG #URR8PPP Locate string/integer not in list Im pulling a list of VLANs from our IPAM via an API and I want to be able to locate an unused "vlanId" that isnt in the list. I was expecting that I could use with_items for the JSON content and then use the random function with an until loop and occasionally it will initially generate a number that doesnt exist in the list. Mostly it just gets stuck and doesnt generate a new random number when the one generated already exists. Playbook: - uri: url: "#" validate_certs: no headers: token: "{{ token }}" method: GET force_basic_auth: yes return_content: yes register: ipam - set_fact: value: "{{ 4094 | random(start=1) }}" until: value not in item.vlanId with_items: "{{ ipam.json.data }}" retries: 4093 - debug: msg="{{ value }}" Relevant Output: ok: [localhost] => (item={u'domainId':...

Ansible - joining two nested lists without the same key

Image
Clash Royale CLAN TAG #URR8PPP Ansible - joining two nested lists without the same key I am currently trying to combine two different dictionaries together. The thing that ties them together is the login field, but this is not the "key" in both dicts. --- - hosts: localhost gather_facts: no vars: userlist: tom123: guid: "tom" sally: guid: "sally" userteamlist: tom: teams: "group4, group6, group5" sally: teams: "group1, group2, group3" How do I get these two lists to merge based on the "guid"? The "guid" is the key in one list (userteamlist) and a "value" (guid) in the other list (userlist). I have used this Stackoverflow link for joining two dicts with the same key, but have not figured out to do this with key+subelement. Thoughts? Apologies all, I am looking strictly for an Ansible solution to this problem. ...